filters.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. import Foundation from "./Foundation.js";
  2. import storage from "@/utils/storage.js";
  3. import { getUserInfo } from "@/api/members";
  4. import Vue from "vue";
  5. /**
  6. * 金钱单位置换 2999 --> 2,999.00
  7. * @param val
  8. * @param unit
  9. * @param location
  10. * @returns {*}
  11. */
  12. export function unitPrice(val, unit, location) {
  13. if (!val) val = 0;
  14. val = Number(val)
  15. let price = Foundation.formatPrice(val);
  16. if (location === "before") {
  17. return price.substr(0, price.length - 3);
  18. }
  19. if (location === "after") {
  20. return price.substr(-2);
  21. }
  22. return (unit || "") + price;
  23. }
  24. export function getStringLength(str) {
  25. if(str == null) return 0;
  26. let emoji_exp = /(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/g;
  27. const ms = [...str.matchAll(emoji_exp)];
  28. console.log(ms)
  29. if (!ms || !ms.length) return str.length;
  30. let emojiSize = 0;
  31. for (const m of ms) emojiSize += m.length - 1;
  32. return str.length - emojiSize;
  33. };
  34. /**
  35. * 格式化价格 1999 --> [1999,00]
  36. * @param {*} val
  37. * @returns
  38. */
  39. export function stationFormatPrice(val) {
  40. if (typeof val == "undefined") {
  41. return val;
  42. }
  43. let valNum = new Number(val);
  44. return valNum.toFixed(2).split(".");
  45. }
  46. /**
  47. * 距离计算
  48. * @param {*} val
  49. * @returns
  50. */
  51. export function calcDistance(lon1, lat1, lon2, lat2) {
  52. let PI = 3.14159265358979323; //圆周率
  53. let R = 6371229; //地球半径
  54. let x, y, distance;
  55. let lonres = lon1 > lon2 ? lon1 - lon2 : lon2 - lon1;
  56. let latres = lat1 > lat2 ? lat1 - lat2 : lat2 - lat1;
  57. x = (lonres * PI * R * Math.cos((((lat1 + lat2) / 2) * PI) / 180)) / 180;
  58. y = ((lat2 - lat1) * PI * R) / 180;
  59. distance = Math.hypot(x, y);
  60. return distance;
  61. }
  62. /**
  63. * 脱敏姓名
  64. */
  65. export function noPassByName(str) {
  66. if (null != str && str != undefined) {
  67. if (str.length <= 3) {
  68. return "*" + str.substring(1, str.length);
  69. } else if (str.length > 3 && str.length <= 6) {
  70. return "**" + str.substring(2, str.length);
  71. } else if (str.length > 6) {
  72. return str.substring(0, 2) + "****" + str.substring(6, str.length);
  73. }
  74. } else {
  75. return "";
  76. }
  77. }
  78. /**
  79. * 处理日期,转换为可阅读时间格式
  80. * @param time
  81. * @param pattern
  82. * @returns {*|string}
  83. */
  84. export function parseTime(time, pattern) {
  85. if (arguments.length === 0 || !time) {
  86. return null;
  87. }
  88. const format = pattern || "{y}-{m}-{d} {h}:{i}:{s}";
  89. let date;
  90. if (typeof time === "object") {
  91. date = time;
  92. } else {
  93. if (typeof time === "string" && /^[0-9]+$/.test(time)) {
  94. time = parseInt(time);
  95. } else if (typeof time === "string") {
  96. // @TODO: 不替换-,可能有问题,待测
  97. time = time.replace(new RegExp(/-/gm), '/')
  98. }
  99. if (typeof time === "number" && time.toString().length === 10) {
  100. time = time * 1000;
  101. }
  102. date = new Date(time);
  103. }
  104. const formatObj = {
  105. y: date.getFullYear(),
  106. m: date.getMonth() + 1,
  107. d: date.getDate(),
  108. h: date.getHours(),
  109. i: date.getMinutes(),
  110. s: date.getSeconds(),
  111. a: date.getDay(),
  112. };
  113. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  114. let value = formatObj[key];
  115. // Note: getDay() returns 0 on Sunday
  116. if (key === "a") {
  117. return ["日", "一", "二", "三", "四", "五", "六"][value];
  118. }
  119. if (result.length > 0 && value < 10) {
  120. value = "0" + value;
  121. }
  122. return value || 0;
  123. });
  124. return time_str;
  125. }
  126. /**
  127. * 处理unix时间戳,转换为可阅读时间格式
  128. * @param unix
  129. * @param format
  130. * @returns {*|string}
  131. */
  132. export function unixToDate(unix, format) {
  133. let _format = format || "yyyy-MM-dd hh:mm:ss";
  134. const d = new Date(unix * 1000);
  135. const o = {
  136. "M+": d.getMonth() + 1,
  137. "d+": d.getDate(),
  138. "h+": d.getHours(),
  139. "m+": d.getMinutes(),
  140. "s+": d.getSeconds(),
  141. "q+": Math.floor((d.getMonth() + 3) / 3),
  142. S: d.getMilliseconds(),
  143. };
  144. if (/(y+)/.test(_format))
  145. _format = _format.replace(
  146. RegExp.$1,
  147. (d.getFullYear() + "").substr(4 - RegExp.$1.length)
  148. );
  149. for (const k in o)
  150. if (new RegExp("(" + k + ")").test(_format))
  151. _format = _format.replace(
  152. RegExp.$1,
  153. RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
  154. );
  155. return _format;
  156. }
  157. /**
  158. * 13888888888 -> 138****8888
  159. * @param mobile
  160. * @returns {*}
  161. */
  162. export function secrecyMobile(mobile) {
  163. mobile = String(mobile);
  164. if (!/\d{11}/.test(mobile)) {
  165. return mobile;
  166. }
  167. return mobile.replace(/(\d{3})(\d{4})(\d{4})/, "$1****$3");
  168. }
  169. /**
  170. * 清除逗号
  171. *
  172. */
  173. export function clearStrComma(str) {
  174. str = str.replace(/,/g, ""); //取消字符串中出现的所有逗号
  175. return str;
  176. }
  177. /**
  178. * 判断用户是否登录
  179. * @param val 如果为auth则判断是否登录
  180. * 如果传入 auth 则为判断是否登录
  181. */
  182. export function isLogin(val) {
  183. let userInfo = storage.getUserInfo();
  184. if (val == "auth") {
  185. // return userInfo && userInfo.userId ? true : false;
  186. return userInfo && userInfo.mobile ? true : false;
  187. } else {
  188. return storage.getUserInfo();
  189. }
  190. }
  191. export function tipsToLogin() {
  192. if (!isLogin("auth")) {
  193. uni.showModal({
  194. title: "提示",
  195. content: "当前用户未登录是否登录?",
  196. confirmText: "确定",
  197. cancelText: "取消",
  198. confirmColor: Vue.prototype.$mainColor,
  199. success: (res) => {
  200. if (res.confirm) {
  201. navigateToLogin();
  202. } else if (res.cancel) {
  203. uni.navigateBack();
  204. }
  205. },
  206. });
  207. return false;
  208. }
  209. return true;
  210. }
  211. /**
  212. * 获取用户信息并重新添加到缓存里面
  213. */
  214. export async function userInfo() {
  215. let res = await getUserInfo();
  216. if (res.data.success) {
  217. storage.setUserInfo(res.data.result);
  218. return res.data.result;
  219. }
  220. }
  221. /**
  222. * 验证是否登录如果没登录则去登录
  223. * @param {*} val
  224. * @returns
  225. */
  226. export function forceLogin() {
  227. let userInfo = storage.getUserInfo();
  228. if (!userInfo || !userInfo.userId) {
  229. // #ifdef MP-WEIXIN || MP-ALIPAY
  230. uni.navigateTo({
  231. url: "/pages/passport/mpLogin",
  232. });
  233. // #endif
  234. // #ifndef MP-WEIXIN || MP-ALIPAY
  235. uni.navigateTo({
  236. url: "/pages/passport/login",
  237. });
  238. // #endif
  239. }
  240. }
  241. /**
  242. * 获取当前加载的页面对象
  243. * @param val
  244. */
  245. export function getPages(val) {
  246. const pages = getCurrentPages(); //获取加载的页面
  247. const currentPage = pages[pages.length - 1]; //获取当前页面的对象
  248. const url = currentPage.route; //当前页面url
  249. return val ? currentPage : url;
  250. }
  251. /**
  252. * 跳转到登录页面
  253. */
  254. export function navigateToLogin(type = "navigateTo", showTip = false) {
  255. /**
  256. * 此处进行条件编译判断
  257. * 微信小程序跳转到微信小程序登录页面
  258. * H5/App跳转到普通登录页面
  259. */
  260. let option = "";
  261. showTip && (option += "?showTip=1");
  262. // #ifdef MP-WEIXIN || MP-ALIPAY
  263. uni[type]({
  264. url: `/pages/passport/mpLogin${option}`,
  265. });
  266. // #endif
  267. // #ifndef MP-WEIXIN || MP-ALIPAY
  268. uni[type]({
  269. url: `/pages/passport/login${option}`,
  270. });
  271. // #endif
  272. }
  273. /**
  274. * 服务状态列表
  275. */
  276. export function serviceStatusList(val) {
  277. let statusList = {
  278. APPLY: "申请售后",
  279. PASS: "通过售后",
  280. REFUSE: "拒绝售后",
  281. BUYER_RETURN: "买家退货,待卖家收货",
  282. SELLER_RE_DELIVERY: "商家换货/补发",
  283. SELLER_CONFIRM: "卖家确认收货",
  284. SELLER_TERMINATION: "卖家终止售后",
  285. BUYER_CONFIRM: "买家确认收货",
  286. BUYER_CANCEL: "买家取消售后",
  287. WAIT_REFUND: "等待平台退款",
  288. COMPLETE: "完成售后",
  289. };
  290. return statusList[val];
  291. }
  292. /**
  293. * 订单状态列表
  294. */
  295. export function orderStatusList(val) {
  296. let orderStatusList = {
  297. UNDELIVERED: "待发货",
  298. UNPAID: "未付款",
  299. PAID: "已付款",
  300. DELIVERED: "已发货",
  301. CANCELLED: "已取消",
  302. COMPLETED: "已完成",
  303. COMPLETE: "已完成",
  304. TAKE: "待核验",
  305. };
  306. return orderStatusList[val];
  307. }
  308. /**
  309. * 对象数组按某一key排序
  310. * @param [] array
  311. * @returns
  312. */
  313. export function arraySort(array, key) {
  314. array.sort(compare(key));
  315. }
  316. /**
  317. * 将null转成0
  318. */
  319. export function transNull(num) {
  320. if(num == null) num = 0;
  321. return num;
  322. }
  323. function compare(key) {
  324. return function (value1, value2) {
  325. var val1 = value1[key];
  326. var val2 = value2[key];
  327. return val1 - val2;
  328. };
  329. }
  330. /**
  331. * 折扣率转换 80 --> 8 85-->8.5
  332. */
  333. export function unitDiscount(val) {
  334. let result = val
  335. if (!result) result = 0;
  336. if(result % 10 === 0)
  337. result = result /10
  338. return result;
  339. }