helper.js 9.2 KB

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