uploadVNormal.vue 18 KB

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