uploadVNormal copy.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. mediaId: '',
  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.mediaId = 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 (this.photoIdentifyType && this.photoIdentifyType != '6') {
  394. // 重置loaidng状态
  395. this.resetProgress();
  396. this.imageAIVerifyFlag = true;
  397. this.imageAIVerifyData = res.data;
  398. } else {
  399. // 正常流程
  400. // 图像识别白名单用户弹出框提示
  401. if (res.data.whiteStore) {
  402. this.imageWhiteStoreFlag = true;
  403. this.imageWhiteStoreData = res;
  404. } else {
  405. this.normalFlow(res);
  406. }
  407. }
  408. } else {
  409. this.resetProgress();
  410. that.$toast('上传失败!');
  411. }
  412. },
  413. // 公用请求catch
  414. requestCatch(error) {
  415. if (error.message === 'canceled') {
  416. this.$toast('取消上传');
  417. console.log('请求被取消:', error.message);
  418. }
  419. this.resetProgress();
  420. },
  421. // 正常流程
  422. normalFlow(res) {
  423. this.$toast('上传成功!');
  424. this.$emit('newimgarr', {
  425. fileUrl: res.data.url,
  426. id: res.data.fileId,
  427. type: 2,
  428. photoIdentifyType: this.photoIdentifyType,
  429. });
  430. },
  431. progress() {
  432. // 后端接口20000ms后失效,每1000m progress加10,到90停止;
  433. this.progressFlag = true;
  434. this.percentage = 10;
  435. this.timeFlag = setInterval(() => {
  436. this.percentage = this.percentage + 10;
  437. if (this.percentage == 90) clearInterval(this.timeFlag);
  438. }, 1000);
  439. },
  440. format(percentage) {
  441. return `${percentage} %\n图像识别中`;
  442. },
  443. // 重置loaidng状态
  444. resetProgress() {
  445. this.percentage = 100;
  446. clearInterval(this.timeFlag);
  447. this.progressFlag = false;
  448. this.percentage = 0;
  449. },
  450. // 照片是否入库,1.照片识别三次不通过仍要上传,2.照片识别通过
  451. // isUpdate:是否更新店招照片,只有门店店招需要更新
  452. confirmUpload(res) {
  453. if (this.photoIdentifyType && this.photoIdentifyType != '6') {
  454. var form = {
  455. mediaId: '',
  456. fileUrl: '',
  457. visitSource: '1', // Long 拜访模式
  458. storeGroupId: this.storeGroupId, // string 门店任务组,多个用逗号隔开
  459. visitsId: localStorage.getItem('visitId'), // string 拜访id
  460. taskId: this.taskId, // string 任务id
  461. objectType: this.objectType, // string 照片类型,取任务上的照片类型,如果没有则取手动选择的照片类型
  462. locationRemark: this.addressesRemark, // String 当前地址信息
  463. firstCollectionId: this.firstCollectionId, // Long 第一级采集项id,取当前采集项的字段就行
  464. secondCollectionId: this.secondCollectionId, // Long 第二级采集项id,取当前采集项的字段就行
  465. putInCode: this.putInCode, // String 当前任务对应的投放编号
  466. deviceCode: this.deviceCode, // String 当前任务对应的设备编号
  467. collectionItemId: this.collectionId,
  468. url: res.data.url, // String 当前拍摄图片的url
  469. businessId: res.data.businessId, // 当前拍摄图片id
  470. feedbackMessage: res.feedbackMessage,
  471. };
  472. // 0=企业微信,1=H5相机
  473. if (this.userInfo.photoMethod == '0') {
  474. form.mediaId = this.localIdsArr; // string 图片素材id
  475. } else {
  476. form.fileUrl = this.fileUrl; // string 图片素材id
  477. }
  478. if (res.isUpdate) {
  479. form.isUpdate = 'true';
  480. }
  481. addPhotoToDB(form).then((resData) => {
  482. if (resData.code == 200) {
  483. console.log(resData);
  484. res.data.fileId = resData.data.fileId;
  485. this.normalFlow(res);
  486. }
  487. });
  488. }
  489. },
  490. close() {
  491. this.imageAIVerifyFlag = false;
  492. this.imageWhiteStoreFlag = false;
  493. },
  494. requestTimeOut(res) {
  495. this.resetProgress();
  496. this.close();
  497. this.$dialog
  498. .confirm({
  499. title: '系统提示',
  500. message: res.msg,
  501. showCancelButton: false,
  502. })
  503. .then(() => {
  504. this.confirmUpload(res);
  505. });
  506. },
  507. // 取消图片上传
  508. progressClose() {
  509. this.controller.abort();
  510. },
  511. },
  512. };
  513. </script>
  514. <style lang="scss" scoped>
  515. .questionNamep {
  516. font-size: 16px;
  517. color: #484848;
  518. line-height: 40px;
  519. padding: 0 15px;
  520. box-sizing: border-box;
  521. position: relative;
  522. .cameraDivp {
  523. flex: 1;
  524. display: flex;
  525. align-items: center;
  526. justify-content: center;
  527. .photo {
  528. /*margin-top: 9px;*/
  529. float: right;
  530. }
  531. .camera {
  532. width: 60px;
  533. height: 100%;
  534. position: absolute;
  535. right: 0;
  536. top: 0;
  537. opacity: 0;
  538. z-index: 89;
  539. }
  540. }
  541. .mask {
  542. position: fixed;
  543. top: 0;
  544. left: 0;
  545. right: 0;
  546. bottom: 0;
  547. width: 100%;
  548. height: 100%;
  549. background: rgba(255, 255, 255, 1);
  550. display: flex;
  551. justify-content: center;
  552. align-items: center;
  553. z-index: 99999999;
  554. display: flex;
  555. flex-direction: column;
  556. .progressClose {
  557. width: 70px;
  558. text-align: center;
  559. background: #67c23a;
  560. color: #fff;
  561. height: 30px;
  562. line-height: 30px;
  563. border-radius: 5px;
  564. margin-top: 5px;
  565. font-size: 12px;
  566. }
  567. }
  568. }
  569. #allmap {
  570. width: 10px;
  571. height: 10px;
  572. left: -1000px;
  573. position: relative;
  574. }
  575. </style>
  576. <style lang="scss">
  577. .mask {
  578. .el-progress__text {
  579. white-space: pre-wrap;
  580. }
  581. }
  582. </style>