QiniuUpload.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <template>
  2. <div class="clearfix up-box">
  3. <div class="up-img-view" v-for="(item, index) in Fimages" :key="index">
  4. <block v-if="item">
  5. <image class="cata-img" :src="item" mode="aspectFill"></image>
  6. <text class="custom-icon custom-icon-ic_searchclosed del-icon" @click="handleRemove(item, index)"></text>
  7. </block>
  8. </div>
  9. <div @click="uploadAva" class="upload-btn"><text class="custom-icon custom-icon-xinzeng"></text></div>
  10. </div>
  11. </template>
  12. <script>
  13. import webUrl from '@/common/config.js';
  14. import qiniuUploader from './qiniuUploader.js';
  15. export default {
  16. data() {
  17. return {
  18. Fimages: [],
  19. qn_token: ''
  20. };
  21. },
  22. computed: {
  23. enToken() {
  24. return this.$store.state.enToken;
  25. }
  26. },
  27. props: {
  28. images: {
  29. type: Array,
  30. default: () => {
  31. return [];
  32. }
  33. },
  34. //最多可以选择的图片张数,默认9
  35. count: {
  36. type: Number,
  37. default: 9
  38. },
  39. // album 从相册选图,camera 使用相机,默认二者都有。如需直接开相机或直接选相册,请只使用一个选项
  40. sourceType: {
  41. type: Array,
  42. default: () => {
  43. return ['album', 'camera'];
  44. }
  45. }
  46. },
  47. watch: {
  48. images(val) {
  49. this.Fimages = val;
  50. }
  51. },
  52. created() {
  53. this.Fimages = this.images;
  54. // 上来获取七牛token
  55. this.getQiniuToken();
  56. },
  57. methods: {
  58. getQiniuToken() {
  59. this.$u.api
  60. .uploadToken({
  61. bucket: webUrl.QINIU_KEY,
  62. key: 0
  63. })
  64. .then(res => {
  65. this.qn_token = res.data;
  66. });
  67. },
  68. // 移除图片 请求七牛云接口
  69. async handleRemove(url, index) {
  70. this.Fimages.splice(index, 1);
  71. this.$emit('handleRemove', this.Fimages);
  72. return;
  73. const urlArr = url.split('/');
  74. const nameSlice = urlArr[urlArr.length - 6];
  75. let name = '';
  76. if (urlArr[urlArr.length - 1] === '750') {
  77. name = nameSlice.slice(0, nameSlice.indexOf('?'));
  78. } else {
  79. name = urlArr[urlArr.length - 1];
  80. }
  81. let key = `${this.enToken}/${name}`;
  82. this.$u.api
  83. .UploadDel({
  84. bucket: webUrl.QINIU_KEY,
  85. key: key
  86. })
  87. .then(res => {});
  88. },
  89. // 上传头像
  90. uploadAva() {
  91. uni.chooseImage({
  92. count: this.count,
  93. sourceType: this.sourceType, //从相册选择
  94. success: async res => {
  95. res.tempFilePaths.forEach(imgPath => {
  96. this.upImg(imgPath);
  97. });
  98. }
  99. });
  100. },
  101. // 请求上传图片接口
  102. async upImg(imgPath) {
  103. // const imgPath = res.tempFilePaths[0]; //选择图片的路径
  104. const imgName = imgPath.split('/')[imgPath.split('/').length - 1];
  105. const key = `${this.enToken}/${imgName}`; //图片和企业token拼接 为自定义文件名
  106. let domain = webUrl.QN_UP; //文件上传地址
  107. let token = this.qn_token; //token为七牛云的token一般由后台接口提供
  108. let filePath = imgPath; //为需要上传的文件
  109. uni.showLoading();
  110. qiniuUploader.upload(
  111. filePath,
  112. res => {
  113. //图片上传完成后返回值
  114. // 压缩图片
  115. uni.hideLoading();
  116. // const imgData = {
  117. // ...res,
  118. // key: res.key + '?imageView2/2/w/750/h/750'
  119. // };
  120. this.$emit('uploadSuccess', `${webUrl.QINIU_URL}/${res.key}`);
  121. },
  122. error => {
  123. uni.hideLoading();
  124. // resolve(error)
  125. },
  126. {
  127. region: 'SCN', // (必须填写正确)ECN, SCN, NCN, NA, ASG,分别对应七牛的:华东,华南,华北,北美,新加坡 5 个区域
  128. domain: domain, // // bucket 域名,下载资源时用到。如果设置,会在 success callback 的 res 参数加上可以直接使用的 ImageURL 字 段。否则需要自己拼接
  129. key: key, // [非必须]自定义文件 key。如果不设置,默认为使用微信小程序 API 的临时文件名
  130. // 以下方法三选一即可,优先级为:uptoken > uptokenURL > uptokenFunc
  131. uptoken: token // 由其他程序生成七牛 uptoken
  132. },
  133. res => {
  134. uni.hideLoading();
  135. //上传进度
  136. if (res.progress === 100) {
  137. // resolve(keys);
  138. }
  139. }
  140. );
  141. }
  142. }
  143. };
  144. </script>
  145. <style scoped lang="scss">
  146. .up-box {
  147. display: inline-block;
  148. .cata-img {
  149. width: 120rpx;
  150. height: 120rpx;
  151. border-radius: 8rpx;
  152. margin-right: 20rpx;
  153. display: block;
  154. }
  155. .upload-btn {
  156. width: 120rpx;
  157. height: 120rpx;
  158. text-align: center;
  159. line-height: 120rpx;
  160. background-color: #f4f5f6;
  161. border-radius: 8rpx;
  162. color: #606266;
  163. display: inline-block;
  164. vertical-align: middle;
  165. .custom-icon-xinzeng {
  166. font-size: 44rpx;
  167. }
  168. }
  169. .up-img-view {
  170. position: relative;
  171. display: inline-block;
  172. vertical-align: middle;
  173. .del-icon {
  174. position: absolute;
  175. color: $uni-color-error;
  176. right: 0;
  177. top: -20upx;
  178. font-size: 40rpx;
  179. background-color: #ffffff;
  180. width: 40rpx;
  181. height: 40rpx;
  182. border-radius: 100%;
  183. line-height: 40rpx;
  184. }
  185. }
  186. }
  187. </style>