uploadVNormal.vue 17 KB

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