index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import statusManager from '@/utils/statusManager.js';
  2. // 日期格式化原型扩展(单独维护)
  3. Date.prototype.Format = function (fmt) {
  4. const o = {
  5. "M+": this.getMonth() + 1, // 月份
  6. "D+": this.getDate(), // 日
  7. "h+": this.getHours(), // 小时
  8. "m+": this.getMinutes(), // 分
  9. "s+": this.getSeconds(), // 秒
  10. "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
  11. "S": this.getMilliseconds() // 毫秒
  12. };
  13. if (/(Y+)/.test(fmt)) {
  14. fmt = fmt.replace(
  15. RegExp.$1,
  16. (this.getFullYear() + "").substr(4 - RegExp.$1.length)
  17. );
  18. }
  19. for (const k in o) {
  20. if (new RegExp(`(${k})`).test(fmt)) {
  21. fmt = fmt.replace(
  22. RegExp.$1,
  23. RegExp.$1.length === 1
  24. ? o[k]
  25. : (`00${o[k]}`).substr((o[k] + "").length)
  26. );
  27. }
  28. }
  29. return fmt;
  30. };
  31. // 数据格式化工具方法
  32. const formatUtil = {
  33. formatTime(time, pattern) {
  34. // uview: uni.$u.date('1585926095536', 'yyyy-mm')
  35. // 过滤器: '1585926095536' | date('yyyy-mm')
  36. const times = time * 1000;
  37. let d = new Date(times).Format("YYYY-MM-DD hh:mm:ss");
  38. if (pattern) {
  39. d = new Date(times).Format(pattern);
  40. }
  41. return d.toLocaleString();
  42. },
  43. // 格式化距离
  44. formatDistance(value) {
  45. if (value == null) return '-';
  46. const numValue = parseFloat(value);
  47. if (isNaN(numValue)) return '无效距离';
  48. if (numValue < 0.001) return '<1m';
  49. if (numValue < 1) return `${Math.round(numValue * 1000)}m`;
  50. return numValue % 1 === 0
  51. ? `${numValue}km`
  52. : `${numValue.toFixed(2)}km`;
  53. },
  54. // 格式化销售数
  55. formatSales(sales) {
  56. if (typeof sales !== 'number' || !isFinite(sales)) return '0';
  57. // 处理负数
  58. sales = Math.max(0, Math.floor(sales));
  59. if (sales < 100) return `${sales}`;
  60. if (sales < 1000) return `${Math.floor(sales / 100) * 100}+`;
  61. if (sales < 10000) return `${Math.floor(sales / 1000)}k+`;
  62. return '1w+';
  63. },
  64. // 格式化评价星数
  65. formatRating(val) {
  66. if (val >= 4.5) return "超赞";
  67. if (val >= 4.0) return "满意";
  68. if (val >= 3.0) return "一般";
  69. if (val >= 2.0) return "不满意";
  70. return "非常差";
  71. },
  72. // 格式化数额差值
  73. formatSubtract(num1, num2) {
  74. const len1 = (num1.toString().split('.')[1] || '').length;
  75. const len2 = (num2.toString().split('.')[1] || '').length;
  76. const base = Math.pow(10, Math.max(len1, len2));
  77. return (Math.round(num1 * base) - Math.round(num2 * base)) / base;
  78. },
  79. };
  80. // 其他工具方法
  81. const otherUtil = {
  82. getQueryStr(name) {
  83. let search = window.location.search;
  84. const hash = window.location.hash;
  85. if (hash.includes('?')) search = hash.split('?')[1]
  86. const params = new URLSearchParams(search);
  87. const val = params.get(name);
  88. return val === null ? null : decodeURIComponent(val)
  89. },
  90. // 以token判断是否登录
  91. checkLogin(options = {}) {
  92. const {
  93. type,
  94. modalText = '请登录账号后再操作',
  95. } = options;
  96. const token = uni.getStorageSync('access-token') || '';
  97. //const token = (uni.getStorageSync('wx_phone') && uni.getStorageSync('access-token')) || '';
  98. const isLogin = !!token;
  99. // 后退参数
  100. let str = uni.$u.queryParams({ isBack: 1 });
  101. // 跳转路由
  102. let path = `/pages/myNew/phone${str}`;
  103. //let path = `/pages/login/wxLogin${str}`;
  104. // 未登录时,根据 type 执行对应操作
  105. if (!isLogin) {
  106. switch (type) {
  107. case 'navigate-webview':
  108. // 跳转登录页内嵌
  109. uni.navigateTo({
  110. url: path+'&type=webview',
  111. success: () => {
  112. console.log('未登录,已跳转至登录页');
  113. }
  114. });
  115. break;
  116. case 'navigate':
  117. // 跳转登录页(带返回跳转,可返回原页面)
  118. uni.navigateTo({
  119. url: path,
  120. success: () => {
  121. console.log('未登录,已跳转至登录页');
  122. }
  123. });
  124. break;
  125. case 'modal':
  126. // 弹窗提示,确认后跳转登录页
  127. uni.showModal({
  128. title: '温馨提示',
  129. content: modalText,
  130. confirmText: '前往登录',
  131. confirmColor: '#0879FF',
  132. success: (res) => {
  133. if (res.confirm) {
  134. uni.navigateTo({ url: path });
  135. }
  136. }
  137. });
  138. break;
  139. // 默认:仅返回登录状态,不执行额外操作
  140. default:
  141. console.log('未登录,仅返回登录状态');
  142. break;
  143. }
  144. }
  145. return isLogin;
  146. },
  147. // 获取多级对象的长链
  148. getProp(obj, path, defaultValue = '') {
  149. const keys = path.split('.');
  150. let result = obj;
  151. for (const key of keys) {
  152. if (result && typeof result === 'object' && key in result) {
  153. result = result[key];
  154. } else {
  155. return defaultValue;
  156. }
  157. }
  158. return result;
  159. },
  160. // 获取多图拼接的值
  161. getSplitImg(e, index = 0) {
  162. // 先处理原始数据:若e为空,返回空数组
  163. if (!e || typeof e !== 'string') {
  164. return [];
  165. }
  166. // 分割图片字符串为数组
  167. const imgArr = e.split(',');
  168. // 判断index是否为null/undefined(用户传空时),返回完整数组
  169. if (index === null || index === undefined) {
  170. return imgArr;
  171. }
  172. // 处理索引:确保是整数,且在有效范围内(0 <= index < 数组长度)
  173. const validIndex = Math.floor(Number(index)); // 转为整数(避免传入字符串/小数)
  174. if (isNaN(validIndex) || validIndex < 0 || validIndex >= imgArr.length) {
  175. return ''; // 索引无效时,返回空字符串(避免显示错误图片)
  176. }
  177. // 返回指定索引的图片URL
  178. return imgArr[validIndex];
  179. },
  180. // 电话
  181. makePhoneCall(e) {
  182. if (e) {
  183. uni.makePhoneCall({ phoneNumber: e });
  184. }
  185. },
  186. // 全局状态管理
  187. statusManager,
  188. };
  189. // 组合导出
  190. export default {
  191. ...formatUtil,
  192. ...otherUtil,
  193. };