Blame view

common/filters.js 1.49 KB
eb4f2617   chenbiao   add filters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  // 为空处理  return ‘-’
  const tranNull = value => {
  	if (value === null || value === undefined || value === '') return '-'
  	return value
  }
  
  
  /*
      @切割字符串
      @str原字符串
      @num字符位数
  */
  const cutString = (str, num) => {
  	var _str = "";
  	if (str == null) {
  		return "";
  	}
  	if (str.length >= num) {
  		var strN = str.substring(0, num);
  		strN += "...";
  		_str = strN;
  	} else {
  		_str = str;
  	}
  	return _str;
  }
  
  // 钱前面加“¥”
  const RMB = (value) => {
  	if (value === '--' || value === null || value === undefined) return value
  	return `¥${value}`
  }
  
3b7af3a2   chenbiao   add 我的钱包 接口联调
34
  // 钱保留两位小数
eb4f2617   chenbiao   add filters
35
  const toFixed2 = num => {
3b7af3a2   chenbiao   add 我的钱包 接口联调
36
  	return isNaN(num) ? 0.00 : parseFloat((num/100).toFixed(2));
eb4f2617   chenbiao   add filters
37
38
  }
  
b398dc4a   chenbiao   add 我的钱包 接口联调
39
40
41
42
43
44
45
  // 钱保留两位小数+其他
  const ortherToFixed = num => {
  	if(num === -1) return '其他';
  	return isNaN(num) ? 0.00 : parseFloat((num/100).toFixed(2));
  }
  
  
eb4f2617   chenbiao   add filters
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  // 数字每三位加个逗号
  const toThousands =(num)=> {
      var result = [],
        counter = 0;
      num = (num || 0).toString().split('');
      for (var i = num.length - 1; i >= 0; i--) {
        counter++;
        result.unshift(num[i]);
        if (!(counter % 3) && i != 0) {
          result.unshift(',');
        }
      }
      return result.join('');
    }
   
   // 根据身份证号码判断男女
   const sexFilter = value => {
   	if (!value) {
   	    return ''
   	} else {
   	    let data = value.substr(16, 1) % 2 === 1 ? '男' : '女'
   	    return data
   	}
   }
  
  export default {
  	tranNull,
  	RMB,
  	cutString,
  	toFixed2,
b398dc4a   chenbiao   add 我的钱包 接口联调
76
  	ortherToFixed,
eb4f2617   chenbiao   add filters
77
78
79
  	toThousands,
  	sexFilter,
  }