filters.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. // import { logout, logoffConfirm } from "@/api/services/login.js";
  2. // import { getUserInfo } from "@/api/services/members.js";
  3. import storage from "@/utils/storage.js";
  4. // import Vue from "vue";
  5. // import { getCurrentInstance } from 'vue'
  6. // 获取当前组件实例
  7. // const { proxy } = getCurrentInstance()
  8. import Foundation from "./Foundation.js";
  9. /**
  10. * 金钱单位置换 2999 --> 2,999.00
  11. * @param val
  12. * @param unit
  13. * @param location
  14. * @returns {*}
  15. */
  16. export function unitPrice (val, unit, location) {
  17. if (!val) val = 0;
  18. let price = Foundation.formatPrice(val);
  19. if (location === "before") {
  20. return price.substr(0, price.length - 3);
  21. }
  22. if (location === "after") {
  23. return price.substr(-2);
  24. }
  25. return (unit || "") + price;
  26. }
  27. /**
  28. * 格式化价格 1999 --> [1999,00]
  29. * @param {*} val
  30. * @returns
  31. */
  32. export function goodsFormatPrice (val) {
  33. if (typeof val == "undefined") {
  34. return val;
  35. }
  36. let valNum = new Number(val);
  37. return valNum.toFixed(2).split(".");
  38. }
  39. /**
  40. * 将内容复制到粘贴板
  41. */
  42. import { h5Copy } from "@/utils/js_sdk/h5-copy/h5-copy.js";
  43. export function setClipboard (val) {
  44. // #ifdef H5
  45. if (val === null || val === undefined) {
  46. val = "";
  47. } else val = val + "";
  48. const result = h5Copy(val);
  49. if (result === false) {
  50. uni.showToast({
  51. title: "不支持",
  52. });
  53. } else {
  54. uni.showToast({
  55. title: "复制成功",
  56. icon: "none",
  57. });
  58. }
  59. // #endif
  60. // #ifndef H5
  61. uni.setClipboardData({
  62. data: val,
  63. success: function () {
  64. uni.showToast({
  65. title: "复制成功!",
  66. duration: 2000,
  67. icon: "none",
  68. });
  69. },
  70. });
  71. // #endif
  72. }
  73. /**
  74. * 拨打电话
  75. */
  76. export function callPhone (phoneNumber) {
  77. uni.makePhoneCall({
  78. phoneNumber: phoneNumber,
  79. });
  80. }
  81. /**
  82. * 脱敏姓名
  83. */
  84. export function noPassByName (str) {
  85. if (null != str && str != undefined) {
  86. if (str.length <= 3) {
  87. return "*" + str.substring(1, str.length);
  88. } else if (str.length > 3 && str.length <= 6) {
  89. return "**" + str.substring(2, str.length);
  90. } else if (str.length > 6) {
  91. return str.substring(0, 2) + "****" + str.substring(6, str.length);
  92. }
  93. } else {
  94. return "";
  95. }
  96. }
  97. /**
  98. * 处理unix时间戳,转换为可阅读时间格式
  99. * @param unix
  100. * @param format
  101. * @returns {*|string}
  102. */
  103. export function unixToDate (unix, format) {
  104. let _format = format || "yyyy-MM-dd hh:mm:ss";
  105. const d = new Date(unix * 1000);
  106. const o = {
  107. "M+": d.getMonth() + 1,
  108. "d+": d.getDate(),
  109. "h+": d.getHours(),
  110. "m+": d.getMinutes(),
  111. "s+": d.getSeconds(),
  112. "q+": Math.floor((d.getMonth() + 3) / 3),
  113. S: d.getMilliseconds(),
  114. };
  115. if (/(y+)/.test(_format))
  116. _format = _format.replace(
  117. RegExp.$1,
  118. (d.getFullYear() + "").substr(4 - RegExp.$1.length)
  119. );
  120. for (const k in o)
  121. if (new RegExp("(" + k + ")").test(_format))
  122. _format = _format.replace(
  123. RegExp.$1,
  124. RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
  125. );
  126. return _format;
  127. }
  128. /**
  129. * 人性化显示时间
  130. *
  131. * @param {Object} datetime
  132. */
  133. export function beautifyTime (datetime = "") {
  134. if (datetime == null || datetime == undefined || !datetime) {
  135. return "";
  136. }
  137. datetime = timestampToTime(datetime).replace(/-/g, "/");
  138. let time = new Date();
  139. let outTime = new Date(datetime);
  140. if (/^[1-9]\d*$/.test(datetime)) {
  141. outTime = new Date(parseInt(datetime) * 1000);
  142. }
  143. if (time.getTime() < outTime.getTime()) {
  144. return parseTime(outTime, "{y}/{m}/{d}");
  145. }
  146. if (time.getFullYear() != outTime.getFullYear()) {
  147. return parseTime(outTime, "{y}/{m}/{d}");
  148. }
  149. if (time.getMonth() != outTime.getMonth()) {
  150. return parseTime(outTime, "{m}/{d}");
  151. }
  152. if (time.getDate() != outTime.getDate()) {
  153. let day = outTime.getDate() - time.getDate();
  154. if (day == -1) {
  155. return parseTime(outTime, "昨天 {h}:{i}");
  156. }
  157. if (day == -2) {
  158. return parseTime(outTime, "前天 {h}:{i}");
  159. }
  160. return parseTime(outTime, "{m}-{d}");
  161. }
  162. if (time.getHours() != outTime.getHours()) {
  163. return parseTime(outTime, "{h}:{i}");
  164. }
  165. let minutes = outTime.getMinutes() - time.getMinutes();
  166. if (minutes == 0) {
  167. return "刚刚";
  168. }
  169. minutes = Math.abs(minutes);
  170. return `${minutes}分钟前`;
  171. }
  172. // 时间转换
  173. function timestampToTime (timestamp) {
  174. var date = new Date(timestamp);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
  175. var Y = date.getFullYear() + '-';
  176. var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  177. var D = date.getDate() + ' ';
  178. var h = date.getHours() + ':';
  179. var m = date.getMinutes() + ':';
  180. var s = date.getSeconds();
  181. return Y + M + D + h + m + s;
  182. }
  183. /**
  184. * 13888888888 -> 138****8888
  185. * @param mobile
  186. * @returns {*}
  187. */
  188. export function secrecyMobile (mobile) {
  189. mobile = String(mobile);
  190. if (!/\d{11}/.test(mobile)) {
  191. return mobile;
  192. }
  193. return mobile.replace(/(\d{3})(\d{4})(\d{4})/, "$1****$3");
  194. }
  195. /**
  196. * 人性化时间显示
  197. *
  198. * @param {Object} datetime
  199. */
  200. export function formatTime (datetime) {
  201. if (datetime == null) return "";
  202. datetime = datetime.replace(/-/g, "/");
  203. let time = new Date();
  204. let outTime = new Date(datetime);
  205. if (/^[1-9]\d*$/.test(datetime)) {
  206. outTime = new Date(parseInt(datetime) * 1000);
  207. }
  208. if (
  209. time.getTime() < outTime.getTime() ||
  210. time.getFullYear() != outTime.getFullYear()
  211. ) {
  212. return parseTime(outTime, "{y}-{m}-{d} {h}:{i}");
  213. }
  214. if (time.getMonth() != outTime.getMonth()) {
  215. return parseTime(outTime, "{m}-{d} {h}:{i}");
  216. }
  217. if (time.getDate() != outTime.getDate()) {
  218. let day = outTime.getDate() - time.getDate();
  219. if (day == -1) {
  220. return parseTime(outTime, "昨天 {h}:{i}");
  221. }
  222. if (day == -2) {
  223. return parseTime(outTime, "前天 {h}:{i}");
  224. }
  225. return parseTime(outTime, "{m}-{d} {h}:{i}");
  226. }
  227. if (time.getHours() != outTime.getHours()) {
  228. return parseTime(outTime, "{h}:{i}");
  229. }
  230. let minutes = outTime.getMinutes() - time.getMinutes();
  231. if (minutes == 0) {
  232. return "刚刚";
  233. }
  234. minutes = Math.abs(minutes);
  235. return `${minutes}分钟前`;
  236. }
  237. /**
  238. * 时间格式化方法
  239. *
  240. * @param {(Object|string|number)} time
  241. * @param {String} cFormat
  242. * @returns {String | null}
  243. */
  244. export function parseTime (time, cFormat) {
  245. if (arguments.length === 0) {
  246. return null;
  247. }
  248. let date;
  249. const format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}";
  250. if (typeof time === "object") {
  251. date = time;
  252. } else {
  253. if (typeof time === "string" && /^[0-9]+$/.test(time)) {
  254. time = parseInt(time);
  255. }
  256. if (typeof time === "number" && time.toString().length === 10) {
  257. time = time * 1000;
  258. console.log("时间判断为number");
  259. }
  260. date = new Date(time.replace(/-/g, "/"));
  261. }
  262. const formatObj = {
  263. y: date.getFullYear(),
  264. m: date.getMonth() + 1,
  265. d: date.getDate(),
  266. h: date.getHours(),
  267. i: date.getMinutes(),
  268. s: date.getSeconds(),
  269. a: date.getDay(),
  270. };
  271. const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
  272. const value = formatObj[key];
  273. // Note: getDay() returns 0 on Sunday
  274. if (key === "a") {
  275. return ["日", "一", "二", "三", "四", "五", "六"][value];
  276. }
  277. return value.toString().padStart(2, "0");
  278. });
  279. return time_str;
  280. }
  281. /**
  282. * 清除逗号
  283. *
  284. */
  285. export function clearStrComma (str) {
  286. str = str.replace(/,/g, ""); //取消字符串中出现的所有逗号
  287. return str;
  288. }
  289. /**
  290. * 判断用户是否登录
  291. * @param val 如果为auth则判断是否登录
  292. * 如果传入 auth 则为判断是否登录
  293. */
  294. export function isLogin (val) {
  295. let userInfo = storage.getUserInfo();
  296. if (val == "auth") {
  297. return userInfo && userInfo.id ? true : false;
  298. } else {
  299. return storage.getUserInfo();
  300. }
  301. }
  302. /**
  303. * 退出登录
  304. *
  305. */
  306. // export function quiteLoginOut () {
  307. // uni.showModal({
  308. // title: "提示",
  309. // content: "是否退出登录?",
  310. // confirmColor: '#ff3c2a',
  311. // async success (res) {
  312. // if (res.confirm) {
  313. // storage.setAccessToken("");
  314. // // storage.setRefreshToken("");
  315. // storage.setUserInfo({});
  316. // storage.setHasLogin(false)
  317. // navigateToLogin("redirectTo");
  318. // await logout();
  319. // }
  320. // },
  321. // });
  322. // }
  323. /**
  324. * 用户注销
  325. *
  326. */
  327. // export function logoff () {
  328. // uni.showModal({
  329. // title: "提示",
  330. // content: "确认注销用户么?注销用户将无法再次登录并失去当前数据。",
  331. // confirmColor: getCurrentInstance.proxy.$mainColor,
  332. // async success (res) {
  333. // if (res.confirm) {
  334. // await logoffConfirm();
  335. // storage.setAccessToken("");
  336. // storage.setRefreshToken("");
  337. // storage.setUserInfo({});
  338. // navigateToLogin("redirectTo");
  339. // }
  340. // },
  341. // });
  342. // }
  343. /**
  344. * 跳转im
  345. */
  346. /* export function talkIm (storeId, goodsId, id) {
  347. if (isLogin('auth')) {
  348. let url = `/pages/mine/im/index?userId=${storeId}`
  349. if(goodsId && id) url = `/pages/mine/im/index?userId=${storeId}&goodsid=${goodsId}&skuid=${id}`
  350. uni.navigateTo({
  351. url
  352. });
  353. }
  354. else {
  355. tipsToLogin()
  356. }
  357. } */
  358. /* export function tipsToLogin (type) {
  359. if (!isLogin("auth")) {
  360. uni.showModal({
  361. title: "提示",
  362. content: "当前用户未登录是否登录?",
  363. confirmText: "确定",
  364. cancelText: "取消",
  365. confirmColor: getCurrentInstance.proxy.$mainColor,
  366. success: (res) => {
  367. if (res.confirm) {
  368. navigateToLogin();
  369. } else if (res.cancel) {
  370. if(type !== 'normal'){
  371. uni.navigateBack();
  372. }
  373. }
  374. },
  375. });
  376. return false;
  377. }
  378. return true;
  379. } */
  380. /**
  381. * 获取用户信息并重新添加到缓存里面
  382. */
  383. export async function userInfo () {
  384. let res = await getUserInfo();
  385. if (res.data.success) {
  386. storage.setUserInfo(res.data.result);
  387. return res.data.result;
  388. }
  389. }
  390. /**
  391. * 验证是否登录如果没登录则去登录
  392. * @param {*} val
  393. * @returns
  394. */
  395. export function forceLogin () {
  396. let userInfo = storage.getUserInfo();
  397. if (!userInfo || !userInfo.id) {
  398. // #ifdef MP-WEIXIN
  399. uni.navigateTo({
  400. url: "/pages/passport/wechatMPLogin",
  401. });
  402. // #endif
  403. // #ifndef MP-WEIXIN
  404. uni.navigateTo({
  405. url: "/pages/passport/login",
  406. });
  407. // #endif
  408. }
  409. }
  410. /**
  411. * 获取当前加载的页面对象
  412. * @param val
  413. */
  414. export function getPages (val) {
  415. const pages = getCurrentPages(); //获取加载的页面
  416. const currentPage = pages[pages.length - 1]; //获取当前页面的对象
  417. const url = currentPage.route; //当前页面url
  418. return val ? currentPage : url;
  419. }
  420. /**
  421. * 跳转到登录页面
  422. */
  423. export function navigateToLogin (type = "navigateTo") {
  424. /**
  425. * 此处进行条件编译判断
  426. * 微信小程序跳转到微信小程序登录页面
  427. * H5/App跳转到普通登录页面
  428. */
  429. // #ifdef MP-WEIXIN
  430. uni[type]({
  431. url: "/pages/passport/wechatMPLogin",
  432. });
  433. // #endif
  434. // #ifndef MP-WEIXIN
  435. uni[type]({
  436. url: "/pages/login/index",
  437. });
  438. // #endif
  439. }
  440. /**
  441. * 服务状态列表
  442. */
  443. export function serviceStatusList (val) {
  444. let statusList = {
  445. APPLY: "申请售后",
  446. PASS: "通过售后",
  447. REFUSE: "拒绝售后",
  448. BUYER_RETURN: "买家退货,待卖家收货",
  449. SELLER_RE_DELIVERY: "商家换货/补发",
  450. SELLER_CONFIRM: "卖家确认收货",
  451. SELLER_TERMINATION: "卖家终止售后",
  452. BUYER_CONFIRM: "买家确认收货",
  453. BUYER_CANCEL: "买家取消售后",
  454. WAIT_REFUND: "等待平台退款",
  455. COMPLETE: "完成售后",
  456. };
  457. return statusList[val];
  458. }
  459. /**
  460. * 订单状态列表
  461. */
  462. export function orderStatusList (val) {
  463. let orderStatusList = {
  464. UNDELIVERED: "待发货",
  465. UNPAID: "未付款",
  466. PAID: "已付款",
  467. PARTS_DELIVERED: "部分发货",
  468. DELIVERED: "已发货",
  469. CANCELLED: "已取消",
  470. COMPLETED: "已完成",
  471. COMPLETE: "已完成",
  472. TAKE: "待核验",
  473. STAY_PICKED_UP: "待自提",
  474. };
  475. return orderStatusList[val];
  476. }