PhotoBtn.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <view class="gzms-wrap">
  3. <view class="photo_btn" @click="chooseImage">拍照</view>
  4. <view class="photo_list">
  5. <PreviewPhoto type="add" @imagedelete="imagedelete" :imageList="imageList" />
  6. </view>
  7. </view>
  8. </template>
  9. <script>
  10. import PreviewPhoto from '@/pages/maintenance/check/components/PreviewPhoto.vue'
  11. export default {
  12. components: {
  13. PreviewPhoto
  14. },
  15. props: {
  16. imageUrl: {
  17. type: Array,
  18. default: () => []
  19. }
  20. },
  21. watch: {
  22. imageUrl: {
  23. handler(newVal) {
  24. let arr = JSON.parse(JSON.stringify(newVal));
  25. this.imageList = arr;
  26. },
  27. deep: true,
  28. immediate: true
  29. }
  30. },
  31. data() {
  32. return {
  33. imageList: [],
  34. }
  35. },
  36. methods: {
  37. imagedelete(index) {
  38. this.imageList.splice(index, 1);
  39. },
  40. getImageData() {
  41. return this.imageList || [];
  42. },
  43. // *** 新增拍照
  44. chooseImage() {
  45. const _this = this
  46. if (_this.imageList.length >= 9) {
  47. _this.$refs.uToast.show({
  48. type: "warning",
  49. message: "最多上传9张照片",
  50. })
  51. return
  52. }
  53. uni.chooseImage({
  54. count: 9, //默认9
  55. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  56. sourceType: ['camera'], //从相册选择
  57. success: function(res) {
  58. uni.showLoading({
  59. title: '上传中'
  60. })
  61. _this.uploadFile(res.tempFilePaths).then(res => {
  62. res.forEach(item => {
  63. let fileNames = item.storePath.split('/')
  64. let url = _this.apiUrl +
  65. '/main/file/getFile?objectName=' + item.storePath +
  66. '&fullfilename=' + fileNames[fileNames.length -
  67. 1]
  68. let images = _this.imageList;
  69. // 不能存储超过 9 张
  70. if (images.length < 9) {
  71. _this.imageList.push(url);
  72. }
  73. })
  74. uni.hideLoading();
  75. }).catch((error) => {
  76. console.log(error, 'errorerrorerror')
  77. uni.hideLoading();
  78. })
  79. }
  80. });
  81. },
  82. uploadFile(list) {
  83. let PromiseAll = []
  84. const apiUrl = this.apiUrl
  85. const token = uni.getStorageSync("token"); //取存本地的token
  86. list.forEach(item => {
  87. PromiseAll.push(
  88. new Promise((resolve, reject) => {
  89. uni.uploadFile({
  90. url: apiUrl + '/main/file/upload',
  91. filePath: item,
  92. name: 'multiPartFile',
  93. header: {
  94. authorization: token
  95. },
  96. success: (uploadFileRes) => {
  97. if (uploadFileRes.statusCode == 500) {
  98. uni.hideLoading();
  99. return;
  100. }
  101. let data = JSON.parse(uploadFileRes.data)
  102. resolve(data.data)
  103. },
  104. fail: (err) => {
  105. console.log(err, '123456');
  106. // 上传请求本身失败(如网络问题等),直接 reject
  107. uni.hideLoading();
  108. reject(err)
  109. }
  110. });
  111. }),
  112. )
  113. })
  114. return Promise.all(PromiseAll)
  115. },
  116. }
  117. }
  118. </script>
  119. <style scoped lang="scss">
  120. .photo_btn {
  121. background-color: #157a2c;
  122. width: 140rpx;
  123. height: 60rpx;
  124. line-height: 60rpx;
  125. font-size: 24rpx;
  126. color: #fff;
  127. text-align: center;
  128. border-radius: 8rpx;
  129. margin-bottom: 12rpx;
  130. display: inline-block;
  131. }
  132. </style>