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