util.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. const validate = require("./validate.js");
  2. import __config from '@/config/env';
  3. import api from '@/utils/api.js';
  4. import Decimal from 'decimal.js';
  5. const formatTime = date => {
  6. const year = date.getFullYear();
  7. const month = date.getMonth() + 1;
  8. const day = date.getDate();
  9. const hour = date.getHours();
  10. const minute = date.getMinutes();
  11. const second = date.getSeconds();
  12. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(
  13. ':');
  14. };
  15. const formatNumber = n => {
  16. n = n.toString();
  17. return n[1] ? n : '0' + n;
  18. };
  19. //空值过滤器
  20. const filterForm = form => {
  21. let obj = {};
  22. Object.keys(form).forEach(ele => {
  23. if (!validate.validatenull(form[ele])) {
  24. obj[ele] = form[ele];
  25. }
  26. });
  27. return obj;
  28. };
  29. //获取当前页面带参数的url
  30. const getCurrentPageUrlWithArgs = val => {
  31. const pages = getCurrentPages();
  32. const currentPage = pages[pages.length - 1];
  33. const url = currentPage.route;
  34. const options = currentPage.options;
  35. let urlWithArgs = `/${url}?`;
  36. for (let key in options) {
  37. const value = options[key];
  38. urlWithArgs += `${key}=${value}&`;
  39. }
  40. urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1);
  41. return urlWithArgs;
  42. }
  43. /* 获取当前页面options参数对象 */
  44. const getCurrentPageOptions = () => {
  45. const pages = getCurrentPages();
  46. const currentPage = pages[pages.length - 1];
  47. const url = currentPage.route;
  48. const options = currentPage.options;
  49. return options
  50. }
  51. //获取url中的参数
  52. const getUrlParam = (path, name) => {
  53. var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
  54. if (reg.test(path))
  55. return unescape(RegExp.$2.replace(/\+/g, " "));
  56. return "";
  57. }
  58. //判断是否为微信浏览器中运行
  59. const isWeiXinBrowser = () => {
  60. // #ifdef H5
  61. // window.navigator.userAgent属性包含了浏览器类型、版本、操作系统类型、浏览器引擎类型等信息,这个属性可以用来判断浏览器类型
  62. let ua = window.navigator.userAgent.toLowerCase()
  63. // 通过正则表达式匹配ua中是否含有MicroMessenger字符串
  64. if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  65. return true
  66. } else {
  67. return false
  68. }
  69. // #endif
  70. return false
  71. }
  72. //判断是否是小程序
  73. const isMiniPg = () => {
  74. let isMiniPg = false
  75. //#ifdef MP-WEIXIN
  76. isMiniPg = true
  77. //#endif
  78. return isMiniPg
  79. }
  80. //获取客服端代码
  81. const getClientCode = () => {
  82. let code = {}
  83. //#ifdef MP-WEIXIN
  84. code = {
  85. key: 'MP-WEIXIN',
  86. value: '微信小程序'
  87. }
  88. //#endif
  89. //#ifdef H5
  90. //普通H5
  91. code = {
  92. key: 'H5',
  93. value: '普通H5'
  94. }
  95. if (isWeiXinBrowser()) {
  96. //微信公众号H5
  97. code = {
  98. key: 'H5-WX',
  99. value: '公众号H5'
  100. }
  101. }
  102. //#endif
  103. return code
  104. }
  105. //重置url中的参数
  106. const resetPageUrl = (query) => {
  107. var ary = [];
  108. for (var p in query) {
  109. if (query.hasOwnProperty(p) && query[p]) {
  110. ary.push(encodeURIComponent(p) + '=' + encodeURIComponent(query[p]));
  111. }
  112. }
  113. if (ary.length > 0) {
  114. let url = "?" + ary.join('&');
  115. history.replaceState(history.state, null, url); //替换页面显示url
  116. }
  117. }
  118. const imgUrlToBase64 = (imageUrl) => {
  119. return new Promise((resolve, reject) => {
  120. try {
  121. uni.downloadFile({
  122. url: imageUrl + '?s=' + Math.random().toString(),
  123. success: res => {
  124. if (res.statusCode === 200) {
  125. // uni 如果是H5或者APP会自动转为base64返回
  126. resolve(res.tempFilePath);
  127. } else {
  128. reject(res.errMsg);
  129. }
  130. },
  131. fail(err) {
  132. reject(err);
  133. }
  134. });
  135. } catch (e) {
  136. console.log(e)
  137. resolve(imageUrl);
  138. }
  139. })
  140. }
  141. // 设置h5的分享地址url
  142. const setH5ShareUrl = val => {
  143. const userInfo = uni.getStorageSync('wx_login_user_info')
  144. const userCode = userInfo ? userInfo.userCode : ''
  145. let url = window.location.href
  146. // 如果没有 sharer_user_code 就添加,如果有就替换为自己的userCode
  147. if (userCode) {
  148. let index = url.indexOf('&sharer_user_code=')
  149. if (index == -1) {
  150. url = url + '&sharer_user_code=' + userCode;
  151. } else {
  152. let urlTemp = url.slice(0, index);
  153. url = urlTemp + '&sharer_user_code=' + userCode;
  154. }
  155. }
  156. return url
  157. }
  158. // 设置H5的分享url(用于分销,默认首页地址)
  159. const setH5HomeShareUrl = val => {
  160. const userInfo = uni.getStorageSync('wx_login_user_info')
  161. const userCode = userInfo ? userInfo.userCode : ''
  162. // 如果没有 sharer_user_code 就添加
  163. let url = window.location.origin + window.location.search;
  164. if (userCode) {
  165. let index = url.indexOf('&sharer_user_code=')
  166. if (index == -1) {
  167. url = url + '&sharer_user_code=' + userCode;
  168. } else {
  169. let urlTemp = url.slice(0, index);
  170. url = urlTemp + '&sharer_user_code=' + userCode;
  171. }
  172. }
  173. return url
  174. }
  175. // 获取当前页面路由或 path
  176. const getCurPage = pages => {
  177. let curPage = pages[pages.length - 1];
  178. return curPage.route
  179. }
  180. // 保存别人分享来的 userCode
  181. const saveSharerUserCode = options => {
  182. if (options.scene) {
  183. //接受二维码中参数
  184. /**
  185. * 这里需要特别注意:
  186. * 由于腾讯限制了scenes的长度,导致传参的局限性,为尽可能的利用这有限的长度传参,
  187. * 故我们定义了scenes的参数格式,当一个页面需要传多个参数时,我们用“&”符号分割开来,第2位固定放分享人的user_code,这样可以最大限度减少长度占用
  188. * 第1位一般放ID,第2位固定放分享人的user_code,比如菜品页面scenes为:goodspuId+&+sharer_user_code
  189. * 因为固定第2位放分享人的user_code,当有些页面无需传ID时,我们也需要用“&”拼一下,第一位随意用一个字符点位即可,比如页面scenes为:0+&+sharer_user_code
  190. */
  191. let scenes = decodeURIComponent(options.scene).split('&');
  192. if (scenes[1]) {
  193. uni.setStorageSync('sharer_user_code', scenes[1]);
  194. }
  195. } else {
  196. if (options.sharer_user_code) {
  197. uni.setStorageSync('sharer_user_code', '');
  198. uni.setStorageSync('sharer_user_code', options.sharer_user_code);
  199. }
  200. }
  201. }
  202. // 如果有分享人则给data带上分享人的user_code
  203. const dataAddSharerUserCode = data => {
  204. let sharer_user_code = uni.getStorageSync('sharer_user_code')
  205. if (sharer_user_code) {
  206. data = Object.assign({
  207. sharerUserCode: sharer_user_code
  208. }, data)
  209. }
  210. return data
  211. }
  212. //返回登录页面
  213. const backLoginPage = data => {
  214. var pages = getCurrentPages(); // 获取页面栈
  215. var currPage = pages[pages.length - 1]; // 当前页面
  216. if (currPage) {
  217. let curParam = currPage.options
  218. // 拼接参数
  219. let reUrl = '/' + currPage.route
  220. if (curParam != null) {
  221. // 拼接参数
  222. let param = ''
  223. for (let key in curParam) {
  224. param += '&' + key + '=' + curParam[key]
  225. }
  226. param = param.substr(1)
  227. reUrl = reUrl + '?' + param
  228. reUrl = encodeURIComponent(reUrl)
  229. }
  230. uni.reLaunch({
  231. url: '/pages/login/index?reUrl=' + reUrl
  232. });
  233. }
  234. }
  235. /**
  236. * 拼接完整图片字符串
  237. */
  238. const spliceAssetsUrl = imgUrl => {
  239. let imgHostUrl = __config.imgHostUrl;
  240. if (!imgUrl)
  241. return imgHostUrl
  242. if (imgUrl.substr(0, 1) != "/") {
  243. imgUrl = `/${imgUrl}`
  244. }
  245. let allImgUrl = `${imgHostUrl}${imgUrl}`
  246. return allImgUrl;
  247. }
  248. /**
  249. * 拼接背景图片字符串 (url('xxxx/xxx.png'))
  250. */
  251. const spliceBgImgUrl = imgUrl => {
  252. if (!imgUrl)
  253. return 'url()'
  254. let allImgUrl = spliceAssetsUrl(imgUrl)
  255. return 'url(' + allImgUrl + ')';
  256. }
  257. //解析用户对象格式
  258. const parseUser = userData => {
  259. let memberAccount = userData
  260. if (memberAccount) {
  261. // 复制属性名字 accountId
  262. memberAccount.memberInfo.accountId = memberAccount.memberInfo.id
  263. if (memberAccount.memberInfo) {
  264. memberAccount.memberInfo['mobile'] = memberAccount['mobile']
  265. if (memberAccount.wxThirdUser) {
  266. memberAccount.memberInfo['openId'] = memberAccount.wxThirdUser['openId']
  267. memberAccount.memberInfo['uuid'] = memberAccount.wxThirdUser['uuid']
  268. memberAccount.memberInfo['unionId'] = memberAccount.wxThirdUser['unionId']
  269. memberAccount.memberInfo['channelCode'] = memberAccount.wxThirdUser['channelCode']
  270. memberAccount.memberInfo['blog'] = memberAccount.wxThirdUser['blog']
  271. memberAccount.memberInfo['clientId'] = memberAccount.wxThirdUser['clientId']
  272. }
  273. }
  274. } else {
  275. memberAccount = {}
  276. memberAccount['memberInfo'] = undefined
  277. console.error('[parseUser]用户登录信息memberAccount返回为null')
  278. }
  279. /* 删除无用的字段 */
  280. //delete memberAccount.memberInfo.id
  281. delete memberAccount.memberInfo.$_createBy
  282. delete memberAccount.memberInfo.$_createTime
  283. delete memberAccount.memberInfo.$_updateBy
  284. delete memberAccount.memberInfo.$_updateTime
  285. delete memberAccount.memberInfo.delFlag
  286. delete memberAccount.memberInfo.params
  287. delete memberAccount.memberInfo.searchValue
  288. delete memberAccount.memberInfo.memberAccount
  289. uni.setStorageSync(__config.loginWxUserInfoKey, memberAccount.memberInfo)
  290. /* 返回解析后的用户信息 */
  291. return memberAccount.memberInfo;
  292. }
  293. /**
  294. * 跳转页面
  295. * @param {Object} page
  296. */
  297. const navigateToPage = function(page, success) {
  298. if (!page) {
  299. return
  300. }
  301. const pages = getCurrentPages();
  302. let curRoute = getCurPage(pages)
  303. console.info('[navigateToPage]_当前路由页面: ', curRoute)
  304. console.info('[navigateToPage]_将要跳转页面: ', page)
  305. if (page.substr(0, 1) == "/") {
  306. curRoute = `/${curRoute}`
  307. }
  308. if (curRoute == page) {
  309. console.log('[navigateToPage]_路由相同: 不进行跳转')
  310. return
  311. }
  312. uni.navigateTo({
  313. url: page,
  314. success: (res) => {
  315. if (success) {
  316. success(res)
  317. }
  318. uni.$emit('reloadSocket')
  319. }
  320. })
  321. }
  322. /**
  323. * 跳转页面
  324. * @param {Object} page
  325. */
  326. const reLaunchToPage = function(page, success) {
  327. if (!page) {
  328. return
  329. }
  330. const pages = getCurrentPages();
  331. let curRoute = getCurPage(pages)
  332. console.info('[reLaunchToPage]_当前路由页面: ', curRoute)
  333. console.info('[reLaunchToPage]_将要跳转页面: ', page)
  334. if (page.substr(0, 1) == "/") {
  335. curRoute = `/${curRoute}`
  336. }
  337. if (curRoute == page) {
  338. console.log('[reLaunchToPage]_路由相同: 不进行跳转')
  339. return
  340. }
  341. uni.reLaunch({
  342. url: page,
  343. success: (res) => {
  344. if (success) {
  345. success(res)
  346. }
  347. uni.$emit('reloadSocket')
  348. }
  349. })
  350. }
  351. /**
  352. * 弹出普通提示窗
  353. * @param {Object} title
  354. */
  355. const showToast = function(title) {
  356. if (!title) {
  357. return
  358. }
  359. uni.showToast({
  360. title: title,
  361. })
  362. }
  363. /**
  364. * 加载网络字体文件
  365. * @param {Object} family
  366. * @param {Object} source
  367. */
  368. const loadFontFace = function(family, source) {
  369. uni.loadFontFace({
  370. family: family,
  371. source: `url("${spliceAssetsUrl(source)}")`,
  372. success: function(res) {
  373. console.log(`load font ${family} from ${spliceAssetsUrl(source)} success!`)
  374. },
  375. fail: function(err) {
  376. console.error(err)
  377. }
  378. })
  379. }
  380. /**
  381. * px 转换 rpx
  382. * @param {Object} px
  383. */
  384. const pxTorpx = function(px) {
  385. let deviceWidth = uni.getSystemInfoSync().windowWidth; //获取设备屏幕宽度
  386. let rpx = (750 / deviceWidth) * Number(px)
  387. return Math.floor(rpx);
  388. };
  389. /**
  390. * rpx 转换 px
  391. * @param {Object} rpx
  392. */
  393. const rpxTopx = function(rpx) {
  394. let deviceWidth = wx.getSystemInfoSync().windowWidth; //获取设备屏幕宽度
  395. let px = (deviceWidth / 750) * Number(rpx)
  396. return Math.floor(px);
  397. };
  398. /**
  399. * 处理优惠卷方法
  400. */
  401. const couponProces = function(e,list){
  402. if(list.type == 1 || list.types == 1){
  403. // 满减卷
  404. console.log(list)
  405. console.log(e)
  406. let price = new Decimal(e).sub(new Decimal(list.reduceAmount)).toFixed(2, Decimal.ROUND_HALF_UP)
  407. let lists = {
  408. price: price < 0 ? 0 : price,
  409. reduceAmount: price < 0 ? e : list.reduceAmount
  410. }
  411. return lists
  412. }else if(list.type == 2){
  413. let lists = {
  414. price: new Decimal(e).mul(new Decimal(list.discount).div(new Decimal(10))).toFixed(2, Decimal.ROUND_HALF_UP),
  415. reduceAmount: new Decimal(e).sub(new Decimal(e).mul(new Decimal(list.discount).div(new Decimal(10))).toFixed(2, Decimal.ROUND_HALF_UP))
  416. }
  417. return lists
  418. }else{
  419. return false
  420. }
  421. };
  422. /**
  423. * 处理第二份半价方法
  424. */
  425. const secondHalfPrice = function(e,shoppingCardList,spuIdsList){
  426. let price = new Decimal(e)
  427. let reduceAmount = new Decimal(0)
  428. console.log(price)
  429. for (var i = 0; i < shoppingCardList.length; i++) {
  430. if(spuIdsList.includes(shoppingCardList[i].spuId)){
  431. // 商品数量向下取整看有几份可第二份半价的
  432. let length = new Decimal(shoppingCardList[i].quantity).div(2).toFixed(0, Decimal.ROUND_DOWN)
  433. // 计算出总共优惠多少这个商品
  434. let prices = new Decimal(length).mul(new Decimal(shoppingCardList[i].goodsSku.salesPrice).div(2))
  435. reduceAmount = reduceAmount.add(prices)
  436. }
  437. }
  438. price = price.sub(reduceAmount.toFixed(2, Decimal.ROUND_HALF_UP))
  439. let lists = {
  440. price: price.toNumber(),
  441. reduceAmount: reduceAmount.toNumber().toFixed(2, Decimal.ROUND_HALF_UP)
  442. }
  443. console.log(lists)
  444. return lists
  445. };
  446. /**
  447. * 根据比例计算rpx值
  448. * @param {Object} rpxVal 比例为3时的值
  449. * @param {Object} offset 偏移量,支持负数
  450. */
  451. const calcPixelRatio = function(rpxVal, offset = 0){
  452. let defaultPR = new Decimal(3).div(new Decimal(rpxVal)).toNumber();
  453. let devicePixelRatio = uni.$u.devicePixelRatio;
  454. if(devicePixelRatio == 3){
  455. return rpxVal
  456. }
  457. if(devicePixelRatio == 1 || Number(rpxVal) <= 0){
  458. return rpxVal + offset
  459. }else{
  460. let newRpxVal = new Decimal(devicePixelRatio).div(new Decimal(defaultPR)).toNumber()
  461. let newRpxValAndOffset = new Decimal(newRpxVal).add(new Decimal(offset)).toNumber();
  462. return newRpxValAndOffset
  463. }
  464. };
  465. const getUuid = function(){
  466. var s = [];
  467. var hexDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  468. for (var i = 0; i < 36; i++) {
  469. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  470. }
  471. s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  472. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  473. s[8] = s[13] = s[18] = s[23] = "";
  474. var uuid = s.join("");
  475. return uuid;
  476. };
  477. module.exports = {
  478. formatTime: formatTime,
  479. filterForm: filterForm,
  480. getCurrentPageUrlWithArgs: getCurrentPageUrlWithArgs,
  481. getUrlParam: getUrlParam,
  482. isWeiXinBrowser: isWeiXinBrowser,
  483. isMiniPg: isMiniPg,
  484. resetPageUrl: resetPageUrl,
  485. getClientCode: getClientCode,
  486. imgUrlToBase64: imgUrlToBase64,
  487. setH5ShareUrl: setH5ShareUrl,
  488. getCurPage: getCurPage,
  489. saveSharerUserCode: saveSharerUserCode,
  490. dataAddSharerUserCode: dataAddSharerUserCode,
  491. backLoginPage: backLoginPage,
  492. setH5HomeShareUrl,
  493. spliceAssetsUrl,
  494. spliceBgImgUrl,
  495. parseUser,
  496. navigateToPage,
  497. reLaunchToPage,
  498. getCurrentPageOptions,
  499. showToast,
  500. loadFontFace,
  501. pxTorpx,
  502. rpxTopx,
  503. getUuid,
  504. couponProces,
  505. secondHalfPrice,
  506. calcPixelRatio
  507. };