uploadVNormalTaskPhoto.vue 16 KB

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