uploadImgVStore.vue 9.3 KB

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