uploadVNormal.vue 18 KB

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