index.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. <view v-for="(item,index) in imgArr" :key="index" style="position: relative;">
  6. <view
  7. v-if="item.substring(item.length - 3) == 'png' || item.substring(item.length - 3) == 'jpg'|| item.substring(item.length-4)=='jpeg' ">
  8. <image :src="loadImgSrcLocalhost(item)" mode="" style="width: 200rpx; height: 150rpx; margin: 0 12rpx;"
  9. @click="showPhoto(index)">
  10. </image>
  11. </view>
  12. <view v-else>
  13. <video :src="loadImgSrcLocalhost(item)" style="width: 100rpx; height: 100rpx; margin: 0 12rpx;"></video>
  14. </view>
  15. <view @click="remove(index)"
  16. style="position: absolute; top: 0; right: 14rpx; border-radius: 50%;">
  17. <image src="../../static/images/upload/icon_close.png" style=" width: 15px; height: 15px;">
  18. </image>
  19. </view>
  20. </view>
  21. <image src="../../static/images/upload/updateimg.png" mode=""
  22. class="uploadPic" @click="choose()"></image>
  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.unshift(data.fileName)
  113. console.log(_this.imgArr)
  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. _this.progress = res.progress;
  131. uni.showLoading({
  132. title: '上传中'
  133. })
  134. if (_this.progress != 100) {
  135. _this.loading = false
  136. } else {
  137. _this.loading = true
  138. }
  139. });
  140. })
  141. },
  142. })
  143. },
  144. choosevideo() {
  145. let _this = this;
  146. uni.chooseVideo({
  147. sourceType: ['album', 'camera'],
  148. maxDuration: 30,
  149. success(resp) {
  150. console.log('视频')
  151. let fileCount=1 +_this.imgArr.length
  152. if ( fileCount> _this.limit) {
  153. uni.showToast({
  154. title: '上传文件数量不能超过 '+_this.limit+' 个!',
  155. icon: 'none'
  156. })
  157. return;
  158. }
  159. if (resp.size / 1024 / 1024 > _this.fileSize) {
  160. uni.showToast({
  161. title: '上传视频大小不能超过 '+_this.fileSize+' MB!',
  162. icon: 'none'
  163. })
  164. return;
  165. }
  166. const task = uni.uploadFile({
  167. url: _this.$HTTP + `/common/upload`,
  168. filePath: resp.tempFilePath,
  169. name: 'file',
  170. formData: {},
  171. header: _this.headers,
  172. success: res => {
  173. // 判断是否json字符串,将其转为json格式
  174. let data = JSON.parse(res.data);
  175. if (![200].includes(res.statusCode)) {
  176. this.uploadError(index, data);
  177. } else {
  178. //上传成功
  179. if (_this.progress === 100) {
  180. _this.imgArr.push(data.fileName)
  181. _this.$modal.msg('上传成功!')
  182. _this.photo = false;
  183. _this.$emit('updateImg', _this.imgArr);
  184. }
  185. }
  186. },
  187. fail: e => {
  188. _this.$modal.msg('上传失败!')
  189. this.uploadError(index, e);
  190. },
  191. complete: res => {
  192. uni.hideLoading();
  193. _this.uploading = false;
  194. }
  195. });
  196. task.onProgressUpdate(res => {
  197. _this.progress = res.progress;
  198. uni.showLoading({
  199. title: '上传中'
  200. })
  201. if (_this.progress != 100) {
  202. _this.loading = false
  203. } else {
  204. _this.loading = true
  205. }
  206. });
  207. },
  208. })
  209. },
  210. //查看图片
  211. showPhoto(index) {
  212. uni.previewImage({
  213. current: index,
  214. urls: this.imgArr,
  215. })
  216. },
  217. //删除图片
  218. remove(index) {
  219. uni.showModal({
  220. title: '提示',
  221. content: '是否删除该图片或视频?',
  222. success: (res) => {
  223. if (res.confirm) {
  224. this.imgArr.splice(index, 1);
  225. this.$emit('updateImg', this.imgArr);
  226. }
  227. }
  228. })
  229. },
  230. }
  231. }
  232. </script>
  233. <style lang="scss">
  234. .uploadPic{
  235. position: relative;
  236. height: 150rpx;
  237. margin: 0 12rpx;
  238. width: 200rpx;
  239. }
  240. .uploadPic::after{
  241. position: absolute;
  242. content:'请上传图片';
  243. bottom: 0;
  244. }
  245. </style>