You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
6.9 KiB

4 years ago
  1. <template>
  2. <view class="upload-content">
  3. <view
  4. class="upload-item"
  5. v-for="(item, index) in imageList"
  6. :key="index"
  7. >
  8. <image class="upload-img" :src="item.filePath" mode="aspectFill" @click="previewImage(index)"></image>
  9. <!-- 删除按钮 -->
  10. <image class="upload-del-btn"
  11. @click.stop.prevent="showDelModal(index)"
  12. src="data:image/jpg;base64,/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAAA8AAD/4QMraHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI/PiA8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA1LjMtYzAxMSA2Ni4xNDU2NjEsIDIwMTIvMDIvMDYtMTQ6NTY6MjcgICAgICAgICI+IDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+IDxyZGY6RGVzY3JpcHRpb24gcmRmOmFib3V0PSIiIHhtbG5zOnhtcD0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLyIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bXA6Q3JlYXRvclRvb2w9IkFkb2JlIFBob3Rvc2hvcCBDUzYgKFdpbmRvd3MpIiB4bXBNTTpJbnN0YW5jZUlEPSJ4bXAuaWlkOkZGNjhCRTZFMzgwRTExRUFCNkUwOEM2Q0Y3RjUzQTE2Ii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YVj1vhzbguTnUlaXp0FlSVRo0ZSRJCxLdU3bSHdx48CIRDR9f8ANZGO59DhqcELbe1phSmj9NRYkwvzS4JH3COgB5BX69cuY/v5p+qR++ord2+Og82Vu7uXeGizA8N1F5HJBHOrVP0bNvEz0FZpVS2NuLtCvm1VoEEqECMv2H+FXZ+qeoqTr91rrU3rdbq9ZfPcwdWqfq3n++qjuPcx1+2ct/sc2xR7ffUTt7OPboHf2AwqRjufzJiWxC23tapsV0vTUWJvo80uCZdwhoM3x58h2DCrBcZkO3LkZzIWLMCe8CFRo0ZSANSQPdU3GYFx4cSMBAU5e7u5dxvK5jw3UXuYGbvGrVPdv39pnoGGffIVmzOwW6TPtymc3irBmbc2QQmPJipQIApxIDuq7i/iQAfEiSAbvr/hUjIs/hzFNiNtsi0zZTpemogTYR5qcAy7gHQVTmuFWPMbG5Z7w2KmVCC2XkCTjTge1xtXEhA/IQ1FStmv1/z/AB2Q4qHDXe7aAjSlQkitzb+bAG4ke8jDx1UAwx6/i/y4W2VzH+Gg5v7S9u09A8wr6/5/kUhtUyGuyW0RCrKmpFDm38GBJxQ9xkHjoKpwrCrHh1jbs9nbFLKRFbzyxNx1wfc44rgYiXkAaiv/2Q=="
  13. mode="scaleToFill">
  14. </image>
  15. <!-- 进度 -->
  16. <view class="upload-progress" v-if="item.progress < 100">{{item.progress}}%</view>
  17. </view>
  18. <!-- 新增按钮 -->
  19. <view class="upload-add-btn" v-if="rduLength > 0" @click="chooseImage"></view>
  20. <mix-modal ref="mixModal" title="提示" text="确定要放弃这张图片么" @onConfirm="delImage"></mix-modal>
  21. </view>
  22. </template>
  23. <script>
  24. export default {
  25. data() {
  26. return {
  27. imageList: []
  28. };
  29. },
  30. props: {
  31. count: {
  32. type: Number,
  33. default: 5 //单次可选择的图片数量
  34. },
  35. length: {
  36. type: Number,
  37. default: 1 //可上传总数量
  38. },
  39. index: {
  40. type: Number,
  41. default: 0
  42. }
  43. },
  44. computed: {
  45. rduLength(){
  46. return this.length - this.imageList.length;
  47. }
  48. },
  49. methods: {
  50. //选择图片
  51. chooseImage(){
  52. uni.chooseImage({
  53. count: this.rduLength < this.count ? this.rduLength : this.count, //最多可以选择的图片张数,默认9
  54. sizeType: ['original', 'compressed'], //original 原图,compressed 压缩图,默认二者都有
  55. sourceType: ['album', 'camera'], //album 从相册选图,camera 使用相机,默认二者都有
  56. success: res=> {
  57. this.uploadFiles(res.tempFilePaths);
  58. }
  59. });
  60. },
  61. //上传图片
  62. async uploadFiles(files){
  63. const item = {
  64. filePath: files[0],
  65. progress: 0
  66. }
  67. this.imageList.push(item);
  68. const result = await uniCloud.uploadFile({
  69. filePath: item.filePath,
  70. cloudPath: + new Date() + ('000000' + Math.floor(Math.random() * 999999)).slice(-6) + '.jpg',
  71. onUploadProgress: e=> {
  72. item.progress = Math.round(
  73. (e.loaded * 100) / e.total
  74. )
  75. }
  76. });
  77. if(!result.fileID){
  78. this.$util.msg('图片上传失败,上传任务已终止');
  79. this.imageList.pop();
  80. return;
  81. }
  82. const tempFiles = await uniCloud.getTempFileURL({
  83. fileList: [result.fileID]
  84. })
  85. const tempFile = tempFiles.fileList[0];
  86. if(tempFile.download_url || tempFile.fileID){
  87. item.url = tempFile.download_url || tempFile.fileID;
  88. this.$emit('onChange', {
  89. list: this.imageList,
  90. index: this.index
  91. })
  92. }else{
  93. this.$util.msg('图片上传失败,上传任务已终止');
  94. this.imageList.pop();
  95. }
  96. files.shift();
  97. if(files.length > 0){
  98. this.uploadFiles(files);
  99. }
  100. },
  101. //删除图片
  102. showDelModal(index){
  103. this.curIndex = index;
  104. this.$refs.mixModal.open();
  105. },
  106. delImage(index){
  107. this.imageList.splice(this.curIndex, 1);
  108. this.$emit('onChange', {
  109. list: this.imageList,
  110. index: this.index
  111. })
  112. },
  113. //预览图片
  114. previewImage(index){
  115. const urls = this.imageList.map(item=> item.filePath);
  116. uni.previewImage({
  117. current: index,
  118. urls: urls
  119. })
  120. }
  121. }
  122. }
  123. </script>
  124. <style lang="scss">
  125. .upload-content{
  126. display: flex;
  127. flex-wrap: wrap;
  128. background-color: #fff;
  129. }
  130. .upload-item{
  131. position: relative;
  132. width:148rpx;
  133. height:148rpx;
  134. margin-right:28rpx;
  135. margin-bottom:24rpx;
  136. &:nth-child(4n){
  137. margin-right:0;
  138. }
  139. .upload-img{
  140. width:100%;
  141. height:100%;
  142. border-radius:8rpx;
  143. }
  144. .upload-del-btn{
  145. position: absolute;
  146. right:-16rpx;
  147. top:-14rpx;
  148. z-index: 5;
  149. width:36rpx;
  150. height:36rpx;
  151. border: 4rpx solid #fff;
  152. border-radius: 100px;
  153. }
  154. .upload-progress{
  155. position: absolute;
  156. left:0;
  157. top:0;
  158. display:flex;
  159. align-items:center;
  160. justify-content: center;
  161. width:100%;
  162. height:100%;
  163. background-color: rgba(0,0,0,.4);
  164. color:#fff;
  165. font-size:24rpx;
  166. border-radius:8rpx;
  167. }
  168. }
  169. .upload-add-btn {
  170. position: relative;
  171. float:left;
  172. width: 148rpx;
  173. height: 148rpx;
  174. margin-bottom: 24rpx;
  175. z-index: 85;
  176. border-radius:8rpx;
  177. background:#f7f7f7;
  178. &:before,
  179. &:after {
  180. content: " ";
  181. position: absolute;
  182. top: 50%;
  183. left: 50%;
  184. -webkit-transform: translate(-50%, -50%);
  185. transform: translate(-50%, -50%);
  186. width: 4rpx;
  187. height: 60rpx;
  188. background-color: #d6d6d6;
  189. }
  190. &:after {
  191. width: 60rpx;
  192. height: 4rpx;
  193. }
  194. }
  195. </style>