filters.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import { oss_baseUrl } from '@/config'; // oss 图片地址
  2. import dayjs from 'dayjs';
  3. /**
  4. * 时间戳转时间-过滤器
  5. * 时间格式:2018-01-01 14:16:13
  6. */
  7. function dateFilter(val) {
  8. let time = parseInt(val) * 1000;
  9. return dayjs(time).format('YYYY-MM-DD HH:mm:ss');
  10. }
  11. /**
  12. * 标准时间转时间时间-过滤器
  13. * 时间格式:2018-01-01 14:16:13
  14. */
  15. function timeFilter(val) {
  16. return dayjs(val).format('YYYY-MM-DD HH:mm:ss');
  17. // let d = new Date(val);
  18. // let datetime = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate();
  19. // return datetime;
  20. }
  21. /**
  22. * 标准时间转时间时间-过滤器
  23. * 时间格式:2018-01-01
  24. */
  25. function dateOnlyFilter(val) {
  26. return dayjs(val).format('YYYY-MM-DD');
  27. }
  28. /**
  29. * 时间戳转标准时间-过滤器
  30. * 时间格式:2018-01-01 14:16:13
  31. */
  32. function ConvertTime(timestamp) {
  33. let date = new Date(timestamp);
  34. let Y = date.getFullYear() + '-';
  35. let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  36. let D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
  37. let h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':';
  38. let m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':';
  39. let s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());
  40. return Y + M + D + h + m + s;
  41. }
  42. /**
  43. * 审核状态过滤器
  44. * @return {string}
  45. */
  46. function Examine(val) {
  47. let str = '';
  48. if (val === '1') {
  49. str = '待审核';
  50. } else if (val === '2') {
  51. str = '审核失败';
  52. } else if (val === '3') {
  53. str = '审核通过';
  54. }
  55. return str;
  56. }
  57. /**
  58. * 时间转标准时间-过滤器
  59. * 时间格式:2018-01-01 14:16:13 =》标准时间
  60. */
  61. function parserDate(date) {
  62. let t = Date.parse(date);
  63. if (!isNaN(t)) {
  64. return new Date(Date.parse(date.replace(/-/g, '/')));
  65. } else {
  66. return new Date();
  67. }
  68. }
  69. /**
  70. * 货币格式化-过滤器
  71. * (1234,2) => ¥1234.00
  72. * (1234,2,$) => $1234.00
  73. * (1234,2,'') => 1234.00
  74. */
  75. function moneyFmt(value, decimals = 2, symbol = '¥') {
  76. const val = parseFloat(value);
  77. if (isNaN(val)) {
  78. return value;
  79. }
  80. const money = val.toFixed(decimals);
  81. return `${symbol}${money}`;
  82. }
  83. /**
  84. * 日期格式化-过滤器
  85. * 根据format格式化日期str
  86. * 2018-01-01 14:16:13 (yyyy/MM/dd hh-mm-ss) => 2018/01/01 14-16-13
  87. * 2018-01-01 14:16:13 (yy/M/d h-m) => 18/01/01 14-16
  88. * 2018-01-01 14:16:13 (yyMMdd) => 180101
  89. * 2018-01-01 (yyyyMM) => 201801
  90. * 14:16:13 (hh:mm) => 14:16
  91. */
  92. function dateFmt(value, format = 'YYYY-MM-dd hh:mm:ss') {
  93. if (!value) {
  94. return;
  95. }
  96. const dateObj = parse(value);
  97. const year = dateObj.year;
  98. const month = dateObj.month;
  99. const day = dateObj.day;
  100. const hour = dateObj.hour;
  101. const minute = dateObj.minute;
  102. const second = dateObj.second;
  103. let str = format;
  104. str = str.replace(/yyyy|YYYY/, year);
  105. str = str.replace(/yy|YY/, year % 100 > 9 ? (year % 100).toString() : '0' + (year % 100));
  106. str = str.replace(/MM|M/g, month);
  107. str = str.replace(/dd|DD|d|D/g, day);
  108. str = str.replace(/hh|HH|h|H/g, hour);
  109. str = str.replace(/mm|m/g, minute);
  110. str = str.replace(/ss|SS|s|S/g, second);
  111. return str;
  112. // 解析日期string->{year,month...},参数格式可以是datetime/date/time
  113. function parse(dateStr) {
  114. const dateObj = {
  115. year: '',
  116. month: '',
  117. day: '',
  118. hour: '',
  119. minute: '',
  120. second: ''
  121. };
  122. if (typeof dateStr !== 'string') {
  123. return dateObj;
  124. }
  125. const arr = dateStr.split(' ');
  126. if (arr.length > 1) {
  127. // datetime
  128. const _date = arr[0].split('-');
  129. dateObj.year = _date[0];
  130. dateObj.month = _date[1];
  131. dateObj.day = _date[2];
  132. const _time = arr[1].split(':');
  133. dateObj.hour = _time[0];
  134. dateObj.minute = _time[1];
  135. dateObj.second = _time[2];
  136. } else if (arr.length === 1) {
  137. // date or time
  138. if (arr[0].indexOf('-') > -1) {
  139. // date
  140. const _date = arr[0].split('-');
  141. dateObj.year = _date[0];
  142. dateObj.month = _date[1];
  143. dateObj.day = _date[2];
  144. } else if (arr[0].indexOf(':') > -1) {
  145. // time
  146. const _time = arr[0].split(':');
  147. dateObj.hour = _time[0];
  148. dateObj.minute = _time[1];
  149. dateObj.second = _time[2];
  150. }
  151. }
  152. return dateObj;
  153. }
  154. }
  155. /**
  156. * 字符串长度溢出格式化-过滤器
  157. */
  158. function ellipsis(value, len) {
  159. if (typeof value !== 'string') {
  160. value = value.toString();
  161. }
  162. return value.length > len ? value.substr(0, len) + '...' : value;
  163. }
  164. /**
  165. * 中间变星号
  166. * phone 手机号
  167. * bank 银行卡
  168. * IDcode 身份证
  169. */
  170. function star(value, type) {
  171. if (!value) {
  172. return;
  173. }
  174. if (type === 'phone') {
  175. return value.substr(0, 3) + '****' + value.substr(7);
  176. } else if (type === 'bank') {
  177. return '************' + value.substr(12);
  178. } else if (type === 'IDcode') {
  179. return value.substr(0, 1) + '****************' + value.substr(17);
  180. }
  181. }
  182. /**
  183. * 距离值格式化-过滤器
  184. * 854 => '<854km'
  185. * 1234 => '<1.2km'
  186. * 5678 => '<5.7km'
  187. */
  188. function distanceFmt(value) {
  189. value = parseFloat(value);
  190. let str = '';
  191. if (value < 1000) {
  192. str = `${value.toFixed(0)}m`;
  193. } else {
  194. str = `${(value / 1000).toFixed(1)}km`;
  195. }
  196. return str;
  197. }
  198. /**
  199. * 静态图片组装格式化-过滤器
  200. * name 图片名称
  201. * path 图片路径 默认static
  202. * type 图片格式 (jpg,png)
  203. */
  204. function imageFmt(name, type = 'png', path = 'static') {
  205. return `${oss_baseUrl}/${path}/${name}.${type}`;
  206. }
  207. // 时间截取转换
  208.  function timeClipFmt(value) {
  209.     var time = value.replace("T", " ");
  210.     return time.slice(0, 19);
  211. };
  212. // 时间转换
  213.  function timeChange(value) {
  214.     var time = value.replace("T", " ");
  215.     return time.slice(0, 19);
  216. };
  217. export default {
  218. moneyFmt,
  219. dateFmt,
  220. Examine,
  221. timeFilter,
  222. dateFilter,
  223. dateOnlyFilter,
  224. parserDate,
  225. ellipsis,
  226. ConvertTime,
  227. star,
  228. distanceFmt,
  229. imageFmt,
  230. timeClipFmt
  231. };