index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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="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="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. // imgArr: [],
  36. }
  37. },
  38. props:{
  39. imgArr:{
  40. type: Array,
  41. default: () => []
  42. },
  43. },
  44. methods: {
  45. //上传图片
  46. choose() {
  47. let _this = this;
  48. uni.showActionSheet({
  49. title: '上传',
  50. itemList: ['图片', '视频'],
  51. success: (res) => {
  52. if (res.tapIndex == 0) {
  53. this.chooseimage()
  54. } else {
  55. this.choosevideo()
  56. }
  57. }
  58. })
  59. },
  60. chooseimage() {
  61. let _this = this;
  62. uni.chooseImage({
  63. sizeType: ['album', 'camera'],
  64. success(resp) {
  65. resp.tempFiles.forEach((item, index) => {
  66. const task = uni.uploadFile({
  67. url: _this.$HTTP + `/common/upload`,
  68. filePath: item.path,
  69. name: 'file',
  70. formData: {},
  71. header: _this.headers,
  72. success: res => {
  73. // 判断是否json字符串,将其转为json格式
  74. let data = JSON.parse(res.data);
  75. if (![200].includes(data.code)) {
  76. _this.$modal.msg(data.msg)
  77. } else {
  78. if (_this.progress === 100) {
  79. _this.imgArr.push(data.data.url)
  80. _this.$modal.msg('上传成功!')
  81. _this.photo = false;
  82. this.$emit('updateImg',_this.imgArr);
  83. }
  84. }
  85. },
  86. fail: e => {
  87. console.log(e)
  88. _this.$modal.msg('上传失败!')
  89. },
  90. complete: res => {
  91. uni.hideLoading();
  92. _this.uploading = false;
  93. }
  94. });
  95. task.onProgressUpdate(res => {
  96. _this.progress = res.progress;
  97. uni.showLoading({
  98. title: '上传中'
  99. })
  100. if (_this.progress != 100) {
  101. _this.loading = false
  102. } else {
  103. _this.loading = true
  104. }
  105. });
  106. })
  107. },
  108. })
  109. },
  110. choosevideo() {
  111. let _this = this;
  112. console.log('视频')
  113. uni.chooseVideo({
  114. sourceType: ['album', 'camera'],
  115. maxDuration: 30,
  116. success(resp) {
  117. const task = uni.uploadFile({
  118. url: _this.$HTTP + `/common/upload`,
  119. filePath: resp.tempFilePath,
  120. name: 'file',
  121. formData: {},
  122. header: _this.headers,
  123. success: res => {
  124. // 判断是否json字符串,将其转为json格式
  125. let data = JSON.parse(res.data);
  126. if (![200].includes(res.statusCode)) {
  127. this.uploadError(index, data);
  128. } else {
  129. //上传成功
  130. if (_this.progress === 100) {
  131. _this.imgArr.push(data.data.url)
  132. _this.$modal.msg('上传成功!')
  133. _this.photo = false;
  134. this.$emit('updateImg',_this.imgArr);
  135. }
  136. }
  137. },
  138. fail: e => {
  139. _this.$modal.msg('上传失败!')
  140. this.uploadError(index, e);
  141. },
  142. complete: res => {
  143. uni.hideLoading();
  144. _this.uploading = false;
  145. }
  146. });
  147. task.onProgressUpdate(res => {
  148. _this.progress = res.progress;
  149. uni.showLoading({
  150. title: '上传中'
  151. })
  152. if (_this.progress != 100) {
  153. _this.loading = false
  154. } else {
  155. _this.loading = true
  156. }
  157. });
  158. },
  159. })
  160. },
  161. //查看图片
  162. showPhoto(index) {
  163. uni.previewImage({
  164. current: index,
  165. urls: this.imgArr,
  166. })
  167. },
  168. //删除图片
  169. remove(index) {
  170. uni.showModal({
  171. title: '提示',
  172. content: '是否删除该图片或视频?',
  173. success: (res) => {
  174. if (res.confirm) {
  175. this.imgArr.splice(index, 1);
  176. this.$emit('updateImg',this.imgArr);
  177. }
  178. }
  179. })
  180. },
  181. }
  182. }
  183. </script>
  184. <style>
  185. </style>