common.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*******************************************************************************
  2. * Copyright(c) 2020 dongke All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:
  5. * 2.功能描述:共同类
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * admin 2021-03-31 1.00 新建
  9. *******************************************************************************/
  10. var QQMapWX = require('./qqmap-wx-jssdk.min');
  11. const config = require('../config/config.js');
  12. const Constants = require('../utils/Constants.js');
  13. const util = require('./util');
  14. const app = getApp();
  15. var qqmapsdk;
  16. /**
  17. * 小程序地图调用
  18. */
  19. function onClickWxchartAddress() {
  20. const key = config.key; //使用在腾讯位置服务申请的key
  21. const referer = 'wx76a9a06e5b4e693e'; //调用插件的app的名称
  22. const category = '小区';
  23. wx.navigateTo({
  24. url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer + '&category=' + category
  25. });
  26. }
  27. /**
  28. * 判断是否有按钮权限
  29. * @param {*} functionCode
  30. */
  31. function hasRight(functionCode) {
  32. let rights = app.globalData.user.rights
  33. let value = false
  34. if (rights != null && rights.length > 0) {
  35. rights.forEach(item => {
  36. if (item.FunctionCode == functionCode) {
  37. value = true
  38. return
  39. }
  40. })
  41. }
  42. return value
  43. }
  44. /**
  45. * 跳转到固定节点的位置
  46. * @param {*} selectid
  47. * @param {*} minus
  48. */
  49. function navigatePosition(selectid, minus) {
  50. //1、返回一个查询实例
  51. const query = wx.createSelectorQuery();
  52. //2、选择要跳转的节点id
  53. query.select('#' + selectid).boundingClientRect();
  54. //3、获取显示区域的尺寸、滚动等位置等信息,然后添加节点的滚动位置查询
  55. query.selectViewport().scrollOffset();
  56. //4、执行跳转
  57. query.exec((res) => {
  58. //5、res[0]是步骤2中的数据,res[1]是步骤3中的数据
  59. if (res[0] && res[1]) {
  60. //6、将页面滚动到目标位置 减300不是固定的 根据自己的要求调
  61. wx.pageScrollTo({
  62. //7、计算滚动到目标的位置
  63. scrollTop: res[0].top + res[1].scrollTop - minus,
  64. duration: 300
  65. })
  66. }
  67. });
  68. }
  69. /**
  70. * @desc : 前台实现千分位
  71. * @date : 2022/6/30 16:49
  72. * @author : 周兴
  73. */
  74. function toThousandCents(num) {
  75. if (num == undefined) {
  76. return 0;
  77. }
  78. var num = num + '';
  79. var d = '';
  80. if (num.slice(0, 1) == '-') {
  81. d = num.slice(0, 1);
  82. num = num.slice(1);
  83. }
  84. var len = num.length;
  85. var index = num.indexOf('.');
  86. if (index == -1) {
  87. num = num + '.00';
  88. } else if ((index + 2) == len) {
  89. num = num + '0';
  90. }
  91. var index = num.indexOf('.'); // 字符出现的位置
  92. var num2 = num.slice(-3);
  93. num = num.slice(0, index)
  94. var result = '';
  95. while (num.length > 3) {
  96. result = ',' + num.slice(-3) + result;
  97. num = num.slice(0, num.length - 3);
  98. }
  99. if (num) {
  100. result = num + result;
  101. }
  102. return d + (result + num2)
  103. }
  104. /**
  105. * 导航
  106. */
  107. function navigate(address) {
  108. qqmapsdk.geocoder({
  109. //获取表单传入地址
  110. address: address,
  111. //地址参数,例:固定地址,address: '北京市海淀区彩和坊路海淀西大街74号'
  112. success: function (res) {//成功后的回调
  113. var res = res.result;
  114. var latitude = res.location.lat;
  115. var longitude = res.location.lng;
  116. wx.openLocation({
  117. latitude,
  118. longitude,
  119. scale: 18
  120. })
  121. },
  122. fail: function (error) {
  123. wx.showToast({
  124. title: '地址无法导航',
  125. image: '/static/image/warning.png',
  126. duration: 2000
  127. })
  128. },
  129. complete: function (res) {
  130. }
  131. })
  132. }
  133. /**
  134. * @desc : 设置图片路径
  135. * @date : 2022/7/5 16:49
  136. * @author : 周兴
  137. */
  138. function setSkuIcon(skuData) {
  139. if (skuData && skuData.length > 0) {
  140. let imagePath = config.server_img_http;
  141. let imageHgPath = config.server_hg_img_http;
  142. if (skuData != null && skuData.length > 0) {
  143. skuData.forEach(it => {
  144. if (it.pathType === 1) { //业务中台 2 本系统 1 2 需要加密
  145. // 对行数据进行图标路径的处理
  146. setImagePath(it, imagePath, imageHgPath);
  147. // 如果有子级,需要对子级进行处理
  148. if (it.goodsSkuList != null && it.goodsSkuList.length > 0) {
  149. it.goodsSkuList.forEach(row => {
  150. // 对子数据行数据进行图标路径的处理
  151. setImagePath(row, imagePath, imageHgPath);
  152. })
  153. }
  154. }
  155. })
  156. }
  157. }
  158. }
  159. /**
  160. * @desc : 设置行的图标路径
  161. * @date : 2022/7/5 16:49
  162. * @author : 周兴
  163. */
  164. function setImagePath(it, imagePath, imageHgPath) {
  165. // 根据图标来源设置绝对路径
  166. if (it.pathType == Constants.PATH_TYPE.HG) {
  167. if (it.iconPath && it.iconPath.indexOf(imageHgPath) < 0) {
  168. it.iconPath = imageHgPath + it.iconPath
  169. }
  170. if (it.iconThumPath && it.iconThumPath.indexOf(imageHgPath) < 0) {
  171. it.iconThumPath = imageHgPath + it.iconThumPath
  172. }
  173. } else {
  174. if (it.iconPath && it.iconPath.indexOf(imagePath) < 0) {
  175. it.iconPath = imagePath + it.iconPath
  176. }
  177. if (it.iconThumPath && it.iconThumPath.indexOf(imagePath) < 0) {
  178. it.iconThumPath = imagePath + it.iconThumPath
  179. }
  180. }
  181. }
  182. function setImagePathForm(it, imagePath, imageHgPath) {
  183. // 根据图标来源设置绝对路径
  184. if (it.path_type == Constants.PATH_TYPE.HG) {
  185. if (it.icon_path && it.icon_path.indexOf(imageHgPath) < 0) {
  186. it.icon_path = imageHgPath + it.icon_path
  187. }
  188. if (it.icon_thum_path && it.icon_thum_path.indexOf(imageHgPath) < 0) {
  189. it.icon_thum_path = imageHgPath + it.icon_thum_path
  190. }
  191. } else {
  192. if (it.icon_path && it.icon_path.indexOf(imagePath) < 0) {
  193. it.icon_path = imagePath + it.icon_path
  194. }
  195. if (it.icon_thum_path && it.icon_thum_path.indexOf(imagePath) < 0) {
  196. it.icon_thum_path = imagePath + it.icon_thum_path
  197. }
  198. }
  199. }
  200. /**
  201. * @desc : 设置行的图标路径
  202. * @date : 2022/7/5 16:49
  203. * @author : 周兴
  204. */
  205. function setRowImagePath(it) {
  206. let imagePath = config.server_img_http;
  207. let imageHgPath = config.server_hg_img_http;
  208. // 根据图标来源设置绝对路径
  209. setImagePath(it, imagePath, imageHgPath);
  210. }
  211. /**
  212. * 本接口提供由坐标到坐标所在位置的文字描述的转换,输入坐标返回地理位置信息和附近poi列表。
  213. * @param {*} loc
  214. */
  215. function reverseGeocoder(loc) {
  216. return new Promise(resolve => {
  217. qqmapsdk.reverseGeocoder({
  218. location: {
  219. latitude: loc.latitude,
  220. longitude: loc.longitude
  221. } || '',
  222. //get_poi: 1, //是否返回周边POI列表:1.返回;0不返回(默认),非必须参数
  223. success: function (res) {//成功后的回调
  224. resolve(res)
  225. },
  226. fail: function (error) {
  227. resolve('')
  228. },
  229. complete: function (res) {
  230. }
  231. })
  232. });
  233. }
  234. /**
  235. * 调用距离计算接口
  236. * @param {*} fromloc
  237. * @param {*} toloc
  238. */
  239. function calculateDistance(fromloc, toloc) {
  240. return new Promise(resolve => {
  241. //调用距离计算接口
  242. qqmapsdk.calculateDistance({
  243. //mode: 'straight',//可选值:'driving'(驾车)、'walking'(步行),不填默认:'walking',可不填 新增直线距离计算,‘straight’(直线)
  244. //from参数不填默认当前地址
  245. //获取表单提交的经纬度并设置from和to参数(示例为string格式)
  246. from: fromloc, //若起点有数据则采用起点坐标,若为空默认当前地址
  247. to: toloc, //终点坐标
  248. success: function (res) {//成功后的回调
  249. var destinationDistance = res.result.elements[0].distance;
  250. resolve(destinationDistance)
  251. },
  252. fail: function (error) {
  253. resolve(0)
  254. },
  255. complete: function (res) {
  256. }
  257. });
  258. });
  259. }
  260. /**
  261. * 百度转腾讯
  262. * @param {*} lat
  263. * @param {*} lng
  264. */
  265. function bdMap_to_wxMap(lat, lng) {
  266. let pi = 3.14159265358979324 * 3000.0 / 180.0;
  267. let x = lng - 0.0065;
  268. let y = lat - 0.006;
  269. var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * pi);
  270. var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * pi);
  271. let lngv = z * Math.cos(theta);
  272. let latv = z * Math.sin(theta);
  273. return { 'longitude': lngv, 'latitude': latv };
  274. }
  275. /**
  276. * 腾讯转百度
  277. * @param {*} lat
  278. * @param {*} lng
  279. */
  280. function wxMap_to_bdMap(lat, lng) {
  281. let pi = 3.14159265358979324 * 3000.0 / 180.0;
  282. let x = lng;
  283. let y = lat;
  284. var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * pi);
  285. var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * pi);
  286. let lngv = z * Math.cos(theta) + 0.0065;
  287. let latv = z * Math.sin(theta) + 0.006;
  288. return { 'longitude': lngv, 'latitude': latv };
  289. }
  290. /**
  291. * 在onload时间中新建qq地址对象
  292. */
  293. function openQQMap() {
  294. if (qqmapsdk === undefined || qqmapsdk === null) {
  295. qqmapsdk = new QQMapWX({
  296. key: '7I3BZ-VADCX-TJQ4C-TN5O4-F4BZE-RDFCK'
  297. });
  298. }
  299. }
  300. /**
  301. * @desc : 计算品类数
  302. * @date : 2022/7/11 16:49
  303. * @author : 周兴
  304. */
  305. function getCategoryQuantity(goodsList) {
  306. let categoryQuantityList = [] // 品类数
  307. goodsList.forEach(res => {
  308. // 如果有子级,计算子级的数量
  309. if (res.goodsSkuList && res.goodsSkuList.length > 0) {
  310. res.goodsSkuList.forEach(it => {
  311. if (it.skuSpecs == null) {
  312. it.skuSpecs = " "
  313. }
  314. if (it.skuSpecs && !categoryQuantityList.includes(it.skuSpecs) && !it.flgBomCombined) {
  315. categoryQuantityList.push(it.skuSpecs)
  316. }
  317. })
  318. } else {
  319. if (res.skuSpecs == null) {
  320. res.skuSpecs = " "
  321. }
  322. // 如果没有子级,就加上即可
  323. if (res.skuSpecs && !categoryQuantityList.includes(res.skuSpecs) && !res.flgBomCombined) {
  324. categoryQuantityList.push(res.skuSpecs)
  325. }
  326. }
  327. })
  328. return categoryQuantityList.length;
  329. }
  330. /**
  331. * @desc : 重新计算最后一条金额(用父级去减去其他的金额,计算最后一条)
  332. * @date : 2022/7/7 16:49
  333. * @author : 周兴
  334. */
  335. function countLastAmount(choosedGoodsList, followFlag) {
  336. if (choosedGoodsList && choosedGoodsList.length > 0) {
  337. let pRows = choosedGoodsList.filter(it => !it.parentId || it.parentId == null);
  338. if (pRows && pRows.length > 0) {
  339. pRows.forEach(it => {
  340. if (it.goodsSkuList != null && it.goodsSkuList.length > 0) {
  341. // 获取子级
  342. let childRows = it.goodsSkuList.filter(row => row.parentId == it.id && row.hasChild == 0);
  343. let amount = Number(it.itemAmount); // 父级的金额
  344. if (childRows && childRows.length > 0) {
  345. // 只到倒数第二条
  346. for (let i = 0; i < childRows.length - 1; i++) {
  347. amount = util.SubNumber(amount, Number(childRows[i].itemAmount))
  348. }
  349. // 如果是退换补,还需要给实际赋值 //于继渤 添加else分支 不然 退还补 选完bom 价格出现NAN 报错 2022/07/21
  350. childRows[childRows.length - 1].itemAmount = amount;
  351. childRows[childRows.length - 1].priceSale = Number(childRows[childRows.length - 1].itemAmount / childRows[childRows.length - 1].itemQuantity).toFixed(2)
  352. if (followFlag) {
  353. childRows[childRows.length - 1].tItemAmount = amount;
  354. childRows[childRows.length - 1].priceSale = Number(childRows[childRows.length - 1].tItemAmount / childRows[childRows.length - 1].tItemQuantity).toFixed(2)
  355. }
  356. }
  357. }
  358. })
  359. }
  360. }
  361. }
  362. /**
  363. * 判断是否有按钮权限
  364. * @param {*} functionCode
  365. */
  366. function hasButtonRight(functionCode) {
  367. let buttonRights = app.globalData.buttonRights
  368. let orderAuditVisible = buttonRights.indexOf(functionCode)
  369. return orderAuditVisible == -1 ? false : true
  370. }
  371. /**
  372. * 判断必填项
  373. * @param {*} functionCode
  374. */
  375. function hasInputPageItemMust(functionCode) {
  376. let pageItemMusts = app.globalData.pageItemMusts
  377. let orderAuditVisible = false
  378. pageItemMusts.forEach(res => {
  379. if (res.itemCodeTag['WX-CODE'] && res.itemCodeTag['WX-CODE'].indexOf(functionCode) != -1 && res.flgMustItem) {
  380. orderAuditVisible = true
  381. }
  382. })
  383. return orderAuditVisible
  384. }
  385. /**
  386. * 判断是否有画面的uuid权限
  387. * @param {*} functionCode
  388. */
  389. function hasPageRight(objectCode) {
  390. if (objectCode) {
  391. let hasRightFlag = false
  392. let menuList = app.globalData.menus
  393. if (menuList && menuList.length > 0) {
  394. // 查看该功能是否有权限
  395. let filters = menuList.filter(it => it.objectCode == objectCode);
  396. if (filters && filters.length > 0) {
  397. hasRightFlag = true;
  398. }
  399. }
  400. return hasRightFlag
  401. } else {
  402. return false
  403. }
  404. }
  405. /**
  406. * 判断是否有画面的uuid权限
  407. * @param {*} functionCode
  408. */
  409. function hasPageUUidRight(uuid) {
  410. if (uuid) {
  411. let pageRights = app.globalData.pageRights
  412. let pageVisible = pageRights.indexOf(uuid)
  413. return pageVisible == -1 ? false : true
  414. } else {
  415. return
  416. }
  417. }
  418. function isUserSensitive() {
  419. //敏感信息控制
  420. if (app.globalData.user.userSensitive && app.globalData.user.userSensitive.length > 0) {
  421. let userSensitive = app.globalData.user.userSensitive
  422. let purchasePriceFlag = true
  423. userSensitive.forEach(res => {
  424. if (res.isCheck) {
  425. if (res.docCode == '分销采购订单' && res.itemName == '采购价格' && res.isCheck) {
  426. // purchasePriceFlag = false
  427. purchasePriceFlag = true
  428. }
  429. } else {
  430. purchasePriceFlag = false
  431. }
  432. })
  433. return purchasePriceFlag
  434. } else {
  435. return false
  436. }
  437. }
  438. /**
  439. * 导出函数
  440. */
  441. module.exports = {
  442. openQQMap,
  443. navigate,
  444. toThousandCents,
  445. reverseGeocoder,
  446. calculateDistance,
  447. hasButtonRight,
  448. hasRight,
  449. wxMap_to_bdMap,
  450. bdMap_to_wxMap,
  451. setSkuIcon,
  452. setRowImagePath,
  453. onClickWxchartAddress,
  454. countLastAmount,
  455. getCategoryQuantity,
  456. setImagePath,
  457. hasPageUUidRight,
  458. setImagePathForm,
  459. hasInputPageItemMust,
  460. isUserSensitive,
  461. hasPageRight
  462. }