123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- import { round } from './libs/function/digit.js'
- const debug = process.env.NODE_ENV === 'development';
- function priceFormat(number, decimals = 0, decimalPoint = '.', thousandsSeparator = ',') {
- number = (`${number}`).replace(/[^0-9+-Ee.]/g, '');
- const n = !isFinite(+number) ? 0 : +number;
- const prec = !isFinite(+decimals) ? 0 : Math.abs(decimals);
- const sep = (typeof thousandsSeparator === 'undefined') ? ',' : thousandsSeparator;
- const dec = (typeof decimalPoint === 'undefined') ? '.' : decimalPoint;
- let s = '';
- s = (prec ? round(n, prec) + '' : `${Math.round(n)}`).split('.');
- const re = /(-?\d+)(\d{3})/;
- while (re.test(s[0])) {
- s[0] = s[0].replace(re, `$1${sep}$2`);
- }
- if ((s[1] || '').length < prec) {
- s[1] = s[1] || '';
- s[1] += new Array(prec - s[1].length + 1).join('0');
- }
- return s.join(dec);
- }
- export default {
- computed: {
-
- value() {
- const {
- text,
- mode,
- format,
- href
- } = this
-
- if (mode === 'price') {
-
- if (!/^\d+(\.\d+)?$/.test(text)) {
- if (debug) console.error('金额模式下,text参数需要为金额格式');
- }
-
- if (uni.$u.test.func(format)) {
-
- return format(text)
- }
-
- return priceFormat(text, 2)
- }
- if (mode === 'date') {
-
- if (!uni.$u.test.date(text)) {
- if (debug) console.error('日期模式下,text参数需要为日期或时间戳格式');
- }
-
- if (uni.$u.test.func(format)) {
-
- return format(text);
- }
- if (format) {
-
- return uni.$u.timeFormat(text, format);
- }
-
- return uni.$u.timeFormat(text, 'yyyy-mm-dd');
- }
- if (mode === 'phone') {
-
- if (uni.$u.test.func(format)) {
-
- return format(text);
- }
- if (format === 'encrypt') {
-
- return `${text.substr(0, 3)}****${text.substr(7)}`;
- }
- return text
- }
- if (mode === 'name') {
-
- if (!(typeof(text) === 'string')) {
- if (debug) console.error('姓名模式下,text参数需要为字符串格式');
- }
- if (uni.$u.test.func(format)) {
-
- return format(text)
- }
- if (format === 'encrypt') {
-
- return this.formatName(text);
- }
- return text
- }
- if (mode === 'link') {
-
- if (!uni.$u.test.url(href)) {
- if (debug) console.error('超链接模式下,href参数需要为URL格式');
- }
- return text;
- }
- return text;
- }
- },
- methods: {
-
- formatName(name) {
- let value = '';
- if (name.length === 2) {
- value = name.substr(0, 1) + '*';
- } else if (name.length > 2) {
- let char = '';
- for (let i = 0, len = name.length - 2; i < len; i++) {
- char += '*';
- }
- value = name.substr(0, 1) + char + name.substr(-1, 1);
- } else {
- value = name;
- }
- return value;
- }
- }
- }
|