uploadVNormal.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. defaultCameraMode: 'normal', //表示进入拍照界面的默认模式,目前有normal与batch两种选择,normal表示普通单拍模式,batch表示连拍模式,不传该参数则为normal模式。从3.0.26版本开始支持front和batch_front两种值,其中front表示默认为前置摄像头单拍模式,batch_front表示默认为前置摄像头连拍模式。(注:用户进入拍照界面仍然可自由切换两种模式)
  294. isSaveToAlbum: 0, //整型值,0表示拍照时不保存到系统相册,1表示自动保存,默认值是1
  295. success: function (res) {
  296. var localIds = '';
  297. if (res.localIds != undefined) {
  298. localIds = res.localIds[0];
  299. } else {
  300. localIds = res.localId;
  301. }
  302. wx.uploadImage({
  303. localId: localIds, // 需要上传的图片的本地ID,由chooseImage接口获得
  304. isShowProgressTips: 1, // 默认为1,显示进度提示
  305. success: function (res) {
  306. that.mediaId = res.serverId;
  307. that.uploadImagev(addressesRemark);
  308. },
  309. });
  310. },
  311. });
  312. });
  313. }
  314. });
  315. },
  316. uploadImagev(addressesRemark = '') {
  317. // 初始化重置 图匠校验
  318. this.resetProgress();
  319. this.close();
  320. var that = this;
  321. var parentCollectionId = null;
  322. if (that.parentCollectionId != null && that.parentCollectionId != 'null') {
  323. parentCollectionId = that.parentCollectionId;
  324. }
  325. var secondCollectionId = null;
  326. if (that.secondCollectionId != null && that.secondCollectionId != 'null') {
  327. secondCollectionId = that.secondCollectionId;
  328. }
  329. var firstCollectionId = null;
  330. if (that.firstCollectionId != null && that.firstCollectionId != 'null') {
  331. firstCollectionId = that.firstCollectionId;
  332. }
  333. var fourthCollectionId = null;
  334. if (that.fourthCollectionId != null && that.fourthCollectionId != 'null') {
  335. fourthCollectionId = that.fourthCollectionId;
  336. }
  337. var thirdCollectionId = null;
  338. if (that.thirdCollectionId != null && that.thirdCollectionId != 'null') {
  339. thirdCollectionId = that.thirdCollectionId;
  340. }
  341. this.addressesRemark = addressesRemark;
  342. var form = {
  343. mediaId: '',
  344. fileUrl: '',
  345. collectionItemId: that.collectionId,
  346. objectType: that.objectType,
  347. storeGroupId: that.storeGroupId,
  348. taskId: that.taskId,
  349. visitsId: localStorage.getItem('visitId'),
  350. visitModel: that.visitModel,
  351. visitSource: '1',
  352. locationRemark: addressesRemark,
  353. parentCollectionId: parentCollectionId,
  354. secondCollectionId: secondCollectionId,
  355. firstCollectionId: firstCollectionId,
  356. fourthCollectionId: fourthCollectionId,
  357. thirdCollectionId: thirdCollectionId,
  358. deviceCode: that.deviceCode, //设备编号
  359. putInCode: that.putInCode, //投放编号
  360. };
  361. // 0=企业微信,1=H5相机
  362. if (this.userInfo.photoMethod == '0') {
  363. form.mediaId = this.mediaId; // string 图片素材id
  364. } else {
  365. form.fileUrl = this.fileUrl; // string 图片素材id
  366. }
  367. this.controller = null;
  368. // 需要图匠校验的添加参数和loading
  369. if (this.photoIdentifyType && this.photoIdentifyType != '6') {
  370. form.photoIdentifyType = this.photoIdentifyType;
  371. this.progress();
  372. this.controller = new AbortController(); //取消请求
  373. } else {
  374. this.toastLoading(0, '上传中...', true);
  375. }
  376. addstorePhoto(form, this.controller ? this.controller.signal : null)
  377. .then((res) => {
  378. this.requestThen(res);
  379. })
  380. .catch((error) => {
  381. this.requestCatch(error);
  382. });
  383. },
  384. // 公用请求then
  385. requestThen(res) {
  386. this.toastLoading().clear();
  387. if (res.code == -1) {
  388. // 图匠图片校验接口超时
  389. this.requestTimeOut(res);
  390. } else if (res.code == 200) {
  391. // 图匠校验结果返回
  392. if (this.photoIdentifyType && this.photoIdentifyType != '6') {
  393. // 重置loaidng状态
  394. this.resetProgress();
  395. this.imageAIVerifyFlag = true;
  396. this.imageAIVerifyData = res.data;
  397. } else {
  398. // 正常流程
  399. // 图像识别白名单用户弹出框提示
  400. if (res.data.whiteStore) {
  401. this.imageWhiteStoreFlag = true;
  402. this.imageWhiteStoreData = res;
  403. } else {
  404. this.normalFlow(res);
  405. }
  406. }
  407. } else {
  408. this.resetProgress();
  409. that.$toast('上传失败!');
  410. }
  411. },
  412. // 公用请求catch
  413. requestCatch(error) {
  414. if (error.message === 'canceled') {
  415. this.$toast('取消上传');
  416. console.log('请求被取消:', error.message);
  417. }
  418. this.resetProgress();
  419. },
  420. // 正常流程
  421. normalFlow(res) {
  422. this.$toast('上传成功!');
  423. this.$emit('newimgarr', {
  424. fileUrl: res.data.url,
  425. id: res.data.fileId,
  426. type: 2,
  427. photoIdentifyType: this.photoIdentifyType,
  428. });
  429. },
  430. progress() {
  431. // 后端接口20000ms后失效,每1000m progress加10,到90停止;
  432. this.progressFlag = true;
  433. this.percentage = 10;
  434. this.timeFlag = setInterval(() => {
  435. this.percentage = this.percentage + 10;
  436. if (this.percentage == 90) clearInterval(this.timeFlag);
  437. }, 1000);
  438. },
  439. format(percentage) {
  440. return `${percentage} %\n图像识别中`;
  441. },
  442. // 重置loaidng状态
  443. resetProgress() {
  444. this.percentage = 100;
  445. clearInterval(this.timeFlag);
  446. this.progressFlag = false;
  447. this.percentage = 0;
  448. },
  449. // 照片是否入库,1.照片识别三次不通过仍要上传,2.照片识别通过
  450. // isUpdate:是否更新店招照片,只有门店店招需要更新
  451. confirmUpload(res) {
  452. if (this.photoIdentifyType && this.photoIdentifyType != '6') {
  453. var form = {
  454. mediaId: '',
  455. fileUrl: '',
  456. visitSource: '1', // Long 拜访模式
  457. storeGroupId: this.storeGroupId, // string 门店任务组,多个用逗号隔开
  458. visitsId: localStorage.getItem('visitId'), // string 拜访id
  459. taskId: this.taskId, // string 任务id
  460. objectType: this.objectType, // string 照片类型,取任务上的照片类型,如果没有则取手动选择的照片类型
  461. locationRemark: this.addressesRemark, // String 当前地址信息
  462. firstCollectionId: this.firstCollectionId, // Long 第一级采集项id,取当前采集项的字段就行
  463. secondCollectionId: this.secondCollectionId, // Long 第二级采集项id,取当前采集项的字段就行
  464. putInCode: this.putInCode, // String 当前任务对应的投放编号
  465. deviceCode: this.deviceCode, // String 当前任务对应的设备编号
  466. collectionItemId: this.collectionId,
  467. url: res.data.url, // String 当前拍摄图片的url
  468. businessId: res.data.businessId, // 当前拍摄图片id
  469. feedbackMessage: res.feedbackMessage,
  470. };
  471. // 0=企业微信,1=H5相机
  472. if (this.userInfo.photoMethod == '0') {
  473. form.mediaId = this.mediaId; // string 图片素材id
  474. } else {
  475. form.fileUrl = this.fileUrl; // string 图片素材id
  476. }
  477. if (res.isUpdate) {
  478. form.isUpdate = 'true';
  479. }
  480. addPhotoToDB(form).then((resData) => {
  481. if (resData.code == 200) {
  482. console.log(resData);
  483. res.data.fileId = resData.data.fileId;
  484. this.normalFlow(res);
  485. }
  486. });
  487. }
  488. },
  489. close() {
  490. this.imageAIVerifyFlag = false;
  491. this.imageWhiteStoreFlag = false;
  492. },
  493. requestTimeOut(res) {
  494. this.resetProgress();
  495. this.close();
  496. this.$dialog
  497. .confirm({
  498. title: '系统提示',
  499. message: res.msg,
  500. showCancelButton: false,
  501. })
  502. .then(() => {
  503. this.confirmUpload(res);
  504. });
  505. },
  506. // 取消图片上传
  507. progressClose() {
  508. this.controller.abort();
  509. },
  510. },
  511. };
  512. </script>
  513. <style lang="scss" scoped>
  514. .questionNamep {
  515. font-size: 16px;
  516. color: #484848;
  517. line-height: 40px;
  518. padding: 0 15px;
  519. box-sizing: border-box;
  520. position: relative;
  521. .cameraDivp {
  522. flex: 1;
  523. display: flex;
  524. align-items: center;
  525. justify-content: center;
  526. .photo {
  527. /*margin-top: 9px;*/
  528. float: right;
  529. }
  530. .camera {
  531. width: 60px;
  532. height: 100%;
  533. position: absolute;
  534. right: 0;
  535. top: 0;
  536. opacity: 0;
  537. z-index: 89;
  538. }
  539. }
  540. .mask {
  541. position: fixed;
  542. top: 0;
  543. left: 0;
  544. right: 0;
  545. bottom: 0;
  546. width: 100%;
  547. height: 100%;
  548. background: rgba(255, 255, 255, 1);
  549. display: flex;
  550. justify-content: center;
  551. align-items: center;
  552. z-index: 99999999;
  553. display: flex;
  554. flex-direction: column;
  555. .progressClose {
  556. width: 70px;
  557. text-align: center;
  558. background: #67c23a;
  559. color: #fff;
  560. height: 30px;
  561. line-height: 30px;
  562. border-radius: 5px;
  563. margin-top: 5px;
  564. font-size: 12px;
  565. }
  566. }
  567. }
  568. #allmap {
  569. width: 10px;
  570. height: 10px;
  571. left: -1000px;
  572. position: relative;
  573. }
  574. </style>
  575. <style lang="scss">
  576. .mask {
  577. .el-progress__text {
  578. white-space: pre-wrap;
  579. }
  580. }
  581. </style>