helper.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /* eslint-disable */
  2. import mRouter from '@/utils/router';
  3. import mConstDataConfig from '@/config/constData.config';
  4. import mStore from '@/store';
  5. import appShare from '@/utils/share';
  6. // #ifdef H5
  7. import jweixin from '@/common/jweixin';
  8. import $mPayment from '@/utils/payment';
  9. // #endif
  10. import { http } from '@/utils/request';
  11. import { advView } from '@/api/basic';
  12. //常用方法集合
  13. export default {
  14. /**
  15. * toast提示
  16. */
  17. toast(title, duration = 3000, mask = false, icon = 'none') {
  18. if (Boolean(title) === false) {
  19. return;
  20. }
  21. uni.showToast({
  22. title,
  23. duration,
  24. mask,
  25. icon
  26. });
  27. },
  28. /**
  29. * 返回登录页面
  30. */
  31. async backToLogin() {
  32. // 存当前页面的地址
  33. const currentPage = getCurrentPages()[getCurrentPages().length - 1];
  34. const params = {};
  35. // #ifdef H5
  36. params.route = `/${currentPage.$vm.route}`;
  37. params.query = currentPage.$vm.$mp && currentPage.$vm.$mp.query;
  38. // #endif
  39. // #ifdef MP
  40. params.route = `/${currentPage.$vm.__route__}`;
  41. params.query = currentPage.$vm.$mp && currentPage.$vm.$mp.query;
  42. // #endif
  43. // #ifdef APP-PLUS
  44. params.route = `/${currentPage.route}`;
  45. params.query = currentPage.options;
  46. // #endif
  47. uni.setStorageSync('backToPage', JSON.stringify(params));
  48. uni.removeTabBarBadge({ index: mConstDataConfig.cartIndex });
  49. uni.removeTabBarBadge({ index: mConstDataConfig.notifyIndex });
  50. await mStore.commit('logout');
  51. mRouter.push({ route: '/pages/public/logintype' });
  52. },
  53. /**
  54. * 返回上一页携带参数
  55. */
  56. prePage(index) {
  57. let pages = getCurrentPages();
  58. let prePage = pages[pages.length - (index || 2)];
  59. // #ifdef H5
  60. return prePage;
  61. // #endif
  62. return prePage.$vm;
  63. },
  64. /**
  65. * 开发环境全局打印日志
  66. * @param {Object} title
  67. */
  68. log(title) {
  69. if (process.env.NODE_ENV === 'development' && Boolean(title) === true) {
  70. console.log(JSON.stringify(title));
  71. }
  72. },
  73. /**
  74. * 异步获取设备信息
  75. */
  76. getInfoAsync() {
  77. return new Promise((resolve, reject) => {
  78. plus.device.getInfo({
  79. success(e) {
  80. resolve(e);
  81. },
  82. fail(e) {
  83. reject(e.message);
  84. }
  85. });
  86. });
  87. },
  88. /**
  89. * 安卓10不支持IMEI,则获取OAID
  90. */
  91. getOaidAsync() {
  92. return new Promise((resolve, reject) => {
  93. plus.device.getOAID({
  94. success(e) {
  95. resolve(e);
  96. },
  97. fail(e) {
  98. reject(e.message);
  99. }
  100. });
  101. });
  102. },
  103. /**
  104. * 获取一个随机数
  105. * @param {Object} min
  106. * @param {Object} max
  107. */
  108. random(min, max) {
  109. switch (arguments.length) {
  110. case 1:
  111. return parseInt(Math.random() * min + 1, 10);
  112. break;
  113. case 2:
  114. return parseInt(Math.random() * (max - min + 1) + min, 10);
  115. break;
  116. default:
  117. return 0;
  118. break;
  119. }
  120. },
  121. /**
  122. * 获取ios的IDFA
  123. */
  124. getIdfa() {
  125. let idfa = '';
  126. try {
  127. if ('iOS' == plus.os.name) {
  128. let manager = plus.ios.invoke('ASIdentifierManager', 'sharedManager');
  129. if (plus.ios.invoke(manager, 'isAdvertisingTrackingEnabled')) {
  130. let identifier = plus.ios.invoke(manager, 'advertisingIdentifier');
  131. idfa = plus.ios.invoke(identifier, 'UUIDString');
  132. plus.ios.deleteObject(identifier);
  133. }
  134. plus.ios.deleteObject(manager);
  135. }
  136. } catch (e) {
  137. console.error('获取idfa失败');
  138. }
  139. return idfa;
  140. },
  141. /*
  142. * obj 转 params字符串参数
  143. * 例子:{a:1,b:2} => a=1&b=2
  144. */
  145. objParseParam(obj) {
  146. let paramsStr = '';
  147. if (obj instanceof Array) return paramsStr;
  148. if (!(obj instanceof Object)) return paramsStr;
  149. for (let key in obj) {
  150. paramsStr += `${key}=${obj[key]}&`;
  151. }
  152. return paramsStr.substring(0, paramsStr.length - 1);
  153. },
  154. /*
  155. * obj 转 路由地址带参数
  156. * 例子:{a:1,b:2} => /pages/index/index?a=1&b=2
  157. */
  158. objParseUrlAndParam(path, obj) {
  159. let url = path || '/';
  160. let paramsStr = '';
  161. if (obj instanceof Array) return url;
  162. if (!(obj instanceof Object)) return url;
  163. paramsStr = this.objParseParam(obj);
  164. paramsStr && (url += '?');
  165. url += paramsStr;
  166. return url;
  167. },
  168. /*
  169. * 获取url字符串参数
  170. */
  171. getRequestParameters(locationhref) {
  172. let href = locationhref || '';
  173. let theRequest = new Object();
  174. let str = href.split('?')[1];
  175. if (str != undefined) {
  176. let strs = str.split('&');
  177. for (let i = 0; i < strs.length; i++) {
  178. theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
  179. }
  180. }
  181. return theRequest;
  182. },
  183. /**
  184. * 加密字符串
  185. */
  186. strEncode(str) {
  187. const key = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  188. let l = key.length;
  189. let a = key.split('');
  190. let s = '',
  191. b,
  192. b1,
  193. b2,
  194. b3;
  195. for (let i = 0; i < str.length; i++) {
  196. b = str.charCodeAt(i);
  197. b1 = b % l;
  198. b = (b - b1) / l;
  199. b2 = b % l;
  200. b = (b - b2) / l;
  201. b3 = b % l;
  202. s += a[b3] + a[b2] + a[b1];
  203. }
  204. return s;
  205. },
  206. /**
  207. * 解密字符串
  208. */
  209. strDecode(str) {
  210. const key = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  211. let l = key.length;
  212. let b,
  213. b1,
  214. b2,
  215. b3,
  216. d = 0,
  217. s;
  218. s = new Array(Math.floor(str.length / 3));
  219. b = s.length;
  220. for (let i = 0; i < b; i++) {
  221. b1 = key.indexOf(str.charAt(d));
  222. d++;
  223. b2 = key.indexOf(str.charAt(d));
  224. d++;
  225. b3 = key.indexOf(str.charAt(d));
  226. d++;
  227. s[i] = b1 * l * l + b2 * l + b3;
  228. }
  229. b = eval('String.fromCharCode(' + s.join(',') + ')');
  230. return b;
  231. },
  232. /**
  233. * 比较版本号
  234. */
  235. compareVersion(reqV, curV) {
  236. if (curV && reqV) {
  237. let arr1 = curV.split('.'),
  238. arr2 = reqV.split('.');
  239. let minLength = Math.min(arr1.length, arr2.length),
  240. position = 0,
  241. diff = 0;
  242. while (
  243. position < minLength &&
  244. (diff = parseInt(arr1[position]) - parseInt(arr2[position])) == 0
  245. ) {
  246. position++;
  247. }
  248. diff = diff != 0 ? diff : arr1.length - arr2.length;
  249. if (diff > 0) {
  250. if (position == minLength - 1) {
  251. return 1;
  252. } else {
  253. return 2;
  254. }
  255. } else {
  256. return 0;
  257. }
  258. } else {
  259. return 0;
  260. }
  261. },
  262. /**
  263. * H5复制
  264. */
  265. h5Copy(content) {
  266. let textarea = document.createElement('textarea');
  267. textarea.value = content;
  268. textarea.readOnly = 'readOnly';
  269. document.body.appendChild(textarea);
  270. textarea.select(); // 选择对象
  271. textarea.setSelectionRange(0, content.length); //核心
  272. let result = document.execCommand('Copy'); // 执行浏览器复制命令
  273. textarea.remove();
  274. const msg = result ? '复制成功' : '复制失败';
  275. this.toast(msg);
  276. },
  277. /**
  278. * app分享
  279. */
  280. handleAppShare(shareUrl, shareTitle, shareContent, shareImg) {
  281. let shareData = {
  282. shareUrl,
  283. shareTitle,
  284. shareContent,
  285. shareImg
  286. };
  287. appShare(shareData, res => {});
  288. },
  289. async handleWxH5Share(title, desc, link, imgUrl) {
  290. // #ifdef H5
  291. console.log("进入到分享的js里面")
  292. if ($mPayment.isWechat()) {
  293. if (uni.getSystemInfoSync().platform === 'android') {
  294. }
  295. //await $mPayment.wxConfigH5(link);
  296. jweixin.checkJsApi({
  297. jsApiList: ['chooseImage'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
  298. success: function(res) {
  299. // 以键值对的形式返回,可用的api值true,不可用为false
  300. // 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
  301. console.log("输出返回的数据是啥"+res)
  302. }
  303. });
  304. jweixin.ready(function () {
  305. console.log("进入到微信分享里面的东西了")
  306. // eslint-disable-next-line
  307. jweixin.updateAppMessageShareData({
  308. title, // 分享标题
  309. desc, // 分享描述
  310. link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  311. imgUrl, // 分享图标
  312. success: function () {
  313. // 用户确认分享后执行的回调函数
  314. },
  315. cancel: function () {
  316. // 用户取消分享后执行的回调函数
  317. }
  318. });
  319. // eslint-disable-next-line
  320. jweixin.updateTimelineShareData({
  321. title, // 分享标题
  322. desc, // 分享描述
  323. link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  324. imgUrl, // 分享图标
  325. success: function () {
  326. // 用户确认分享后执行的回调函数
  327. },
  328. cancel: function () {
  329. // 用户取消分享后执行的回调函数
  330. }
  331. });
  332. });
  333. }
  334. // #endif
  335. },
  336. // 去掉字符串中的空格
  337. trim(str){
  338. if (!str) {
  339. return '';
  340. }
  341. return str.replace(/\s*/g,'');
  342. },
  343. // 判断两个对象是否相同
  344. isObjectValueEqual(x, y) {
  345. // 指向同一内存时
  346. if (x === y) {
  347. return true;
  348. } else if (
  349. typeof x == 'object' &&
  350. x != null &&
  351. typeof y == 'object' && y != null
  352. ) {
  353. if (Object.keys(x).length != Object.keys(y).length) return false;
  354. for (var prop in x) {
  355. if (y.hasOwnProperty(prop)) {
  356. if (!this.isObjectValueEqual(x[prop], y[prop])) return false;
  357. } else return false;
  358. }
  359. return true;
  360. } else return false;
  361. },
  362. platformGroupFilter () {
  363. let platformGroup = 'tinyShop';
  364. // #ifdef H5
  365. if ($mPayment.isWechat()) {
  366. platformGroup = 'tinyShopWechat';
  367. } else {
  368. platformGroup = 'tinyShopH5';
  369. }
  370. // #endif
  371. // #ifdef MP-QQ
  372. platformGroup = 'tinyShopQqMp';
  373. // #endif
  374. // #ifdef MP-WEIXIN
  375. platformGroup = 'tinyShopWechatMp';
  376. // #endif
  377. // #ifdef MP-ALIPAY
  378. platformGroup = 'tinyShopAliMp';
  379. // #endif
  380. // #ifdef MP-QQ
  381. platformGroup = 'tinyShopQqMp';
  382. // #endif
  383. // #ifdef MP-BAIDU
  384. platformGroup = 'tinyShopBaiduMp';
  385. // #endif
  386. // #ifdef APP-PLUS
  387. switch(uni.getSystemInfoSync().platform){
  388. case 'android':
  389. platformGroup = 'tinyShopAndroid';
  390. break;
  391. case 'ios':
  392. platformGroup = 'tinyShopIos';
  393. break;
  394. }
  395. // #endif
  396. return platformGroup;
  397. },
  398. // 广告图跳转封装
  399. handleBannerNavTo(data, id, advId) {
  400. let url;
  401. http.get(advView, { id: advId });
  402. switch (data) {
  403. case 'notify_announce_view': // 公告详情
  404. url = `/pages/index/notice/detail?id=${id}`;
  405. break;
  406. case 'product_view': // 产品详情
  407. url = `/pages/product/product?id=${id}`;
  408. break;
  409. case 'combination_view': // 某分类下产品列表
  410. url = `/pages/marketing/combination/list?id=${id}`;
  411. break;
  412. case 'coupon_view': // 优惠券详情
  413. url = `/pages/user/coupon/detail?id=${id}`;
  414. break;
  415. case 'helper_view': // 站点帮助详情
  416. url = '/pages/set/helper/index';
  417. break;
  418. case 'bargain_list': // 砍价列表
  419. url = '/pages/marketing/bargain/list';
  420. break;
  421. case 'discount_list': // 限时折扣
  422. url = '/pages/marketing/discount/list';
  423. break;
  424. case 'group_buy_list': // 团购列表
  425. url = '/pages/marketing/group/list';
  426. break;
  427. case 'wholesale_list': // 拼团列表
  428. url = '/pages/marketing/wholesale/list';
  429. break;
  430. case 'product_list_for_cate': // 某分类下产品列表
  431. url = `/pages/product/list?cate_id=${id}`;
  432. break;
  433. case 'mini_program_live_view': // 跳转至带货直播间
  434. // #ifdef MP-WEIXIN
  435. url = `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${[id]}`;
  436. // #endif
  437. // #ifndef MP-WEIXIN
  438. this.toast('请从微信小程序进入直播间');
  439. // #endif
  440. break;
  441. default:
  442. break;
  443. }
  444. if (url) {
  445. mRouter.push({ route: url });
  446. }
  447. }
  448. };