QiniuUpload.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <div style="display: inline-block;">
  3. <view class="bank-license" v-for="item,index in Fimages" :key='index'>
  4. <block v-if="item">
  5. <img class="license-pic" :src="item" alt="" />
  6. <u-icon v-if="!flag" name="close" class='del-icon' size='30' @click.stop="handleRemove(item, index)">
  7. </u-icon>
  8. </block>
  9. </view>
  10. <view class="bank-license" @click="uploadAva">
  11. <text>+</text>
  12. </view>
  13. <!-- <slot v-show="false" name="cont"><button>上传</button></slot> -->
  14. </div>
  15. </template>
  16. <script>
  17. import webUrl from '@/config.js';
  18. import qiniuUploader from './qiniuUploader.js';
  19. import {
  20. getEnToken
  21. } from '@/access/common.js';
  22. export default {
  23. data() {
  24. return {
  25. Fimages: []
  26. };
  27. },
  28. props: {
  29. images: {
  30. // type: Array,
  31. default: () => {
  32. return [];
  33. }
  34. },
  35. flag: {
  36. type: Boolean,
  37. default: false
  38. },
  39. },
  40. watch: {
  41. images(val) {
  42. this.Fimages = val;
  43. }
  44. },
  45. created() {
  46. this.Fimages = this.images;
  47. },
  48. methods: {
  49. // 上传头像
  50. uploadAva() {
  51. uni.chooseImage({
  52. success: async res => {
  53. const imgPath = res.tempFilePaths[0]; //选择图片的路径
  54. const imgName = imgPath
  55. .split('.')
  56. .slice(-2)
  57. .join('.');
  58. const key = `${getEnToken()}/${imgName}`; //图片和企业token拼接 为自定义文件名
  59. this.$u.api.uploadToken({
  60. bucket: webUrl.QINIU_KEY, //企业token
  61. key: key //图片的路径
  62. }).then(data => {
  63. this.uploadtoken = data.data; //获取上传图片的token
  64. // url: 'https://upload-z2.qiniup.com',
  65. let domain = webUrl.QINIU_UP; //文件上传地址
  66. let token = data.data; //token为七牛云的token一般由后台接口提供
  67. let filePath = imgPath; //为需要上传的文件
  68. uni.showLoading();
  69. qiniuUploader.upload(
  70. filePath,
  71. res => {
  72. uni.hideLoading();
  73. //图片上传完成后返回值
  74. //因为目前只有头像上传,所以压缩图片,限制尺寸为 300 px
  75. const imgData = {
  76. ...res,
  77. key: res.key
  78. };
  79. const uploadPicUrl = `${webUrl.QINIU_URL}/${res.key}`;
  80. this.$emit('uploadSuccess', imgData, uploadPicUrl);
  81. },
  82. error => {
  83. uni.hideLoading();
  84. // resolve(error)
  85. }, {
  86. region: 'SCN', // (必须填写正确)ECN, SCN, NCN, NA, ASG,分别对应七牛的:华东,华南,华北,北美,新加坡 5 个区域
  87. domain: domain, // // bucket 域名,下载资源时用到。如果设置,会在 success callback 的 res 参数加上可以直接使用的 ImageURL 字 段。否则需要自己拼接
  88. key: key, // [非必须]自定义文件 key。如果不设置,默认为使用微信小程序 API 的临时文件名
  89. // 以下方法三选一即可,优先级为:uptoken > uptokenURL > uptokenFunc
  90. uptoken: token // 由其他程序生成七牛 uptoken
  91. },
  92. res => {
  93. //上传进度
  94. uni.hideLoading();
  95. if (res.progress === 100) {
  96. // resolve(keys);
  97. }
  98. }
  99. );
  100. });
  101. }
  102. });
  103. },
  104. //移除图片
  105. handleRemove(url, index) {
  106. this.Fimages.splice(index, 1);
  107. this.$emit('handleRemove', this.Fimages);
  108. }
  109. }
  110. };
  111. </script>
  112. <style scoped lang="scss">
  113. .bank-license {
  114. width: 124upx;
  115. height: 124upx;
  116. border-radius: 8upx;
  117. border: 2upx #999999 dashed;
  118. margin: 32upx 10rpx;
  119. background: #eee;
  120. text-align: center;
  121. line-height: 100upx;
  122. color: #666;
  123. font-size: 100rpx;
  124. font-weight: 300;
  125. display: inline-block;
  126. vertical-align: middle;
  127. position: relative;
  128. .license-pic {
  129. width: 120upx;
  130. height: 120upx;
  131. display: block;
  132. }
  133. .del-icon {
  134. box-sizing: border-box;
  135. padding-left: 7rpx;
  136. position: absolute;
  137. color: #fff;
  138. right: -12rpx;
  139. top: -20upx;
  140. background-color: #FD463E;
  141. width: 40rpx;
  142. height: 40rpx;
  143. border-radius: 100%;
  144. line-height: 40rpx;
  145. }
  146. }
  147. </style>