uploadImgVStore.vue 9.2 KB

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