tools.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import Foundation from '@/utils/Foundation.js';
  2. import {
  3. md5
  4. } from '@/utils/md5.js';
  5. import storage from "@/utils/storage.js";
  6. import store from "@/store/index.js";
  7. // 重新整理一下config
  8. const configHandle = (config) => {
  9. // 'development', 'production'
  10. if (process.env.NODE_ENV === 'development') {
  11. const nonce = Foundation.randomString(6)
  12. const timestamp = parseInt(new Date().getTime() / 1000)
  13. const sign = md5( nonce + timestamp + storage.getAccessToken())
  14. if (config.url.indexOf('?') === -1) {
  15. config.url = `${config.url}?&nonce=${nonce}&timestamp=${timestamp}&sign=${sign}`
  16. } else {
  17. let params = urlParse(config.url);
  18. console.info(params);
  19. let url = config.url.split('?')[0];
  20. params = { ...params,
  21. nonce,
  22. timestamp,
  23. sign
  24. };
  25. let str = '';
  26. for (var k in params) {
  27. console.info(k, params[k])
  28. str += '&' + k + '=' + params[k];
  29. }
  30. str = str.substr(1);
  31. config.url = `${url}?${str}`;
  32. }
  33. config.header = {
  34. ...config.header,
  35. uuid: storage.getUuid()
  36. }
  37. } else {
  38. config.header = {
  39. ...config.header,
  40. Authorization: storage.getAccessToken(),
  41. uuid: storage.getUuid()
  42. }
  43. }
  44. return config
  45. }
  46. /**
  47. * 解析url参数
  48. * @example ?id=12345&a=b
  49. * @return Object {id:12345,a:b}
  50. */
  51. function urlParse(url) {
  52. let obj = {};
  53. let reg = /[?&][^?&]+=[^?&]+/g;
  54. let arr = url.match(reg);
  55. if (arr) {
  56. arr.forEach((item) => {
  57. let tempArr = item.substring(1).split('=');
  58. let key = decodeURIComponent(tempArr[0]);
  59. let val = decodeURIComponent(tempArr.splice(1).join('='));
  60. obj[key] = val;
  61. });
  62. }
  63. return obj;
  64. };
  65. const getNetworkType = () => {
  66. uni.getNetworkType({
  67. success: (res) => {
  68. if (res.networkType === 'none') {
  69. uni.showToast({
  70. title: '网络好像有点问题,请检查后重试!',
  71. duration: 2000,
  72. icon: 'none'
  73. });
  74. let pages = getCurrentPages();
  75. if (pages.length) {
  76. let route = pages[pages.length - 1].route;
  77. if (route !== 'pages/empty/empty') {
  78. uni.navigateTo({
  79. url: `/pages/empty/empty?type=wifi`
  80. })
  81. }
  82. }else{
  83. uni.navigateTo({
  84. url: `/pages/empty/empty?type=wifi`
  85. })
  86. }
  87. }
  88. }
  89. })
  90. }
  91. const throttle = (fn, that, gapTime) => {
  92. // export function throttle(fn, gapTime) {
  93. if (gapTime == null || gapTime == undefined) {
  94. gapTime = 1800
  95. }
  96. let _lastTime = that.lastTime
  97. let _nowTime = +new Date()
  98. if (_nowTime - _lastTime > gapTime || !_lastTime) {
  99. fn.apply(that, arguments) //将this和参数传给原函数
  100. that.lastTime = _nowTime
  101. }
  102. }
  103. /**
  104. * 计算传秒数的倒计时【天、时、分、秒】
  105. * @param seconds
  106. * @returns {{day : *, hours : *, minutes : *, seconds : *}}
  107. */
  108. const countTimeDown = (seconds) => {
  109. const leftTime = (time) => {
  110. if (time < 10) time = '0' + time
  111. return time + ''
  112. }
  113. return {
  114. day: leftTime(parseInt(seconds / 60 / 60 / 24, 10)),
  115. hours: leftTime(parseInt(seconds / 60 / 60 % 24, 10)),
  116. minutes: leftTime(parseInt(seconds / 60 % 60, 10)),
  117. seconds: leftTime(parseInt(seconds % 60, 10))
  118. }
  119. }
  120. /**
  121. * 计算当前时间到第二天0点的倒计时[秒]
  122. * @returns {number}
  123. */
  124. const theNextDayTime = () => {
  125. const nowDate = new Date()
  126. const time = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate() + 1, 0, 0, 0).getTime() -
  127. nowDate.getTime()
  128. return parseInt(time / 1000)
  129. }
  130. export {
  131. //configHandle,
  132. getNetworkType,
  133. throttle,
  134. countTimeDown,
  135. theNextDayTime
  136. }