uploadFiel.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <view class="container">
  3. <view class="uploadBox">
  4. <view class="uploadItem upload" v-for="item in form" :key="item">
  5. <image :src="item" class="uploadImg"></image>
  6. <image src="../../static/cw2.png" class="imgx" @click="imgx({ url: item })"></image>
  7. </view>
  8. <view class="upload" @click="upload('retrofit_over')" v-if="form.length < 4">
  9. <image src="../../static/xj.png" class="xiangji"></image>
  10. </view>
  11. </view>
  12. <button class="submit" type="primary" @click="submit">提交</button>
  13. </view>
  14. </template>
  15. <script>
  16. import request from '../../api/activity.js';
  17. import { BASE_URL } from '../../env.js';
  18. const token = uni.getStorageSync('token');
  19. export default {
  20. onLoad: function (option) {
  21. this.activityId = option.activityId;
  22. },
  23. data() {
  24. return {
  25. activityId: '',
  26. form: []
  27. }
  28. },
  29. async mounted() {
  30. const res = await request.getPhotos({ activityId: this.activityId });
  31. if (res.errcode) {
  32. wx.showToast({
  33. title: res.details,
  34. icon: 'error'
  35. })
  36. setTimeout(() => {
  37. uni.navigateBack()
  38. }, 1000)
  39. return;
  40. }
  41. const photos = res.data.photos?.map(e => e.photo);
  42. if (photos) this.form = photos;
  43. },
  44. methods: {
  45. async submit() {
  46. if (this.form.length <= 0) {
  47. wx.showToast({
  48. title: '请选择图片',
  49. icon: 'error'
  50. })
  51. return;
  52. };
  53. const res = await request.uploadImgs({ activityId: this.activityId, photos: this.form });
  54. if (res.code == 200) {
  55. wx.showToast({
  56. title: '提交成功'
  57. })
  58. setTimeout(() => {
  59. uni.navigateBack()
  60. }, 1000)
  61. }
  62. },
  63. // 文件上传
  64. upload() {
  65. const token = uni.getStorageSync('token');
  66. uni.chooseImage({
  67. count: 1,
  68. success: async (chooseImageRes) => {
  69. wx.showLoading({
  70. title: '上传中'
  71. })
  72. const tempFilePaths = chooseImageRes.tempFilePaths;
  73. uni.uploadFile({
  74. url: BASE_URL.url + '/activity/photo',
  75. name: 'file',
  76. header: {
  77. 'Authorization': `Bearer ${token}`
  78. },
  79. filePath: tempFilePaths[0],
  80. success: (uploadFileRes) => {
  81. const obj = JSON.parse(uploadFileRes.data);
  82. this.form.push(BASE_URL.fileUrl + obj.imgUrl);
  83. },
  84. fail(e) {
  85. console.log(e)
  86. wx.showToast({
  87. title: '上传失败'
  88. })
  89. },
  90. complete() {
  91. wx.hideLoading();
  92. }
  93. });
  94. }
  95. });
  96. },
  97. // 取消图片上传
  98. imgx({ url }) {
  99. this.form = this.form.filter(e => e !== url);
  100. },
  101. }
  102. }
  103. </script>
  104. <style>
  105. .upload {
  106. width: 75px;
  107. height: 75px;
  108. border-radius: 12px;
  109. border: 1px solid #999;
  110. margin: 10px 0;
  111. margin-right: 5px;
  112. background: #dadada;
  113. }
  114. .xiangji {
  115. display: block;
  116. width: 40px;
  117. height: 40px;
  118. margin: 25px auto;
  119. }
  120. .uploadItem {
  121. display: flex;
  122. position: relative;
  123. }
  124. .uploadImg {
  125. width: 100%;
  126. height: 100%;
  127. display: block;
  128. border-radius: 12px;
  129. overflow: hidden;
  130. }
  131. .uploadBox {
  132. display: flex;
  133. width: 90%;
  134. margin: 5px auto;
  135. flex-wrap: wrap;
  136. }
  137. .imgx {
  138. width: 15px;
  139. height: 15px;
  140. position: absolute;
  141. top: -5px;
  142. right: -5px;
  143. }
  144. .submit {
  145. width: 90%;
  146. margin: 25px auto;
  147. }
  148. </style>