index.js 6.4 KB

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