files.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <div class="files">
  3. <el-drawer v-model="drawer" direction="rtl" append-to-body style="min-width: 500px;">
  4. <template #header>
  5. <h4>上传图片</h4>
  6. </template>
  7. <template #default>
  8. <div>
  9. <el-upload v-model:file-list="fileList" multiple
  10. action="#"
  11. list-type="picture-card"
  12. :on-change="beforeUpload"
  13. :auto-upload="false"
  14. :on-preview="handlePictureCardPreview">
  15. <el-icon>
  16. <Plus />
  17. </el-icon>
  18. </el-upload>
  19. <el-dialog v-model="dialogVisible">
  20. <img w-full :src="dialogImageUrl" style="width: 100%;height: auto;"/>
  21. </el-dialog>
  22. </div>
  23. </template>
  24. <template #footer>
  25. <div style="flex: auto">
  26. <el-button @click="drawer = false">取消</el-button>
  27. <el-button type="primary" @click="confirmClick">立即提交</el-button>
  28. </div>
  29. </template>
  30. </el-drawer>
  31. </div>
  32. </template>
  33. <script lang="ts" setup>
  34. import { ref,reactive} from "vue";
  35. import * as qiniu from 'qiniu-js';
  36. import { upFileToken,medicineimgedit } from "@/api";
  37. import type { UploadProps, UploadUserFile } from 'element-plus'
  38. const dialogImageUrl = ref('')
  39. const dialogVisible = ref(false)
  40. const fileList:any = ref<UploadUserFile[]>([])//上传的图片列表
  41. const beforeUpload = async(file:any) => {
  42. console.log('上传',file)
  43. var strtype:any = file.name.substring(file.name.lastIndexOf('.') + 1); //获取后缀 png jpg
  44. const token:any = await upFileTokenApi(strtype)
  45. handleUpload(file,token.data);
  46. }
  47. //获取token
  48. const upFileTokenApi = async (strtype:any)=>{
  49. return await upFileToken({T_suffix: strtype})
  50. }
  51. /**
  52. * 上传七牛云
  53. */
  54. const handleUpload = (file:any,qiniuToken:any)=> {
  55. var config = {
  56. useCdnDomain: false, //表示是否使用 cdn 加速域名,为布尔值,true 表示使用,默认为 false。
  57. region: qiniu.region.z2,
  58. domain: "https://qiniu.region.z2", //配置好的七牛云域名 如 https://cdn.qniyun.com/
  59. chunkSize: 1000, //每个分片的大小,单位mb,默认值3
  60. forceDirect: false //直传还是断点续传方式,true为直传
  61. };
  62. var putExtra:any = {
  63. params: {},
  64. mimeType: [] || null
  65. };
  66. var key = file.name || null
  67. const observable = qiniu.upload(objectToBinary(file), key, qiniuToken);
  68. // const observable = qiniu.upload(file, key, qiniuToken, putExtra, config);
  69. observable.subscribe({
  70. next(res:any) {
  71. // 上传进度信息
  72. console.log(res);
  73. },
  74. error(err:any) {
  75. // 上传错误信息
  76. console.log(`上传错误信息: ${err}`);
  77. },
  78. complete(res:any) {
  79. // 上传完成信息
  80. const j = fileList.value.findIndex((item:any)=>{return item.name == file.name})
  81. fileList.value[j] = { name: file.name, url: res.key }
  82. // 更新你的文件列表或进行其他操作
  83. },
  84. });
  85. }
  86. const objectToBinary = (obj:any)=> {
  87. // 将对象转换为JSON字符串
  88. const jsonStr = JSON.stringify(obj);
  89. // 将字符串转换为二进制数据
  90. const binaryStr = unescape(encodeURIComponent(jsonStr));
  91. // 创建Blob对象
  92. const blob = new Blob([binaryStr], { type: 'application/octet-stream' });
  93. // 创建File对象
  94. const file = new File([blob], "filename.bin", {
  95. type: 'application/octet-stream',
  96. });
  97. return file;
  98. }
  99. // 示例用法
  100. const obj = { key: "value" };
  101. const file = objectToBinary(obj);
  102. console.log(file);
  103. /**
  104. * 查看图片
  105. */
  106. const handlePictureCardPreview: UploadProps['onPreview'] = (uploadFile) => {
  107. dialogImageUrl.value = uploadFile.url!
  108. dialogVisible.value = true
  109. }
  110. const drawer = ref(false)
  111. //请求参数
  112. const initParam:any = reactive({
  113. "id": "",//生产企业
  114. "img": "",//图片地址逗号分隔
  115. })
  116. const confirmClick = () => {
  117. let arr = [...fileList.value]
  118. let array:any = []
  119. arr.forEach((item)=>{
  120. array.push(item.url)
  121. })
  122. initParam.img = array.join(',')
  123. setMedicineeditApi()
  124. }
  125. const emit:any = defineEmits(['upTable'])
  126. const setMedicineeditApi = async()=>{
  127. const reslut:any = await medicineimgedit(initParam)
  128. if(reslut.code==200){
  129. emit('upTable', '');
  130. drawer.value = false
  131. }
  132. }
  133. const showDrawer = () => {
  134. drawer.value = true
  135. }
  136. //暴露方法
  137. defineExpose({
  138. showDrawer,initParam
  139. })
  140. </script>
  141. <style lang="scss">
  142. /* @import url(); 引入css类 */
  143. </style>