uploadProject.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <template>
  2. <view>
  3. <image src="/static/icon/background.png" mode="" class="background"></image>
  4. <view style="padding-top: 100rpx;">
  5. <view class="align-items">
  6. <back></back>
  7. </view>
  8. <view class="project-content">
  9. <view class="font-forty-eight blue">
  10. 项目名称:{{name}}
  11. </view>
  12. <view class="font-forty-eight blue">
  13. 项目地址:{{address}}
  14. </view>
  15. </view>
  16. <view v-if="photo != null && photo.length >= 1">
  17. <view class="flex" v-for="(item,index) in photo" :key="index">
  18. <image :src="item" mode="" class="project-img" @click="showPhotos(index)" v-if="item.substring(item.length - 3) == 'jpg'"></image>
  19. <image :src="item" mode="" class="project-img" @click="showPhotos(index)" v-else-if="item.substring(item.length - 3) == 'JPG'"></image>
  20. <image :src="item" mode="" class="project-img" @click="showPhotos(index)" v-else-if="item.substring(item.length - 3) == 'png'"></image>
  21. <image :src="item" mode="" class="project-img" @click="showPhotos(index)" v-else-if="item.substring(item.length - 3) == 'PNG'"></image>
  22. <image :src="item" mode="" class="project-img" @click="showPhotos(index)" v-else-if="item.substring(item.length - 4) == 'jpeg'"></image>
  23. <image :src="item" mode="" class="project-img" @click="showPhotos(index)" v-else-if="item.substring(item.length - 4) == 'JPEG'"></image>
  24. <video :src="item" controls v-else class="project-img"></video>
  25. </view>
  26. </view>
  27. <view class="" style="display: flex; flex-wrap: wrap; margin: 0 55rpx;">
  28. <image src="/static/icon/chooseimg.png" mode="" style="width: 190rpx; height: 190rpx; margin: 0 12rpx; "
  29. @click="choose()"></image>
  30. <view v-for="(item,index) in imgymxs" :key="index" style="position: relative;">
  31. <view v-if="item.type == 'image'">
  32. <image :src="item.url" mode=""
  33. style="width: 190rpx; height: 190rpx; margin: 0 20rpx;" @click="showPhoto(index)">
  34. </image>
  35. </view>
  36. <view v-else>
  37. <video :src="item"
  38. style="width: 190rpx; height: 190rpx; margin: 0 20rpx;"></video>
  39. </view>
  40. <view @click="remove(index)" style="position: absolute; top: 0; right: 14rpx; border-radius: 50%; background-color: #FF0000;">
  41. <u-icon name="close" color="#FFFFFF" size="35" ></u-icon>
  42. </view>
  43. </view>
  44. </view>
  45. <view class="project background-color1 font-sixty-four white" @click="updatePhoto()">
  46. 保存数据
  47. </view>
  48. </view>
  49. </view>
  50. </template>
  51. <script>
  52. import service from '@/api/index.js'
  53. export default {
  54. data() {
  55. return {
  56. action: this.$HTTP.webUrl + `/obs`,
  57. headers: {
  58. MAuthorization: "wxBearer " + uni.getStorageSync('token')
  59. },
  60. url: [],
  61. id: 0,
  62. name: '',
  63. address: '',
  64. photo: [],
  65. uploading: false,
  66. imgArr: [],
  67. imgymxs: [],
  68. progress: 0 ,//图片或视频上传百分比
  69. }
  70. },
  71. onLoad(e) {
  72. console.log(e)
  73. this.id = e.id;
  74. this.name = e.name;
  75. this.address = e.address
  76. this.getEngineDetail();
  77. },
  78. methods: {
  79. updatePhoto() {
  80. if (this.imgArr.length > 0) {
  81. service.updateEnginePhoto({
  82. photo: this.imgArr,
  83. id: this.id
  84. }).then(res => {
  85. this.$UTILS.showPrompt('提交成功!')
  86. setTimeout(function() {
  87. uni.navigateTo({
  88. url: '/pages/noLogin/typeList'
  89. })
  90. }, 1000)
  91. }).catch(e => {
  92. this.$UTILS.showPrompt('提交失败!')
  93. })
  94. } else {
  95. this.$UTILS.showPrompt('请先选择照片!')
  96. }
  97. },
  98. showPhoto(index){
  99. uni.previewImage({
  100. current:index,
  101. urls:this.imgArr,
  102. })
  103. },
  104. showPhotos(index){
  105. uni.previewImage({
  106. current:index,
  107. urls:this.photo,
  108. })
  109. },
  110. getEngineDetail() {
  111. service.getEngineDetail({
  112. id: this.id
  113. }).then(res => {
  114. this.photo = res.photo
  115. })
  116. },
  117. choose() {
  118. let _this = this;
  119. uni.showActionSheet({
  120. title: '上传',
  121. itemList: ['图片', '视频'],
  122. success: (res) => {
  123. // console.log(res)
  124. if (res.tapIndex == 0) {
  125. this.chooseimage()
  126. } else {
  127. this.choosevideo()
  128. }
  129. }
  130. })
  131. },
  132. chooseimage() {
  133. console.log('图片')
  134. let _this = this;
  135. uni.chooseImage({
  136. sizeType: ['album', 'camera'],
  137. success(resp) {
  138. console.log('res--uni.chooseMedia', resp);
  139. resp.tempFiles.forEach((item, index) => {
  140. const task = uni.uploadFile({
  141. url: _this.$HTTP.webUrl + `/obs`,
  142. filePath: item.path,
  143. name: 'file',
  144. formData: {},
  145. header: _this.headers,
  146. success: res => {
  147. // 判断是否json字符串,将其转为json格式
  148. let data = _this.$u.test.jsonString(res
  149. .data) ? JSON.parse(res.data) : res.data;
  150. if (![200, 201, 204].includes(res.statusCode)) {
  151. // this.uploadError(index, data);
  152. _this.$UTILS.showPrompt('选取失败!')
  153. } else {
  154. // 上传成功
  155. // this.lists[index].response = data;
  156. // this.lists[index].progress = 100;
  157. // this.lists[index].error = false;
  158. // this.$emit('on-success', data, index, this.lists, this
  159. // .index);
  160. if (_this.progress === 100) {
  161. // console.log('_this.progress', _this.progress)
  162. // console.log('data----', data)
  163. // console.log('res--', res)
  164. _this.imgymxs.push({url:data.data.url,type:'image'})
  165. _this.imgArr.push(data.data.url)
  166. // console.log('imgArr', _this.imgArr)
  167. _this.$UTILS.showPrompt('选取成功!')
  168. }
  169. }
  170. },
  171. fail: e => {
  172. _this.$UTILS.showPrompt('选取失败!')
  173. this.uploadError(index, e);
  174. },
  175. complete: res => {
  176. _this.uploading = false;
  177. // _this.uploadFile(index + 1);
  178. // this.$emit('on-change', res, index, this.lists, this
  179. // .index);
  180. }
  181. });
  182. task.onProgressUpdate(res => {
  183. // if (res.progress > 0) {
  184. // this.lists[index].progress = res.progress;
  185. // this.$emit('on-progress', res, index, this.lists, this.index);
  186. // }
  187. _this.progress = res.progress;
  188. console.log('onProgressUpdate', res)
  189. uni.showLoading({
  190. title: '选取中'
  191. })
  192. });
  193. })
  194. },
  195. })
  196. },
  197. choosevideo() {
  198. let _this = this;
  199. console.log('视频')
  200. uni.chooseVideo({
  201. sourceType: ['album','camera'],
  202. maxDuration: 30,
  203. success(resp) {
  204. const task = uni.uploadFile({
  205. url: _this.$HTTP.webUrl + `/obs`,
  206. filePath: resp.tempFilePath,
  207. name: 'file',
  208. formData: {},
  209. header: _this.headers,
  210. success: res => {
  211. // 判断是否json字符串,将其转为json格式
  212. let data = _this.$u.test.jsonString(res
  213. .data) ? JSON.parse(res.data) : res.data;
  214. if (![200, 201, 204].includes(res.statusCode)) {
  215. this.uploadError(index, data);
  216. } else {
  217. // 上传成功
  218. // this.lists[index].response = data;
  219. // this.lists[index].progress = 100;
  220. // this.lists[index].error = false;
  221. // this.$emit('on-success', data, index, this.lists, this
  222. // .index);
  223. if (_this.progress === 100) {
  224. console.log('_this.progress', _this.progress)
  225. console.log('data----', data)
  226. console.log('res--', res)
  227. // _this.imgArr.push(data.data.url)
  228. _this.imgymxs.push({url:data.data.url,type:'video'})
  229. _this.imgArr.push(data.data.url)
  230. console.log('imgArr', _this.imgArr)
  231. _this.$UTILS.showPrompt('选取成功!')
  232. }
  233. }
  234. },
  235. fail: e => {
  236. _this.$UTILS.showPrompt('选取失败!')
  237. this.uploadError(index, e);
  238. },
  239. complete: res => {
  240. uni.hideLoading();
  241. _this.uploading = false;
  242. // _this.uploadFile(index + 1);
  243. // this.$emit('on-change', res, index, this.lists, this
  244. // .index);
  245. }
  246. });
  247. task.onProgressUpdate(res => {
  248. // if (res.progress > 0) {
  249. // this.lists[index].progress = res.progress;
  250. // this.$emit('on-progress', res, index, this.lists, this.index);
  251. // }
  252. _this.progress = res.progress;
  253. console.log('onProgressUpdate', res)
  254. uni.showLoading({
  255. title: '选取中'
  256. })
  257. });
  258. },
  259. })
  260. },
  261. remove(index) {
  262. uni.showModal({
  263. title: '提示',
  264. content: '是否删除该图片或视频?',
  265. success: (res) => {
  266. if (res.confirm) {
  267. this.imgArr.splice(index, 1);
  268. this.imgymxs.splice(index, 1);
  269. console.log('this.imgarr',this.imgArr)
  270. }
  271. }
  272. })
  273. },
  274. }
  275. }
  276. </script>
  277. <style lang="scss" scoped>
  278. .background {
  279. z-index: -1;
  280. position: fixed;
  281. width: 100%;
  282. height: 100%;
  283. background-size: 100% 100%;
  284. }
  285. .project-content {
  286. border: 4rpx solid #3857F3;
  287. margin: 80rpx 55rpx 30rpx;
  288. padding: 30rpx 20rpx;
  289. border-radius: 48rpx;
  290. }
  291. .project {
  292. border-radius: 72rpx;
  293. padding: 25rpx 0;
  294. text-align: center;
  295. margin: 60rpx 55rpx 0;
  296. }
  297. .project-img {
  298. width: 300rpx;
  299. height: 168rpx;
  300. margin: 20rpx auto;
  301. }
  302. </style>