common.js
3.29 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* 显示消息提示框
* @param content 提示的标题
*/
export function toast(content) {
uni.showToast({
icon: 'none',
title: content
})
}
/**
* 显示模态弹窗
* @param content 提示的标题
*/
export function showConfirm(content) {
return new Promise((resolve, reject) => {
uni.showModal({
title: '提示',
content: content,
cancelText: '取消',
confirmText: '确定',
success: function(res) {
resolve(res)
}
})
})
}
/**
* 参数处理
* @param params 参数
*/
export function tansParams(params) {
let result = ''
for (const propName of Object.keys(params)) {
const value = params[propName]
var part = encodeURIComponent(propName) + "="
if (value !== null && value !== "" && typeof (value) !== "undefined") {
if (typeof value === 'object') {
for (const key of Object.keys(value)) {
if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
let params = propName + '[' + key + ']'
var subPart = encodeURIComponent(params) + "="
result += subPart + encodeURIComponent(value[key]) + "&"
}
}
} else {
result += part + encodeURIComponent(value) + "&"
}
}
}
return result
}
// 金额转人民币大写
export function formatCurrencyToChinese(num) {
// 处理空值或非数字
if (!num || isNaN(Number(num))) return '零元整';
// 转换为数字并保留2位小数(避免分以下的单位)
const n = parseFloat(Number(num).toFixed(2));
if (n === 0) return '零元整';
const digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
const unit = ['', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟'];
const decUnit = ['角', '分'];
let str = '';
// 处理整数部分
const integer = Math.floor(n);
// 处理小数部分(角、分)
const decimal = Math.round((n - integer) * 100);
const hasDecimal = decimal > 0;
// 转换整数部分
if (integer > 0) {
let intStr = integer.toString();
for (let i = 0; i < intStr.length; i++) {
const digitNum = parseInt(intStr[i]);
const unitIndex = intStr.length - 1 - i;
if (digitNum !== 0) {
str += digit[digitNum] + unit[unitIndex];
} else {
// 避免连续多个零
if (intStr[i - 1] !== '0' || (unitIndex % 4 === 0 && unitIndex > 0)) {
str += digit[0];
}
}
}
str += '元';
} else {
str += '零元';
}
// 转换小数部分
if (hasDecimal) {
const jiao = Math.floor(decimal / 10);
const fen = decimal % 10;
if (jiao > 0) str += digit[jiao] + decUnit[0];
if (fen > 0) str += digit[fen] + decUnit[1];
} else {
str += '整';
}
// 处理特殊情况(如"零元整"已在开头处理,此处处理多零情况)
return str.replace(/零+/g, '零').replace(/零([万亿])/g, '$1').replace(/零元/, '元')
.replace(/零角零分$/, '整').replace(/零分$/, '整');
}
/**
* 校验邮箱格式
* @param email
* @returns {boolean}
*/
export function isEmail(email) {
const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return reg.test(email);
}