util.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import {
  2. chatUrl
  3. } from '../config/setting.js'
  4. import {
  5. getToken,
  6. getSycCache,
  7. getSign,
  8. } from '../config/cache.js'
  9. import request from '../config/request.js'
  10. import store from '../store/index.js'
  11. /**
  12. * 防抖动节流
  13. * @param {Object} func
  14. * @param {Object} limit
  15. */
  16. function throttle(func, limit) {
  17. console.log('节流请求1')
  18. let inThrottle = false;
  19. if (!inThrottle) {
  20. const args = arguments;
  21. const context = this;
  22. inThrottle = true;
  23. setTimeout(() => {
  24. console.log('结束请求')
  25. inThrottle = false;
  26. }, limit);
  27. console.log('节流请求2')
  28. return func.apply(context, args);
  29. } else {
  30. return false;
  31. }
  32. }
  33. /**
  34. * 时间格式化
  35. * @param {Object} time
  36. */
  37. function formatTime(time, type = 0) {
  38. if (typeof time !== 'number' || time < 0) {
  39. return time
  40. }
  41. var hour = parseInt(time / 3600)
  42. time = time % 3600
  43. var minute = parseInt(time / 60)
  44. time = time % 60
  45. var second = time
  46. return (type == 1 ? [hour, minute, second] : [minute, second]).map(function(n) {
  47. n = n.toString()
  48. return n[1] ? n : '0' + n
  49. }).join(':')
  50. }
  51. /**
  52. * 坐标格式化
  53. * @param {Object} longitude
  54. * @param {Object} latitude
  55. */
  56. function formatLocation(longitude, latitude) {
  57. if (typeof longitude === 'string' && typeof latitude === 'string') {
  58. longitude = parseFloat(longitude)
  59. latitude = parseFloat(latitude)
  60. }
  61. longitude = longitude.toFixed(2)
  62. latitude = latitude.toFixed(2)
  63. return {
  64. longitude: longitude.toString().split('.'),
  65. latitude: latitude.toString().split('.')
  66. }
  67. }
  68. /**
  69. * 日期格式化
  70. */
  71. var dateUtils = {
  72. UNITS: {
  73. '年': 31557600000,
  74. '月': 2629800000,
  75. '天': 86400000,
  76. '小时': 3600000,
  77. '分钟': 60000,
  78. '秒': 1000
  79. },
  80. humanize: function(milliseconds) {
  81. var humanize = '';
  82. for (var key in this.UNITS) {
  83. if (milliseconds >= this.UNITS[key]) {
  84. humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
  85. break;
  86. }
  87. }
  88. return humanize || '刚刚';
  89. },
  90. format: function(dateStr) {
  91. var date = this.parse(dateStr)
  92. var diff = Date.now() - date.getTime();
  93. if (diff < this.UNITS['天']) {
  94. return this.humanize(diff);
  95. }
  96. var _format = function(number) {
  97. return (number < 10 ? ('0' + number) : number);
  98. };
  99. return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDate()) + '-' +
  100. _format(date.getHours()) + ':' + _format(date.getMinutes());
  101. },
  102. parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象
  103. var a = str.split(/[^0-9]/);
  104. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
  105. }
  106. };
  107. /**
  108. * 获取当前日期
  109. */
  110. const getDate = (time = 0, type = 1) => {
  111. var date = time > 0 ? (new Date(time)) : (new Date());
  112. var year = date.getFullYear();
  113. var month = date.getMonth() + 1;
  114. var day = date.getDate();
  115. if (type == 1) {
  116. return year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day);
  117. } else {
  118. return year + '年' + (month < 10 ? '0' + month : month) + '月' + (day < 10 ? '0' + day : day) + '日';
  119. }
  120. }
  121. /**
  122. * 页面跳转
  123. * @param {*} url
  124. * @param {*} type
  125. */
  126. const gotoPage = (url, type = '') => {
  127. if (!url || url.length == 0) {
  128. return false;
  129. }
  130. if (url.substr(0, 1) !== '/') {
  131. url = '/' + url;
  132. }
  133. let p = url;
  134. if (url.indexOf('?') != -1) {
  135. // #ifdef APP-PLUS
  136. p = url.substr(0, url.indexOf('?'));
  137. // #endif
  138. } else {
  139. // #ifdef H5
  140. if (url.search("app_id") == -1) {
  141. url = url;
  142. }
  143. // #endif
  144. }
  145. if (type == 'redirect') {
  146. uni.navigateTo({
  147. url: url
  148. })
  149. }
  150. if (type == 'reLaunch') {
  151. uni.reLaunch({
  152. url: url
  153. })
  154. }
  155. if (type == 'switchTab') {
  156. uni.switchTab({
  157. url: url
  158. })
  159. }
  160. // 普通页面
  161. uni.navigateTo({
  162. url: url,
  163. fail() {
  164. uni.switchTab({
  165. url: url
  166. })
  167. }
  168. })
  169. }
  170. /**
  171. * 页面跳转
  172. * @param {*} url
  173. * @param {*} type
  174. */
  175. const redirectTo = (url, type = '') => {
  176. console.log(url)
  177. // 普通页面
  178. uni.navigateTo({
  179. url: url
  180. })
  181. }
  182. /**
  183. * 图片压缩预览
  184. */
  185. const getCompressImage = async (url, rate = '') => {
  186. if (rate) {
  187. return url + rate;
  188. } else {
  189. let config = await request.apiGetConfig();
  190. if (typeof(config.img_compress) != 'undefined' && config.img_compress) {
  191. return url + config.img_compress;
  192. }
  193. }
  194. return url;
  195. }
  196. /**
  197. * 去0
  198. */
  199. const removeTrailingZeros = (str) => {
  200. return str ? (str + '').replace(/0+$/, '') : str;
  201. }
  202. export {
  203. formatTime,
  204. formatLocation,
  205. dateUtils,
  206. getDate,
  207. gotoPage,
  208. redirectTo,
  209. throttle,
  210. removeTrailingZeros,
  211. getCompressImage
  212. }