index.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.picUrl.substring(item.picUrl.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. debugger
  107. // 判断是否json字符串,将其转为json格式
  108. let data = JSON.parse(res.data);
  109. if (![200].includes(data.code)) {
  110. _this.$modal.msg(data.msg)
  111. } else {
  112. if (_this.progress === 100) {
  113. _this.imgArr.push(data.fileName)
  114. _this.$modal.msg('上传成功!')
  115. _this.photo = false;
  116. _this.$emit('updateImg', _this.imgArr);
  117. }
  118. }
  119. },
  120. fail: e => {
  121. console.log(e)
  122. _this.$modal.msg('上传失败!')
  123. },
  124. complete: res => {
  125. uni.hideLoading();
  126. _this.uploading = false;
  127. }
  128. });
  129. task.onProgressUpdate(res => {
  130. debugger
  131. _this.progress = res.progress;
  132. uni.showLoading({
  133. title: '上传中'
  134. })
  135. if (_this.progress != 100) {
  136. _this.loading = false
  137. } else {
  138. _this.loading = true
  139. }
  140. });
  141. })
  142. },
  143. })
  144. },
  145. choosevideo() {
  146. let _this = this;
  147. uni.chooseVideo({
  148. sourceType: ['album', 'camera'],
  149. maxDuration: 30,
  150. success(resp) {
  151. console.log('视频')
  152. if (resp.size / 1024 / 1024 > _this.fileSize) {
  153. uni.showToast({
  154. title: '上传视频大小不能超过 '+_this.fileSize+' MB!',
  155. icon: 'none'
  156. })
  157. return;
  158. }
  159. const task = uni.uploadFile({
  160. url: _this.$HTTP + `/common/upload`,
  161. filePath: resp.tempFilePath,
  162. name: 'file',
  163. formData: {},
  164. header: _this.headers,
  165. success: res => {
  166. // 判断是否json字符串,将其转为json格式
  167. let data = JSON.parse(res.data);
  168. if (![200].includes(res.statusCode)) {
  169. this.uploadError(index, data);
  170. } else {
  171. //上传成功
  172. if (_this.progress === 100) {
  173. _this.imgArr.push(data.fileName)
  174. _this.$modal.msg('上传成功!')
  175. _this.photo = false;
  176. _this.$emit('updateImg', _this.imgArr);
  177. }
  178. }
  179. },
  180. fail: e => {
  181. _this.$modal.msg('上传失败!')
  182. this.uploadError(index, e);
  183. },
  184. complete: res => {
  185. uni.hideLoading();
  186. _this.uploading = false;
  187. }
  188. });
  189. task.onProgressUpdate(res => {
  190. _this.progress = res.progress;
  191. uni.showLoading({
  192. title: '上传中'
  193. })
  194. if (_this.progress != 100) {
  195. _this.loading = false
  196. } else {
  197. _this.loading = true
  198. }
  199. });
  200. },
  201. })
  202. },
  203. //查看图片
  204. showPhoto(index) {
  205. uni.previewImage({
  206. current: index,
  207. urls: this.imgArr,
  208. })
  209. },
  210. //删除图片
  211. remove(index) {
  212. uni.showModal({
  213. title: '提示',
  214. content: '是否删除该图片或视频?',
  215. success: (res) => {
  216. if (res.confirm) {
  217. this.imgArr.splice(index, 1);
  218. this.$emit('updateImg', this.imgArr);
  219. }
  220. }
  221. })
  222. },
  223. }
  224. }
  225. </script>
  226. <style>
  227. </style>