uploadVNormal.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. <template>
  2. <div class="questionNamep">
  3. <!-- 0=企业微信,1=H5相机 -->
  4. <!-- 企业微信拍照 -->
  5. <div class="cameraDiv" @click="uploadImg" v-if="userInfo.photoMethod == '0'">
  6. <van-icon class="photo photos" name="photograph" size="22px" color="#969696" />
  7. </div>
  8. <!-- 原生自带拍照 -->
  9. <H5Camera
  10. @getImg="getImg"
  11. ref="H5Camera"
  12. :capture="pictureSource == '1' ? '' : 'camera'"
  13. v-else />
  14. <div id="allmap"></div>
  15. <div class="mask" v-if="progressFlag">
  16. <el-progress
  17. type="circle"
  18. :percentage="percentage"
  19. :show-text="true"
  20. :width="110"
  21. :format="format"></el-progress>
  22. <div class="progressClose" @click="progressClose">取消</div>
  23. </div>
  24. <imageAIVerifyErr
  25. v-if="imageAIVerifyFlag"
  26. :imageAIVerifyFlag="imageAIVerifyFlag"
  27. :imageAIVerifyData="imageAIVerifyData"
  28. @confirmUpload="confirmUpload"
  29. @uploadImgFun="uploadImgFun"
  30. :source="'visit'"
  31. @normalFlow="normalFlow"
  32. @close="close"></imageAIVerifyErr>
  33. <!-- 图像识别白名单弹框提示 -->
  34. <imageWhiteStore
  35. v-if="imageWhiteStoreFlag"
  36. :imageWhiteStoreFlag="imageWhiteStoreFlag"
  37. :imageWhiteStoreData="imageWhiteStoreData"
  38. @normalFlow="normalFlow"
  39. @close="close">
  40. </imageWhiteStore>
  41. </div>
  42. </template>
  43. <script>
  44. import { addstorePhoto, addVisitsPosition, addPhotoToDB } from '@/api/index';
  45. import imageAIVerifyErr from './imageAIVerifyErr';
  46. import imageWhiteStore from './imageWhiteStore';
  47. import H5Camera from '@/components/H5Camera';
  48. import axios from 'axios';
  49. import uploadAliOss from '@/utils/uploadAliOss';
  50. import { addH5Photo } from '@/api/H5Camera';
  51. import { mapState } from 'vuex';
  52. export default {
  53. name: 'uploadImg',
  54. components: { imageAIVerifyErr, H5Camera, imageWhiteStore },
  55. props: {
  56. uploadid: {
  57. type: String,
  58. default: '',
  59. },
  60. storeGroupId: {
  61. type: String,
  62. default: '',
  63. },
  64. parentCollectionId: {
  65. type: String,
  66. default: '',
  67. },
  68. secondCollectionId: {
  69. type: [String, Number],
  70. default: '',
  71. },
  72. firstCollectionId: {
  73. type: String,
  74. default: '',
  75. },
  76. fourthCollectionId: {
  77. type: String,
  78. default: '',
  79. },
  80. thirdCollectionId: {
  81. type: String,
  82. default: '',
  83. },
  84. visitsId: {
  85. type: String,
  86. default: '',
  87. },
  88. taskId: {
  89. type: String,
  90. default: '',
  91. },
  92. collectionId: {
  93. type: String,
  94. default: '',
  95. },
  96. objectType: {
  97. type: String,
  98. default: '',
  99. },
  100. type: {
  101. type: Number,
  102. default: 1,
  103. },
  104. imgArr: {
  105. type: Array,
  106. default() {
  107. return [];
  108. },
  109. },
  110. visitModel: {
  111. type: String,
  112. default: '1',
  113. },
  114. deviceCode: {
  115. type: String,
  116. default: '',
  117. },
  118. putInCode: {
  119. type: String,
  120. default: '',
  121. },
  122. pictureSource: {
  123. // 是否允许从相册选择图片 1:允许;0:不允许
  124. type: String,
  125. default: '0',
  126. },
  127. photoIdentifyType: {
  128. // 图匠识别目的(1:店招内容识别(不能连拍和多选),3:调色机识别(不能连拍和多选),6:陈列SKU图片识别(不需要图匠实时识别))
  129. type: String,
  130. default: '',
  131. },
  132. continuousShoot: {
  133. // 是否允许连拍/相册多选 1:允许;0:不允许
  134. type: String,
  135. default: '0',
  136. },
  137. },
  138. computed: {
  139. ...mapState({
  140. userInfo: (state) => state.user.userInfo,
  141. }),
  142. },
  143. data() {
  144. return {
  145. shows: false,
  146. url: '',
  147. progressFlag: false,
  148. percentage: 0,
  149. timeFlag: null,
  150. imageAIVerifyFlag: false,
  151. imageAIVerifyData: null, //图匠校验返回的数据
  152. mediaId: '', //当前上传图片id
  153. addressesRemark: '', //当前位置信息
  154. controller: null, //取消请求状态
  155. fileUrl: '',
  156. imageWhiteStoreData: null,
  157. imageWhiteStoreFlag: false,
  158. localIdsArr: [],
  159. };
  160. },
  161. methods: {
  162. // 原生H5拍照图片
  163. // url: base64
  164. getImg(base64) {
  165. if (this.objectType == '' || this.objectType == null) {
  166. this.$toast('请选择类型!');
  167. return;
  168. }
  169. // 图片名称:用户名-时间戳
  170. let username = localStorage.getItem('loginName');
  171. let imgName = username + '-' + new Date().getTime();
  172. uploadAliOss(base64, imgName)
  173. .then((res) => {
  174. if (res.url && res.url.indexOf('http') != -1) {
  175. this.fileUrl = res.url;
  176. this.uploadImagev();
  177. }
  178. })
  179. .catch((err) => {
  180. console.log('err:' + err);
  181. });
  182. },
  183. uploadImgFun() {
  184. // 0=企业微信,1=H5相机
  185. if (this.userInfo.photoMethod == '0') {
  186. this.uploadImg();
  187. } else {
  188. this.$refs.H5Camera.camera();
  189. }
  190. },
  191. uploadImg() {
  192. var map = new TMap.Map('allmap', {
  193. zoom: 14,
  194. center: new TMap.LatLng(39.986785, 116.301012),
  195. });
  196. var geocoder = new TMap.service.Geocoder(); // 新建一个正逆地址解析类
  197. var markers = new TMap.MultiMarker({
  198. map: map,
  199. geometries: [],
  200. });
  201. markers.setGeometries([]);
  202. if (this.objectType == '' || this.objectType == null) {
  203. this.$toast('请选择类型!');
  204. return;
  205. }
  206. let url = window.location.href;
  207. let that = this;
  208. let wx = this.wx;
  209. let qiyeData;
  210. this.addressesRemark = '';
  211. const instance = axios.create();
  212. instance.defaults.headers.common['userId'] = localStorage.getItem('loginName');
  213. instance
  214. .get(process.env.VUE_APP_BASE_API + 'mobile/wx/ticket', {
  215. params: {
  216. url: url,
  217. },
  218. })
  219. .then((response) => {
  220. if (response.status == 200) {
  221. qiyeData = response.data.data;
  222. wx.config({
  223. beta: true, // 必须这么写,否则wx.invoke调用形式的jsapi会有问题
  224. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  225. appId: qiyeData.appId, // 必填,企业微信的corpID
  226. timestamp: qiyeData.timestamp, // 必填,生成签名的时间戳
  227. nonceStr: qiyeData.nonceStr, // 必填,生成签名的随机串
  228. signature: qiyeData.signature, // 必填,签名,见 附录-JS-SDK使用权限签名算法
  229. jsApiList: ['ready', 'chooseImage', 'uploadImage', 'getLocation'], // 必填,需要使用的JS接口列表,凡是要调用的接口都需要传进来
  230. });
  231. wx.ready(function () {
  232. wx.getLocation({
  233. type: 'gcj02',
  234. success: function (res) {
  235. var location = new TMap.LatLng(res.latitude, res.longitude);
  236. map.setCenter(location);
  237. markers.updateGeometries([
  238. {
  239. id: 'main', // 点标注数据数组
  240. position: location,
  241. },
  242. ]);
  243. geocoder.getAddress({ location: location }).then(
  244. function (result) {
  245. var addresses = result.result.formatted_addresses;
  246. that.addressesRemark = addresses.recommend;
  247. },
  248. function (err) {
  249. that.addressesRemark = '';
  250. }
  251. );
  252. },
  253. fail: function () {
  254. that.$dialog.alert({
  255. message: 'GPS未开启',
  256. });
  257. },
  258. });
  259. console.log(that.pictureSource);
  260. let sourceType = that.pictureSource == '1' ? ['album', 'camera'] : ['camera'];
  261. let count = 1;
  262. // 1:店招内容识别(不能连拍和多选),3:调色机识别(不能连拍和多选) 需要实时识别的不支持连拍和多选
  263. if (that.photoIdentifyType != 1 && that.photoIdentifyType != 3) {
  264. count = that.continuousShoot == '1' ? 5 : 1; //是否允许连拍/相册多选 最多5张
  265. }
  266. wx.chooseImage({
  267. count: count,
  268. sizeType: ['original'], // 可以指定是原图还是压缩图,默认二者都有
  269. sourceType: sourceType, // 可以指定来源是相册还是相机,默认二者都有
  270. defaultCameraMode: 'normal', //表示进入拍照界面的默认模式,目前有normal与batch两种选择,normal表示普通单拍模式,batch表示连拍模式,不传该参数则为normal模式。从3.0.26版本开始支持front和batch_front两种值,其中front表示默认为前置摄像头单拍模式,batch_front表示默认为前置摄像头连拍模式。(注:用户进入拍照界面仍然可自由切换两种模式)
  271. isSaveToAlbum: 0, //整型值,0表示拍照时不保存到系统相册,1表示自动保存,默认值是1
  272. success: function (res) {
  273. let localIds = res.localIds;
  274. that.localIdsArr = [];
  275. that.syncUpload(localIds);
  276. // var localIds = '';
  277. // if (res.localIds != undefined) {
  278. // localIds = res.localIds[0];
  279. // } else {
  280. // localIds = res.localId;
  281. // }
  282. // wx.uploadImage({
  283. // localId: localIds, // 需要上传的图片的本地ID,由chooseImage接口获得
  284. // isShowProgressTips: 0, // 默认为1,显示进度提示
  285. // success: function (res) {
  286. // that.mediaId = res.serverId;
  287. // that.uploadImagev(addressesRemark);
  288. // },
  289. // fail: (err) => {
  290. // that.$toast(err.errMsg);
  291. // that.$toast(err.errCode);
  292. // },
  293. // });
  294. },
  295. });
  296. });
  297. }
  298. });
  299. },
  300. syncUpload(localIds) {
  301. if (!localIds.length) {
  302. this.uploadImagev();
  303. } else {
  304. var localId = localIds.pop();
  305. wx.uploadImage({
  306. localId: localId,
  307. isShowProgressTips: 1, // 默认为1,显示进度提示
  308. success: (res) => {
  309. this.localIdsArr.push(res.serverId);
  310. this.syncUpload(localIds);
  311. },
  312. });
  313. }
  314. },
  315. uploadImagev() {
  316. // 初始化重置 图匠校验
  317. this.resetProgress();
  318. this.close();
  319. var that = this;
  320. var parentCollectionId = null;
  321. if (that.parentCollectionId != null && that.parentCollectionId != 'null') {
  322. parentCollectionId = that.parentCollectionId;
  323. }
  324. var secondCollectionId = null;
  325. if (that.secondCollectionId != null && that.secondCollectionId != 'null') {
  326. secondCollectionId = that.secondCollectionId;
  327. }
  328. var firstCollectionId = null;
  329. if (that.firstCollectionId != null && that.firstCollectionId != 'null') {
  330. firstCollectionId = that.firstCollectionId;
  331. }
  332. var fourthCollectionId = null;
  333. if (that.fourthCollectionId != null && that.fourthCollectionId != 'null') {
  334. fourthCollectionId = that.fourthCollectionId;
  335. }
  336. var thirdCollectionId = null;
  337. if (that.thirdCollectionId != null && that.thirdCollectionId != 'null') {
  338. thirdCollectionId = that.thirdCollectionId;
  339. }
  340. var form = {
  341. mediaIds: '',
  342. fileUrl: '',
  343. collectionItemId: that.collectionId,
  344. objectType: that.objectType,
  345. storeGroupId: that.storeGroupId,
  346. taskId: that.taskId,
  347. visitsId: localStorage.getItem('visitId'),
  348. visitModel: that.visitModel,
  349. visitSource: '1',
  350. locationRemark: that.addressesRemark,
  351. parentCollectionId: parentCollectionId,
  352. secondCollectionId: secondCollectionId,
  353. firstCollectionId: firstCollectionId,
  354. fourthCollectionId: fourthCollectionId,
  355. thirdCollectionId: thirdCollectionId,
  356. deviceCode: that.deviceCode, //设备编号
  357. putInCode: that.putInCode, //投放编号
  358. };
  359. // 0=企业微信,1=H5相机
  360. if (this.userInfo.photoMethod == '0') {
  361. form.mediaIds = this.localIdsArr; // string 图片素材id
  362. } else {
  363. form.fileUrl = this.fileUrl; // string 图片素材id
  364. }
  365. this.controller = null;
  366. // 需要图匠校验的添加参数和loading
  367. if (
  368. this.photoIdentifyType &&
  369. (this.photoIdentifyType == '1' || this.photoIdentifyType == '3')
  370. ) {
  371. form.photoIdentifyType = this.photoIdentifyType;
  372. this.progress();
  373. this.controller = new AbortController(); //取消请求
  374. } else {
  375. this.toastLoading(0, '上传中...', true);
  376. }
  377. addstorePhoto(form, this.controller ? this.controller.signal : null)
  378. .then((res) => {
  379. this.requestThen(res);
  380. })
  381. .catch((error) => {
  382. this.requestCatch(error);
  383. });
  384. },
  385. // 公用请求then
  386. requestThen(res) {
  387. this.toastLoading().clear();
  388. if (res.code == -1) {
  389. // 图匠图片校验接口超时
  390. this.requestTimeOut(res);
  391. } else if (res.code == 200) {
  392. // 图匠校验结果返回
  393. if (
  394. this.photoIdentifyType &&
  395. (this.photoIdentifyType == '1' || this.photoIdentifyType == '3')
  396. ) {
  397. // 重置loaidng状态
  398. this.resetProgress();
  399. this.imageAIVerifyFlag = true;
  400. this.imageAIVerifyData = res.data;
  401. } else {
  402. // 正常流程
  403. // 图像识别白名单用户弹出框提示
  404. if (res.data.whiteStore) {
  405. this.imageWhiteStoreFlag = true;
  406. this.imageWhiteStoreData = res;
  407. } else {
  408. this.normalFlow(res);
  409. }
  410. }
  411. } else {
  412. this.resetProgress();
  413. that.$toast('上传失败!');
  414. }
  415. },
  416. // 公用请求catch
  417. requestCatch(error) {
  418. if (error.message === 'canceled') {
  419. this.$toast('取消上传');
  420. console.log('请求被取消:', error.message);
  421. }
  422. this.resetProgress();
  423. },
  424. // 正常流程
  425. normalFlow(res) {
  426. this.$toast('上传成功!');
  427. this.$emit('newimgarr', {
  428. fileUrl: res.data.url,
  429. id: res.data.fileId,
  430. type: 2,
  431. photoIdentifyType: this.photoIdentifyType,
  432. });
  433. },
  434. progress() {
  435. // 后端接口20000ms后失效,每1000m progress加10,到90停止;
  436. this.progressFlag = true;
  437. this.percentage = 10;
  438. this.timeFlag = setInterval(() => {
  439. this.percentage = this.percentage + 10;
  440. if (this.percentage == 90) clearInterval(this.timeFlag);
  441. }, 1000);
  442. },
  443. format(percentage) {
  444. return `${percentage} %\n图像识别中`;
  445. },
  446. // 重置loaidng状态
  447. resetProgress() {
  448. this.percentage = 100;
  449. clearInterval(this.timeFlag);
  450. this.progressFlag = false;
  451. this.percentage = 0;
  452. },
  453. // 照片是否入库,1.照片识别三次不通过仍要上传,2.照片识别通过
  454. // isUpdate:是否更新店招照片,只有门店店招需要更新
  455. confirmUpload(res) {
  456. if (this.photoIdentifyType && this.photoIdentifyType != '6') {
  457. var form = {
  458. mediaIds: '',
  459. fileUrl: '',
  460. visitSource: '1', // Long 拜访模式
  461. storeGroupId: this.storeGroupId, // string 门店任务组,多个用逗号隔开
  462. visitsId: localStorage.getItem('visitId'), // string 拜访id
  463. taskId: this.taskId, // string 任务id
  464. objectType: this.objectType, // string 照片类型,取任务上的照片类型,如果没有则取手动选择的照片类型
  465. locationRemark: this.addressesRemark, // String 当前地址信息
  466. firstCollectionId: this.firstCollectionId, // Long 第一级采集项id,取当前采集项的字段就行
  467. secondCollectionId: this.secondCollectionId, // Long 第二级采集项id,取当前采集项的字段就行
  468. putInCode: this.putInCode, // String 当前任务对应的投放编号
  469. deviceCode: this.deviceCode, // String 当前任务对应的设备编号
  470. collectionItemId: this.collectionId,
  471. url: res.data.url, // String 当前拍摄图片的url
  472. businessId: res.data.businessId, // 当前拍摄图片id
  473. feedbackMessage: res.feedbackMessage,
  474. };
  475. // 0=企业微信,1=H5相机
  476. if (this.userInfo.photoMethod == '0') {
  477. form.mediaIds = this.localIdsArr; // string 图片素材id
  478. } else {
  479. form.fileUrl = this.fileUrl; // string 图片素材id
  480. }
  481. if (res.isUpdate) {
  482. form.isUpdate = 'true';
  483. }
  484. addPhotoToDB(form).then((resData) => {
  485. if (resData.code == 200) {
  486. console.log(resData);
  487. res.data.fileId = resData.data.fileId;
  488. this.normalFlow(res);
  489. }
  490. });
  491. }
  492. },
  493. close() {
  494. this.imageAIVerifyFlag = false;
  495. this.imageWhiteStoreFlag = false;
  496. },
  497. requestTimeOut(res) {
  498. this.resetProgress();
  499. this.close();
  500. this.$dialog
  501. .confirm({
  502. title: '系统提示',
  503. message: res.msg,
  504. showCancelButton: false,
  505. })
  506. .then(() => {
  507. this.confirmUpload(res);
  508. });
  509. },
  510. // 取消图片上传
  511. progressClose() {
  512. this.controller.abort();
  513. },
  514. },
  515. };
  516. </script>
  517. <style lang="scss" scoped>
  518. .questionNamep {
  519. font-size: 16px;
  520. color: #484848;
  521. line-height: 40px;
  522. padding: 0 15px;
  523. box-sizing: border-box;
  524. position: relative;
  525. .cameraDivp {
  526. flex: 1;
  527. display: flex;
  528. align-items: center;
  529. justify-content: center;
  530. .photo {
  531. /*margin-top: 9px;*/
  532. float: right;
  533. }
  534. .camera {
  535. width: 60px;
  536. height: 100%;
  537. position: absolute;
  538. right: 0;
  539. top: 0;
  540. opacity: 0;
  541. z-index: 89;
  542. }
  543. }
  544. .mask {
  545. position: fixed;
  546. top: 0;
  547. left: 0;
  548. right: 0;
  549. bottom: 0;
  550. width: 100%;
  551. height: 100%;
  552. background: rgba(255, 255, 255, 1);
  553. display: flex;
  554. justify-content: center;
  555. align-items: center;
  556. z-index: 99999999;
  557. display: flex;
  558. flex-direction: column;
  559. .progressClose {
  560. width: 70px;
  561. text-align: center;
  562. background: #67c23a;
  563. color: #fff;
  564. height: 30px;
  565. line-height: 30px;
  566. border-radius: 5px;
  567. margin-top: 5px;
  568. font-size: 12px;
  569. }
  570. }
  571. }
  572. #allmap {
  573. width: 10px;
  574. height: 10px;
  575. left: -1000px;
  576. position: relative;
  577. }
  578. </style>
  579. <style lang="scss">
  580. .mask {
  581. .el-progress__text {
  582. white-space: pre-wrap;
  583. }
  584. }
  585. </style>