uploadImgVStore.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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="#1989fa"
  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 class="mask" v-if="progressFlag">
  21. <el-progress
  22. type="circle"
  23. :percentage="percentage"
  24. :show-text="true"
  25. :format="format"></el-progress>
  26. <div class="progressClose" @click="progressClose">取消</div>
  27. </div>
  28. <imageAIVerifyErr
  29. v-if="imageAIVerifyFlag"
  30. :imageAIVerifyFlag="imageAIVerifyFlag"
  31. :imageAIVerifyData="imageAIVerifyData"
  32. @confirmUpload="confirmUpload"
  33. @uploadImgFun="uploadImg"
  34. :source="'newCreated'"
  35. @normalFlow="normalFlow"
  36. @close="close"></imageAIVerifyErr>
  37. </div>
  38. </template>
  39. <script>
  40. import { ImagePreview } from 'vant';
  41. import axios from 'axios';
  42. import { uploadImagev } from '@/api/index';
  43. import imageAIVerifyErr from './imageAIVerifyErr';
  44. export default {
  45. name: 'uploadImg',
  46. components: { imageAIVerifyErr },
  47. props: {
  48. uploadid: {
  49. type: String,
  50. default: '',
  51. },
  52. imgText: {
  53. type: String,
  54. default: '',
  55. },
  56. visitsId: {
  57. type: String,
  58. default: '',
  59. },
  60. taskId: {
  61. type: String,
  62. default: '',
  63. },
  64. collectionId: {
  65. type: String,
  66. default: '',
  67. },
  68. type: {
  69. type: Number,
  70. default: 1,
  71. },
  72. imgArr: {
  73. type: [String, Array],
  74. },
  75. count: {
  76. type: Number,
  77. default: 1,
  78. },
  79. photoIdentifyType: {
  80. // 图匠识别目的(1:店招内容识别,2:门店代码识别,3:调色机识别,4:更换店招)
  81. type: String,
  82. default: '',
  83. },
  84. },
  85. data() {
  86. return {
  87. shows: false,
  88. url: '',
  89. localIds: [],
  90. num: 0,
  91. serverIdArr: [],
  92. imgUrlArr: [],
  93. progressFlag: false,
  94. percentage: 0,
  95. timeFlag: null,
  96. imageAIVerifyFlag: false,
  97. imageAIVerifyData: null, //图匠校验返回的数据
  98. controller: null, //取消请求状态
  99. };
  100. },
  101. watch: {
  102. imgArr: {
  103. handler(val) {
  104. this.imgUrlArr = val.split(',');
  105. },
  106. immediate: true,
  107. },
  108. },
  109. methods: {
  110. deleteImgs() {
  111. ImagePreview(this.imgUrlArr);
  112. },
  113. uploadImg() {
  114. if (localStorage.getItem('chainName') == null) {
  115. this.$toast('请输入名称!');
  116. return;
  117. }
  118. this.wx.ready(() => {
  119. this.wx.chooseImage({
  120. count: this.count,
  121. sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
  122. sourceType: ['camera'], // 可以指定来源是相册还是相机,默认二者都有
  123. defaultCameraMode: 'normal', //表示进入拍照界面的默认模式,目前有normal与batch两种选择,normal表示普通单拍模式,batch表示连拍模式,不传该参数则为normal模式。从3.0.26版本开始支持front和batch_front两种值,其中front表示默认为前置摄像头单拍模式,batch_front表示默认为前置摄像头连拍模式。(注:用户进入拍照界面仍然可自由切换两种模式)
  124. isSaveToAlbum: 0,
  125. success: (chooseRes) => {
  126. this.localIds = chooseRes.localIds;
  127. console.log('count=' + this.count);
  128. console.log('chooseRes=' + chooseRes);
  129. console.log('localIds=' + this.localIds);
  130. // if (chooseRes.localIds != undefined) {
  131. // localIds = chooseRes.localIds[0];
  132. // } else {
  133. // localIds = chooseRes.localId;
  134. // }
  135. this.num = 0;
  136. this.serverIdArr = [];
  137. this.forUploadImage();
  138. },
  139. });
  140. });
  141. },
  142. forUploadImage() {
  143. this.wx.uploadImage({
  144. localId: this.localIds[this.num],
  145. isShowProgressTips: 1,
  146. success: (uploadRes) => {
  147. this.serverIdArr.push(uploadRes.serverId);
  148. console.log('serverId=' + uploadRes.serverId);
  149. this.num++;
  150. if (this.num == this.localIds.length) {
  151. this.uploadImagev(this.serverIdArr.join(','));
  152. } else {
  153. this.forUploadImage();
  154. }
  155. },
  156. });
  157. },
  158. uploadImagev(meidaId) {
  159. // 初始化重置 图匠校验
  160. this.resetProgress();
  161. this.close();
  162. var that = this;
  163. var form = {
  164. mediaId: meidaId,
  165. storeName: localStorage.getItem('chainName'),
  166. locationRemark: localStorage.getItem('locationRemark'),
  167. deptName: localStorage.getItem('deptName'),
  168. };
  169. this.controller = null;
  170. // 需要图匠校验的添加参数和loading
  171. if (this.photoIdentifyType) {
  172. form.photoIdentifyType = this.photoIdentifyType;
  173. this.progress();
  174. this.controller = new AbortController(); //取消请求
  175. } else {
  176. this.toastLoading(0, '上传中...', true);
  177. }
  178. uploadImagev(form, this.controller ? this.controller.signal : null)
  179. .then((res) => {
  180. this.toastLoading().clear();
  181. if (res.code == -1) {
  182. // 图匠图片校验接口超时
  183. this.requestTimeOut(res);
  184. } else if (res.code == 200) {
  185. // 图匠校验结果返回
  186. if (this.photoIdentifyType) {
  187. // 重置loaidng状态
  188. this.resetProgress();
  189. this.imageAIVerifyFlag = true;
  190. console.log('res.data=' + JSON.stringify(res.data));
  191. this.imageAIVerifyData = res.data;
  192. } else {
  193. // 正常流程
  194. this.normalFlow(res);
  195. }
  196. } else {
  197. this.resetProgress();
  198. that.$toast('上传失败!');
  199. }
  200. })
  201. .catch((error) => {
  202. if (error.message === 'canceled') {
  203. this.$toast('取消上传');
  204. console.log('请求被取消:', error.message);
  205. }
  206. this.resetProgress();
  207. });
  208. },
  209. progress() {
  210. // 后端接口20000ms后失效,每1000m progress加10,到90停止;
  211. this.progressFlag = true;
  212. this.percentage = 10;
  213. this.timeFlag = setInterval(() => {
  214. this.percentage = this.percentage + 10;
  215. if (this.percentage == 90) clearInterval(this.timeFlag);
  216. }, 1000);
  217. },
  218. format(percentage) {
  219. return `${percentage} %\n图像识别中`;
  220. },
  221. requestTimeOut(res) {
  222. this.resetProgress();
  223. this.close();
  224. this.$dialog
  225. .confirm({
  226. title: '系统提示',
  227. message: res.msg,
  228. showCancelButton: false,
  229. })
  230. .then(() => {
  231. this.normalFlow(res);
  232. });
  233. },
  234. // 正常流程
  235. normalFlow(res) {
  236. let imgArr = [];
  237. let businessId = [];
  238. res.data.forEach((item) => {
  239. imgArr.push(item.url);
  240. if (item.businessId) businessId.push(item.businessId);
  241. });
  242. this.$toast('上传成功!');
  243. this.$emit('newimgarr', {
  244. fileUrl: imgArr.join(','),
  245. type: this.type,
  246. businessId: businessId.join(','),
  247. });
  248. },
  249. // 重置loaidng状态
  250. resetProgress() {
  251. this.percentage = 100;
  252. clearInterval(this.timeFlag);
  253. this.progressFlag = false;
  254. this.percentage = 0;
  255. },
  256. // 取消图片上传
  257. progressClose() {
  258. this.controller.abort();
  259. },
  260. confirmUpload(res) {
  261. this.normalFlow(res);
  262. },
  263. close() {
  264. this.imageAIVerifyFlag = false;
  265. },
  266. },
  267. };
  268. </script>
  269. <style scoped>
  270. .cameraDiv1 {
  271. position: relative;
  272. height: 164px;
  273. width: 100%;
  274. background-color: white;
  275. }
  276. .cameraDiv1 img {
  277. position: absolute;
  278. width: 100%;
  279. display: block;
  280. height: 164px;
  281. top: 0;
  282. }
  283. .imgPre {
  284. height: 164px;
  285. width: 100%;
  286. background-color: white;
  287. border-radius: 6px;
  288. overflow: hidden;
  289. }
  290. .photos1 {
  291. margin: 70px auto;
  292. left: 50%;
  293. margin-left: -14px;
  294. }
  295. .photobrowsing {
  296. position: absolute;
  297. padding: 4px;
  298. right: 0;
  299. top: 0;
  300. z-index: 99;
  301. background-color: rgba(255, 255, 255, 0.8);
  302. border-bottom-left-radius: 3px;
  303. border-top-left-radius: 3px;
  304. }
  305. .coverImg {
  306. text-align: center;
  307. position: absolute;
  308. top: 0;
  309. left: 0;
  310. height: 164px;
  311. width: 100%;
  312. }
  313. .coverImg .ico {
  314. top: 42%;
  315. }
  316. .mask {
  317. position: fixed;
  318. top: 0;
  319. left: 0;
  320. right: 0;
  321. bottom: 0;
  322. width: 100%;
  323. height: 100%;
  324. background: rgba(255, 255, 255, 0.8);
  325. display: flex;
  326. justify-content: center;
  327. align-items: center;
  328. z-index: 99999999;
  329. display: flex;
  330. flex-direction: column;
  331. .progressClose {
  332. width: 70px;
  333. text-align: center;
  334. background: #67c23a;
  335. color: #fff;
  336. height: 30px;
  337. line-height: 30px;
  338. border-radius: 5px;
  339. margin-top: 5px;
  340. font-size: 12px;
  341. }
  342. }
  343. </style>
  344. <style lang="scss">
  345. .mask {
  346. .el-progress__text {
  347. white-space: pre-wrap;
  348. }
  349. }
  350. </style>