uploadImgVStore.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <template>
  2. <div>
  3. <div class="cameraDiv1">
  4. <div class="imgPre" v-if="imgArr.length">
  5. <van-icon
  6. class="photobrowsing"
  7. name="expand-o"
  8. size="22px"
  9. color="#969696"
  10. @click="deleteImgs" />
  11. <img :src="imgUrlArr[0]" alt="" />
  12. </div>
  13. <p class="coverImg" @click="uploadImg">
  14. <van-icon class="photo ico" name="photograph" size="16px" color="#969696">{{
  15. imgText
  16. }}</van-icon>
  17. </p>
  18. </div>
  19. <p style="text-align: center">{{ imgText }}</p>
  20. </div>
  21. </template>
  22. <script>
  23. import { ImagePreview } from 'vant';
  24. import axios from 'axios';
  25. import { uploadImagev } from '@/api/index';
  26. export default {
  27. name: 'uploadImg',
  28. props: {
  29. uploadid: {
  30. type: String,
  31. default: '',
  32. },
  33. imgText: {
  34. type: String,
  35. default: '',
  36. },
  37. visitsId: {
  38. type: String,
  39. default: '',
  40. },
  41. taskId: {
  42. type: String,
  43. default: '',
  44. },
  45. collectionId: {
  46. type: String,
  47. default: '',
  48. },
  49. type: {
  50. type: Number,
  51. default: 1,
  52. },
  53. imgArr: {
  54. type: [String, Array],
  55. },
  56. count: {
  57. type: Number,
  58. default: 1,
  59. },
  60. },
  61. data() {
  62. return {
  63. shows: false,
  64. url: '',
  65. localIds: [],
  66. num: 0,
  67. serverIdArr: [],
  68. imgUrlArr: [],
  69. };
  70. },
  71. watch: {
  72. imgArr: {
  73. handler(val) {
  74. this.imgUrlArr = val.split(',');
  75. },
  76. immediate: true,
  77. },
  78. },
  79. methods: {
  80. deleteImgs() {
  81. ImagePreview(this.imgUrlArr);
  82. },
  83. uploadImg() {
  84. if (localStorage.getItem('chainName') == null) {
  85. this.$toast('请输入名称!');
  86. return;
  87. }
  88. this.wx.ready(() => {
  89. this.wx.chooseImage({
  90. count: this.count,
  91. sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
  92. sourceType: ['camera'], // 可以指定来源是相册还是相机,默认二者都有
  93. defaultCameraMode: 'normal', //表示进入拍照界面的默认模式,目前有normal与batch两种选择,normal表示普通单拍模式,batch表示连拍模式,不传该参数则为normal模式。从3.0.26版本开始支持front和batch_front两种值,其中front表示默认为前置摄像头单拍模式,batch_front表示默认为前置摄像头连拍模式。(注:用户进入拍照界面仍然可自由切换两种模式)
  94. isSaveToAlbum: 0,
  95. success: (chooseRes) => {
  96. this.localIds = chooseRes.localIds;
  97. console.log('count=' + this.count);
  98. console.log('chooseRes=' + chooseRes);
  99. console.log('localIds=' + this.localIds);
  100. // if (chooseRes.localIds != undefined) {
  101. // localIds = chooseRes.localIds[0];
  102. // } else {
  103. // localIds = chooseRes.localId;
  104. // }
  105. this.num = 0;
  106. this.serverIdArr = [];
  107. this.forUploadImage();
  108. },
  109. });
  110. });
  111. },
  112. forUploadImage() {
  113. this.wx.uploadImage({
  114. localId: this.localIds[this.num],
  115. isShowProgressTips: 1,
  116. success: (uploadRes) => {
  117. this.serverIdArr.push(uploadRes.serverId);
  118. console.log('serverId=' + uploadRes.serverId);
  119. this.num++;
  120. if (this.num == this.localIds.length) {
  121. this.uploadImagev(this.serverIdArr.join(','));
  122. } else {
  123. this.forUploadImage();
  124. }
  125. },
  126. });
  127. },
  128. uploadImagev(meidaId) {
  129. var that = this;
  130. var form = {
  131. mediaId: meidaId,
  132. storeName: localStorage.getItem('chainName'),
  133. locationRemark: localStorage.getItem('locationRemark'),
  134. deptName: localStorage.getItem('deptName'),
  135. };
  136. var loind1 = that.$toast.loading({
  137. duration: 0,
  138. message: '上传中...',
  139. forbidClick: true,
  140. });
  141. uploadImagev(form).then((res) => {
  142. if (res.code == 200) {
  143. // that.imgArr = res.data.url;
  144. let imgArr = [];
  145. res.data.forEach((item) => {
  146. imgArr.push(item.url);
  147. });
  148. loind1.clear();
  149. that.$toast('上传成功!');
  150. that.$emit('newimgarr', { fileUrl: imgArr.join(','), type: that.type });
  151. } else {
  152. that.$toast('上传失败!');
  153. }
  154. });
  155. },
  156. },
  157. };
  158. </script>
  159. <style scoped>
  160. .cameraDiv1 {
  161. position: relative;
  162. height: 164px;
  163. width: 100%;
  164. }
  165. .cameraDiv1 img {
  166. position: absolute;
  167. width: 100%;
  168. display: block;
  169. height: 164px;
  170. top: 0;
  171. }
  172. .imgPre {
  173. height: 164px;
  174. width: 100%;
  175. background-color: white;
  176. border-radius: 6px;
  177. overflow: hidden;
  178. }
  179. .photos1 {
  180. margin: 70px auto;
  181. left: 50%;
  182. margin-left: -14px;
  183. }
  184. .photobrowsing {
  185. position: absolute;
  186. padding: 4px;
  187. right: 0;
  188. top: 0;
  189. z-index: 99;
  190. background-color: rgba(255, 255, 255, 0.8);
  191. border-bottom-left-radius: 3px;
  192. border-top-left-radius: 3px;
  193. }
  194. .coverImg {
  195. text-align: center;
  196. position: absolute;
  197. top: 0;
  198. left: 0;
  199. height: 164px;
  200. width: 100%;
  201. }
  202. .coverImg .ico {
  203. top: 42%;
  204. }
  205. </style>