upimg.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <!-- 上传一张图片回显 -->
  2. <template>
  3. <div class="">
  4. <div class="upImg">
  5. <div class="upImg-item" v-if="!imgData">
  6. <input class="upImg-item-input" type="file" :disabled="props.disabled" placeholder="选择图片" @change="changesFun" />
  7. <div class="upImg-item-icon"><el-icon>
  8. <Plus />
  9. </el-icon></div>
  10. </div>
  11. <div class="upImg-img" v-else @mouseenter="showModel = true" @mouseleave="showModel = false">
  12. <img :src="imgData" style="width: 100%;height: 100%;object-fit: cover;">
  13. <div class="upImg-img-model" v-if='showModel'>
  14. <el-icon @click="clickShow" style="color: #fff;cursor: pointer;font-size: 18px;margin-right: 5px;"><View /></el-icon>
  15. <el-icon @click="clickDel" v-if="!props.disabled" style="color: #fff;cursor: pointer;font-size: 16px;"><Delete /></el-icon>
  16. </div>
  17. </div>
  18. </div>
  19. <el-dialog v-model="dialogVisible" style="z-index: 10;" :append-to-body="true">
  20. <img w-full :src="imgData" style="width: 100%;height: auto;"/>
  21. </el-dialog>
  22. </div>
  23. </template>
  24. <script setup lang="ts">
  25. import {ref } from 'vue';
  26. import { UpFileToken } from '@/api/public/index'
  27. import { GlobalStore } from '@/stores/index'
  28. import { resolve } from 'path';
  29. import * as qiniu from "qiniu-js";
  30. import { ElMessage } from 'element-plus'
  31. const props = defineProps({
  32. imgData: {
  33. type: String,
  34. default: () => '',
  35. },
  36. disabled:{
  37. type: Boolean,
  38. default: () => false,
  39. }
  40. });
  41. const uploadData = {
  42. token: '',
  43. key: ''
  44. }
  45. const globalStore = GlobalStore()
  46. const showModel = ref(false)
  47. const emits = defineEmits()
  48. const files = ref()
  49. const dialogVisible = ref(false)
  50. const changesFun = async (e: any) => {
  51. files.value = e.target.files[0]
  52. await clickToken()//获取token
  53. uploadData.key = e.target.files[0].name || null
  54. QiniuYun()
  55. console.log('选择', e, uploadData)
  56. }
  57. //获取token Api
  58. const clickToken = async () => {
  59. return new Promise((resolve: any) => {
  60. UpFileToken({ User_tokey: globalStore.GET_User_tokey }).then(res => {
  61. uploadData.token = res.Data as string
  62. resolve()
  63. })
  64. })
  65. }
  66. //函数
  67. const QiniuYun = async () => {
  68. var config = {
  69. useCdnDomain: false, //表示是否使用 cdn 加速域名,为布尔值,true 表示使用,默认为 false。
  70. region: qiniu.region.z2,
  71. domain: "https://up-z2.qiniup.com", //配置好的七牛云域名 如 https://cdn.qniyun.com/
  72. chunkSize: 1000, //每个分片的大小,单位mb,默认值3
  73. forceDirect: false //直传还是断点续传方式,true为直传
  74. };
  75. var putExtra: any = {
  76. fname: files.value.name, //文件原始文件名
  77. params: {},
  78. mimeType: [] || null
  79. };
  80. var observable = qiniu.upload(files.value, uploadData.key, uploadData.token, putExtra, config);
  81. // 设置实例的监听对象
  82. var observer = {
  83. next(res: any) {
  84. console.log('实例监听对象', res)
  85. },
  86. // 接收上传错误信息
  87. error(err: any) {
  88. switch (err.code) {
  89. case 413:
  90. ElMessage.error('错了哦,图片可能太大了哦')
  91. break
  92. case 408:
  93. ElMessage.error('错了哦,请求超时')
  94. break
  95. default:
  96. ElMessage.error('错了哦,上传错误')
  97. }
  98. },
  99. // 接收上传完成后的信息
  100. complete(com: any) {
  101. let ERPOSS = import.meta.env.VITE_BZD_ERPOSS_APP_API
  102. emits('update:upImg',ERPOSS+com.key);
  103. }
  104. };
  105. observable.subscribe(observer);
  106. }
  107. //删除
  108. const clickDel = async (e: any) => {
  109. emits('update:upImg', '');
  110. }
  111. //函数
  112. const clickShow = async ()=>{
  113. dialogVisible.value = true
  114. }
  115. </script>
  116. <style lang="scss">
  117. /* @import url(); 引入css类 */
  118. .upImg {
  119. width: 50px;
  120. height: 50px;
  121. position: relative;
  122. overflow: hidden;
  123. border-radius: 6px;
  124. &-item:hover {
  125. border: 1px dashed var(--el-color-primary);
  126. }
  127. &-item {
  128. border: 1px dashed var(--el-border-color-darker);
  129. position: absolute;
  130. top: 0;
  131. left: 0;
  132. width: 100%;
  133. height: 100%;
  134. cursor: pointer;
  135. &-input {
  136. position: absolute;
  137. top: 0;
  138. left: 0;
  139. width: 100%;
  140. height: 100%;
  141. opacity: 0;
  142. z-index: 1;
  143. cursor: pointer;
  144. }
  145. &-icon {
  146. width: 100%;
  147. height: 100%;
  148. display: flex;
  149. align-items: center;
  150. justify-content: center;
  151. cursor: pointer;
  152. }
  153. }
  154. &-img {
  155. width: 100%;
  156. height: 100%;
  157. position: relative;
  158. &-model {
  159. position: absolute;
  160. top: 0;
  161. left: 0;
  162. width: 100%;
  163. height: 100%;
  164. background: rgba(#000, 0.5);
  165. display: flex;
  166. align-items: center;
  167. justify-content: center;
  168. }
  169. }
  170. }
  171. </style>