filter.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * 公共方法的封装
  3. */
  4. import Vue from 'vue'
  5. // 性别
  6. Vue.filter("filterSex", function(val) {
  7. if(val == 1){
  8. return '男';
  9. }else if(val == 2){
  10. return '女';
  11. }else {
  12. return '未知';
  13. }
  14. });
  15. // 链接加时间戳
  16. Vue.filter("timestamp", function(link) {
  17. return link + '?t=' + new Date().getTime();
  18. });
  19. // 保留两位小数,自动补充零
  20. Vue.filter("filterToFixed", function(val) {
  21. var value=Math.round(parseFloat(val)*100)/100;
  22. if(!value){return '0'}
  23. var xsd=value.toString().split(".");
  24. if(xsd.length==1){
  25. value=value.toString()+".00";
  26. return value;
  27. }
  28. if(xsd.length>1){
  29. if(xsd[1].length<2){
  30. value=value.toString()+"0";
  31. }
  32. return value;
  33. }
  34. })
  35. // 订单状态
  36. Vue.filter("filterOrderState", function(val) {
  37. let orderList = ['待支付', '','超时取消', '待使用', '退款中', '己退款', '退款失败', '己使用', '己超期','己关闭']
  38. return orderList[val]
  39. });
  40. // 个人二维码状态
  41. Vue.filter("filterSingleState", function(val) {
  42. let list = ['未使用', '已使用','作废']
  43. return list[val]
  44. });
  45. // 退款状态
  46. Vue.filter("filterRefundState", function(val) {
  47. console.log('filterRefundState',val);
  48. if(!val){
  49. return false
  50. }
  51. let list = ['申请中', '退款成功','退款失败', '退款中']
  52. return list[val]
  53. });
  54. // 支付状态
  55. Vue.filter("filterPayState", function(val) {
  56. let orderList = ['未支付', '已支付','支付中', '支付失败', '支付退款']
  57. return orderList[val]
  58. });
  59. // 结账方式
  60. Vue.filter("filterPayType", function(val) {
  61. let payTypeList = ['', '微信', '余额', '积分','农商行支付'];
  62. return payTypeList[val]
  63. });
  64. // 物流状态
  65. Vue.filter("filterDilevery", function(val) {
  66. let dileveryList = ['下单', '揽件', '运输中', '派件中', '签收', '退回', '拒签']
  67. return dileveryList[val]
  68. });
  69. // 退款状态
  70. Vue.filter("filterRefundState", function(val) {
  71. let rundStateList = ['申请中', '退款成功', '退款失败', '退款中']
  72. return rundStateList[val]
  73. });
  74. // 评分文字
  75. Vue.filter("rateText", function(val) {
  76. let rateList = ['很差', '差', '一般', '很好', '非常好']
  77. return rateList[val-1]
  78. });
  79. // 支持的物流方式
  80. Vue.filter("filterLogisticsType", function(val) {
  81. let logisticsType = ['邮寄', '自提', '同城配送']
  82. return logisticsType[val]
  83. });
  84. // js手机号码中间4位变星星
  85. Vue.filter("hidePhoneNumber", function(val) {
  86. return val?val.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2"):'';
  87. });
  88. // 保质期转换
  89. Vue.filter("filterPeriod", function(day) {
  90. if (!day) return;
  91.   const unitObj = {
  92.     年: 365,
  93.     月: 30,
  94.     日: 1
  95.   };
  96.   for (const key in unitObj) {
  97.     if (day % unitObj[key] === 0) {
  98.       return `${day / unitObj[key]}${key}`;
  99.     }
  100.   }
  101. });
  102. // 身份证加密显示
  103. Vue.filter('maskID', function(value) {
  104. if (value.length != 18) {
  105. // 如果身份证号码不是18位,直接返回原始值
  106. return value;
  107. }
  108. const front = value.slice(0, 6); // 取身份证号码前6位
  109. const tail = value.slice(-4); // 取身份证号码后4位
  110. const masked = '*'.repeat(value.length - 10); // 中间部分用星号替代
  111. return front + masked + tail;
  112. });
  113. // 判断如果是今天就返回今天,如果是明天就返回明天如果都不是就返回星期几
  114. Vue.filter('checkDate', function(date) {
  115. let today = new Date(); // 获取今天的日期
  116. date = new Date(date);
  117. if (date.getFullYear() === today.getFullYear() &&
  118. date.getMonth() === today.getMonth() &&
  119. date.getDate() === today.getDate()) {
  120. return "今天";
  121. } else if (date.getFullYear() === today.getFullYear() &&
  122. date.getMonth() === today.getMonth() &&
  123. date.getDate() === today.getDate() + 1) {
  124. return "明天";
  125. } else {
  126. let days = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
  127. let diffDays = Math.floor((date.getTime() - today.getTime()) / (1000 * 3600 * 24));
  128. if (diffDays > 7) {
  129. // return date.toLocaleDateString("zh-CN");
  130. return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
  131. } else {
  132. return days[date.getDay()];
  133. }
  134. }
  135. });
  136. // 判断如果是今天就返回今天,如果是明天就返回明天如果都不是就返回星期几
  137. Vue.filter('checkWeekDate', function(date) {
  138. let today = new Date(); // 获取今天的日期
  139. date = new Date(date);
  140. if (date.getFullYear() === today.getFullYear() &&
  141. date.getMonth() === today.getMonth() &&
  142. date.getDate() === today.getDate()) {
  143. return "今天";
  144. } else if (date.getFullYear() === today.getFullYear() &&
  145. date.getMonth() === today.getMonth() &&
  146. date.getDate() === today.getDate() + 1) {
  147. return "明天";
  148. } else {
  149. let days = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
  150. let diffDays = Math.floor((date.getTime() - today.getTime()) / (1000 * 3600 * 24));
  151. return days[date.getDay()];
  152. }
  153. });