120 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			120 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| // 校验
 | ||
| export const validatePhone = (phone) => {
 | ||
| 	const reg = /^1[3-9]\d{9}$/;
 | ||
| 	return reg.test(phone);
 | ||
| };
 | ||
| 
 | ||
| 
 | ||
| export const getImageUrl = (imagePath) => {
 | ||
| 	if (!imagePath) return ''
 | ||
| 	if (imagePath.startsWith('http')) return imagePath
 | ||
| 
 | ||
| 	// const baseURL = "http://192.168.1.43:3000"
 | ||
| 	// const baseURL = "https://www.zrbjr.com"
 | ||
| 	const baseURL = "https://minio.zrbjr.com"
 | ||
| 
 | ||
| 	// 如果图片路径以/uploads开头,直接返回原路径
 | ||
| 	if (imagePath.startsWith('/uploads')) {
 | ||
| 		return `${baseURL}/jurongquan${imagePath}`
 | ||
| 	}
 | ||
| 
 | ||
| 	return fullUrl
 | ||
| }
 | ||
| 
 | ||
| export const getUserInfo = () => {
 | ||
| 	return uni.getStorageSync("user")
 | ||
| }
 | ||
| 
 | ||
| /**
 | ||
|  * 验证并脱敏电话号码
 | ||
|  * @param {string} phone - 需要验证和脱敏的电话号码
 | ||
|  * @returns {string|boolean} - 脱敏后的电话号码,无效则返回false
 | ||
|  */
 | ||
| export function maskPhoneNumber(phone) {
 | ||
|     // 检查是否为字符串
 | ||
|     if (typeof phone !== 'string') {
 | ||
|         return phone;
 | ||
|     }
 | ||
| 
 | ||
|     // 移除所有非数字字符
 | ||
|     const cleaned = phone.replace(/\D/g, '');
 | ||
| 
 | ||
|     // 验证常见的电话号码格式
 | ||
|     // 支持: 11位手机号(中国大陆)、带区号的固定电话
 | ||
|     const phoneRegex = /^(1[3-9]\d{9})$|^(\d{3,4}-\d{7,8})$|^(\d{3,4}\d{7,8})$/;
 | ||
| 
 | ||
|     if (!phoneRegex.test(cleaned) && !phoneRegex.test(phone)) {
 | ||
|         return phone; // 不是有效的电话号码
 | ||
|     }
 | ||
| 
 | ||
|     // 根据不同格式进行脱敏
 | ||
|     if (cleaned.length === 11) {
 | ||
|         // 手机号: 保留前3位和后4位,中间4位用*代替
 | ||
|         return cleaned.replace(/^(\d{3})(\d{4})(\d{4})$/, '$1****$3');
 | ||
|     } else if (phone.includes('-')) {
 | ||
|         // 带区号的固定电话: 区号不变,号码中间用*代替
 | ||
|         const [areaCode, number] = phone.split('-');
 | ||
|         if (number.length <= 4) {
 | ||
|             return `${areaCode}-****`;
 | ||
|         }
 | ||
|         return `${areaCode}-${number.substr(0, 2)}****${number.substr(-2)}`;
 | ||
|     } else {
 | ||
|         // 不带区号的固定电话
 | ||
|         if (cleaned.length <= 4) {
 | ||
|             return '****';
 | ||
|         }
 | ||
|         return `${cleaned.substr(0, 2)}****${cleaned.substr(-2)}`;
 | ||
|     }
 | ||
| }
 | ||
| 
 | ||
| /**
 | ||
|  * 姓名脱敏处理
 | ||
|  * @param {string} name - 需要脱敏的姓名
 | ||
|  * @returns {string} - 脱敏后的姓名
 | ||
|  */
 | ||
| export function maskName(name) {
 | ||
|     // 检查输入是否为有效字符串
 | ||
|     if (!name || typeof name !== 'string') {
 | ||
|         return '';
 | ||
|     }
 | ||
| 
 | ||
|     // 去除前后空格
 | ||
|     const trimmedName = name.trim();
 | ||
| 
 | ||
|     // 检查是否为英文姓名(包含空格)
 | ||
|     if (trimmedName.includes(' ')) {
 | ||
|         const parts = trimmedName.split(' ').filter(part => part);
 | ||
| 
 | ||
|         // 处理英文名:名全显,姓只显首字母
 | ||
|         if (parts.length >= 2) {
 | ||
|             const firstName = parts.slice(0, -1).join(' ');
 | ||
|             const lastName = parts[parts.length - 1];
 | ||
|             return `${firstName} ${lastName.charAt(0)}*`;
 | ||
|         }
 | ||
|     }
 | ||
| 
 | ||
|     // 处理中文姓名
 | ||
|     const length = trimmedName.length;
 | ||
| 
 | ||
|     switch (length) {
 | ||
|         case 1:
 | ||
|             // 单字名,不脱敏
 | ||
|             return trimmedName;
 | ||
|         case 2:
 | ||
|             // 双字名,隐藏第二个字
 | ||
|             return `${trimmedName[0]}*`;
 | ||
|         case 3:
 | ||
|             // 三字名,隐藏中间字
 | ||
|             return `${trimmedName[0]}*${trimmedName[2]}`;
 | ||
|         case 4:
 | ||
|             // 四字名(如复姓),隐藏中间两个字
 | ||
|             return `${trimmedName[0]}**${trimmedName[3]}`;
 | ||
|         default:
 | ||
|             // 更长的姓名,显示首尾各两个字,中间用*代替
 | ||
|             if (length > 4) {
 | ||
|                 return `${trimmedName.substr(0, 2)}${'*'.repeat(length - 4)}${trimmedName.substr(-2)}`;
 | ||
|             }
 | ||
|     }
 | ||
| 
 | ||
|     return trimmedName;
 | ||
| } |