index.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <template>
  2. <view class="align-items" style="margin-top: 20px;margin-bottom: 20px;">
  3. <view class="container" style="color: #b2b2b2;">*请上传照片</view>
  4. <view class="" style="display: flex; flex-wrap: wrap;margin-top: 10px; margin-left: 10px;">
  5. <image src="../../static/images/upload/updateimg.png" mode=""
  6. style="width: 200rpx; height: 150rpx; margin: 0 12rpx; " @click="choose()"></image>
  7. <view v-for="(item,index) in imgArr" :key="index" style="position: relative;">
  8. <view
  9. v-if="item.substring(item.length - 3) == 'png' || item.substring(item.length - 3) == 'jpg'|| item.substring(item.length-4)=='jpeg' ">
  10. <image :src="loadImgSrcLocalhost(item)" mode="" style="width: 100rpx; height: 100rpx; margin: 0 12rpx;"
  11. @click="showPhoto(index)">
  12. </image>
  13. </view>
  14. <view v-else>
  15. <video :src="loadImgSrcLocalhost(item)" style="width: 100rpx; height: 100rpx; margin: 0 12rpx;"></video>
  16. </view>
  17. <view @click="remove(index)"
  18. style="position: absolute; top: 0; right: 14rpx; border-radius: 50%;">
  19. <image src="../../static/images/upload/icon_close.png" style=" width: 15px; height: 15px;">
  20. </image>
  21. </view>
  22. </view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. import {getToken} from '@/utils/auth'
  28. export default {
  29. data() {
  30. return {
  31. headers: {
  32. Authorization: "Bearer " + getToken()
  33. },
  34. loading: false,
  35. progress:0,
  36. // imgArr: [],
  37. }
  38. },
  39. props: {
  40. imgArr: {
  41. type: Array,
  42. default: () => []
  43. },
  44. // 图片数量限制
  45. limit: {
  46. type: Number,
  47. default: 100,
  48. },
  49. // 大小限制(MB)
  50. fileSize: {
  51. type: Number,
  52. default: 10,
  53. },
  54. // 文件类型, 例如['png', 'jpg', 'jpeg']
  55. fileType: {
  56. type: Array,
  57. default: () => ["png", "jpg", "jpeg"],
  58. },
  59. },
  60. methods: {
  61. //上传图片
  62. choose() {
  63. // if(this.imgArr>limit)
  64. let _this = this;
  65. uni.showActionSheet({
  66. title: '上传',
  67. itemList: ['图片', '视频'],
  68. success: (res) => {
  69. if (res.tapIndex == 0) {
  70. this.chooseimage()
  71. } else {
  72. this.choosevideo()
  73. }
  74. }
  75. })
  76. },
  77. chooseimage() {
  78. let _this = this;
  79. uni.chooseImage({
  80. // count: 1, //最多可以选择的图片张数,默认9
  81. sizeType: ['album', 'camera'],
  82. success(resp) {
  83. let imgCount=resp.tempFiles.length+_this.imgArr.length
  84. if ( imgCount> _this.limit) {
  85. uni.showToast({
  86. title: '上传文件数量不能超过 '+_this.limit+' 个!',
  87. icon: 'none'
  88. })
  89. return;
  90. }
  91. resp.tempFiles.forEach((item, index) => {
  92. if (item.size / 1024 / 1024 > _this.fileSize) {
  93. uni.showToast({
  94. title: '上传头像图片大小不能超过 '+_this.fileSize+' MB!',
  95. icon: 'none'
  96. })
  97. return;
  98. }
  99. const task = uni.uploadFile({
  100. url: _this.$HTTP + `/common/upload`,
  101. filePath: item.path,
  102. name: 'file',
  103. formData: {},
  104. header: _this.headers,
  105. success: res => {
  106. // 判断是否json字符串,将其转为json格式
  107. let data = JSON.parse(res.data);
  108. if (![200].includes(data.code)) {
  109. _this.$modal.msg(data.msg)
  110. } else {
  111. if (_this.progress === 100) {
  112. _this.imgArr.push(data.fileName)
  113. _this.$modal.msg('上传成功!')
  114. _this.photo = false;
  115. _this.$emit('updateImg', _this.imgArr);
  116. }
  117. }
  118. },
  119. fail: e => {
  120. console.log(e)
  121. _this.$modal.msg('上传失败!')
  122. },
  123. complete: res => {
  124. uni.hideLoading();
  125. _this.uploading = false;
  126. }
  127. });
  128. task.onProgressUpdate(res => {
  129. _this.progress = res.progress;
  130. uni.showLoading({
  131. title: '上传中'
  132. })
  133. if (_this.progress != 100) {
  134. _this.loading = false
  135. } else {
  136. _this.loading = true
  137. }
  138. });
  139. })
  140. },
  141. })
  142. },
  143. choosevideo() {
  144. let _this = this;
  145. uni.chooseVideo({
  146. sourceType: ['album', 'camera'],
  147. maxDuration: 30,
  148. success(resp) {
  149. console.log('视频')
  150. if (resp.size / 1024 / 1024 > _this.fileSize) {
  151. uni.showToast({
  152. title: '上传视频大小不能超过 '+_this.fileSize+' MB!',
  153. icon: 'none'
  154. })
  155. return;
  156. }
  157. const task = uni.uploadFile({
  158. url: _this.$HTTP + `/common/upload`,
  159. filePath: resp.tempFilePath,
  160. name: 'file',
  161. formData: {},
  162. header: _this.headers,
  163. success: res => {
  164. // 判断是否json字符串,将其转为json格式
  165. let data = JSON.parse(res.data);
  166. if (![200].includes(res.statusCode)) {
  167. this.uploadError(index, data);
  168. } else {
  169. //上传成功
  170. if (_this.progress === 100) {
  171. _this.imgArr.push(data.fileName)
  172. _this.$modal.msg('上传成功!')
  173. _this.photo = false;
  174. _this.$emit('updateImg', _this.imgArr);
  175. }
  176. }
  177. },
  178. fail: e => {
  179. _this.$modal.msg('上传失败!')
  180. this.uploadError(index, e);
  181. },
  182. complete: res => {
  183. uni.hideLoading();
  184. _this.uploading = false;
  185. }
  186. });
  187. task.onProgressUpdate(res => {
  188. _this.progress = res.progress;
  189. uni.showLoading({
  190. title: '上传中'
  191. })
  192. if (_this.progress != 100) {
  193. _this.loading = false
  194. } else {
  195. _this.loading = true
  196. }
  197. });
  198. },
  199. })
  200. },
  201. //查看图片
  202. showPhoto(index) {
  203. uni.previewImage({
  204. current: index,
  205. urls: this.imgArr,
  206. })
  207. },
  208. //删除图片
  209. remove(index) {
  210. uni.showModal({
  211. title: '提示',
  212. content: '是否删除该图片或视频?',
  213. success: (res) => {
  214. if (res.confirm) {
  215. this.imgArr.splice(index, 1);
  216. this.$emit('updateImg', this.imgArr);
  217. }
  218. }
  219. })
  220. },
  221. }
  222. }
  223. </script>
  224. <style>
  225. </style>