index.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 fileCount=resp.tempFiles.length+_this.imgArr.length
  84. if ( fileCount> _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. let fileCount=1 +_this.imgArr.length
  151. if ( fileCount> _this.limit) {
  152. uni.showToast({
  153. title: '上传文件数量不能超过 '+_this.limit+' 个!',
  154. icon: 'none'
  155. })
  156. return;
  157. }
  158. if (resp.size / 1024 / 1024 > _this.fileSize) {
  159. uni.showToast({
  160. title: '上传视频大小不能超过 '+_this.fileSize+' MB!',
  161. icon: 'none'
  162. })
  163. return;
  164. }
  165. const task = uni.uploadFile({
  166. url: _this.$HTTP + `/common/upload`,
  167. filePath: resp.tempFilePath,
  168. name: 'file',
  169. formData: {},
  170. header: _this.headers,
  171. success: res => {
  172. // 判断是否json字符串,将其转为json格式
  173. let data = JSON.parse(res.data);
  174. if (![200].includes(res.statusCode)) {
  175. this.uploadError(index, data);
  176. } else {
  177. //上传成功
  178. if (_this.progress === 100) {
  179. _this.imgArr.push(data.fileName)
  180. _this.$modal.msg('上传成功!')
  181. _this.photo = false;
  182. _this.$emit('updateImg', _this.imgArr);
  183. }
  184. }
  185. },
  186. fail: e => {
  187. _this.$modal.msg('上传失败!')
  188. this.uploadError(index, e);
  189. },
  190. complete: res => {
  191. uni.hideLoading();
  192. _this.uploading = false;
  193. }
  194. });
  195. task.onProgressUpdate(res => {
  196. _this.progress = res.progress;
  197. uni.showLoading({
  198. title: '上传中'
  199. })
  200. if (_this.progress != 100) {
  201. _this.loading = false
  202. } else {
  203. _this.loading = true
  204. }
  205. });
  206. },
  207. })
  208. },
  209. //查看图片
  210. showPhoto(index) {
  211. uni.previewImage({
  212. current: index,
  213. urls: this.imgArr,
  214. })
  215. },
  216. //删除图片
  217. remove(index) {
  218. uni.showModal({
  219. title: '提示',
  220. content: '是否删除该图片或视频?',
  221. success: (res) => {
  222. if (res.confirm) {
  223. this.imgArr.splice(index, 1);
  224. this.$emit('updateImg', this.imgArr);
  225. }
  226. }
  227. })
  228. },
  229. }
  230. }
  231. </script>
  232. <style>
  233. </style>