uploadVNormalTaskPhoto.vue 16 KB

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