Files
ks-app-employment-service/utilsRc/common.js
2025-11-03 12:30:37 +08:00

249 lines
7.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* @Descripttion:
* @Author: lip
* @Date: 2023-07-27 15:56:06
* @LastEditors: shirlwang
*/
import {
getPersonBase
} from '@/apiRc/person';
import store from '@/utilsRc/store';
import tab from '@/utilsRc/plugins/tab.js'
/**
* 显示消息提示框
* @param content 提示的标题
*/
export function toast(content) {
uni.showToast({
icon: 'none',
title: content
})
}
/**
* 显示模态弹窗
* @param content 提示的标题
*/
export function showConfirm(content, cancelText, confirmText, showCancel) {
return new Promise((resolve, reject) => {
uni.showModal({
title: '提示',
content: content,
cancelText: cancelText || '取消',
confirmText: confirmText || '确定',
showCancel: showCancel === false ? false : true,
success: function(res) {
resolve(res)
}
})
})
}
// 修改后的 checkPersonBase 方法
export function checkPersonBase() {
return new Promise((resolve) => {
const roles = store.getters.roles;
const userId = store.getters.userId;
const isRole = (role) => roles.includes(role);
if (isRole('qunzhong')) {
getPersonBase(userId).then(resp => {
const shouldGoToPersonInfo = resp.data.auditStatus === '0';
if (shouldGoToPersonInfo) {
uni.showModal({
title: '提示',
content: '您的个人信息尚未维护,请前往维护个人信息。',
confirmText: '去维护',
cancelText: '取消',
success: function(res) {
if (res.confirm) {
tab.navigateTo('/pages/personInfo/index');
}
resolve(false); // 需要维护,返回 false
}
});
} else {
resolve(true); // 不需要维护,返回 true
}
});
} else {
resolve(true); // 非群众角色直接放行
}
});
}
/**
* 参数处理
* @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
}
/**
* 姓名、手机、邮箱、身份证脱敏
* @param str 姓名或手机或邮箱或身份证
* @param type 数据类型
*/
export function publicEncrypt(str, type) {
if (!str) {
return ''
} else if (type == 'name') {
return str.substr(0, 1) + '*'
} else if (type == 'idCard') {
return str.replace(/^(.{6})(?:\d+)(.{4})$/, '$1********$2')
} else if (type == 'phone') {
return str.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
} else if (type == 'email') {
let count, emailArr, emailFront, emailBack;
emailArr = str.split('@');
emailFront = emailArr[0];
count = emailFront.length / 2;
emailFront = emailFront.substring(0, emailFront.length - count);
emailBack = emailArr[1];
return emailFront + '***@' + emailBack;
}
}
/**
* 根据value值筛选对应的label
* @param {Array} options - 原数组
* @param {string | Number} value - 表示
* @returns {string} - 返回后的汉字
*/
export function getLabelByValue(options = [], value, valueKey = 'value', labelKey = 'label') {
for (let i = 0; i < options.length; i++) {
// 动态使用传入的 valueKey 和 labelKey
if (options[i][valueKey] === value) {
return options[i][labelKey];
}
}
return null;
}
/**
* 将数组转换为字符串
* @param {Array} arr - 要转换的数组
* @param {string} delimiter - 用于连接数组元素的分隔符,默认为逗号
* @returns {string} - 转换后的字符串
*/
export function arrayToString(arr, delimiter = ',') {
return arr.join(delimiter);
}
/* 延时函数 */
export function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/* 获取当前年月日 时分秒 */
export function getCurrentDate() {
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始需要加1
const day = String(date.getDate()).padStart(2, '0'); // 使用padStart补零
const hours = String(date.getHours()).padStart(2, '0'); // 补零
const minutes = String(date.getMinutes()).padStart(2, '0'); // 补零
const seconds = String(date.getSeconds()).padStart(2, '0'); // 补零
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
export function processFileUrl(fileUrl) {
let fileList = [];
if (typeof fileUrl === 'string' && fileUrl) {
// 如果 fileUrl 是一个字符串,则按照逗号分割成数组
const urls = fileUrl.split(',').map(url => url.trim()); // 去除每个 URL 前后的空白字符
fileList = urls.map(url => ({
url,
success: 'success',
name: 'fileUrl'
}));
}
return fileList;
}
// 密码强校验
export function isPasswordComplex(password) {
// 长度在8位以上并且包含大小写字母、数字和特殊符号
return /^(?=.*[A-Za-z])(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@#$%^&*!])[A-Za-z\d@#$%^&*!]{8,}$/.test(password);
}
// 手机号码校验
export function isPhoneNumberValid(phoneNumber) {
// 匹配国内常见的手机号格式
return /^1[3-9]\d{9}$/.test(phoneNumber);
}
// 身份证校验函数
export function isValidIdCard (idCard) {
if (typeof idCard !== 'string') {
return false;
}
// 定义省份代码
const provinceCodes = {
11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古",
21: "辽宁", 22: "吉林", 23: "黑龙江",
31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东",
41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南",
50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏",
61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆",
71: "台湾", 81: "香港", 82: "澳门", 91: "国外"
};
// 检查长度
if (!/^\d{17}(\d|X|x)$/.test(idCard)) {
return false;
}
// 验证省份代码
const provinceCode = idCard.substring(0, 2);
if (!provinceCodes[provinceCode]) {
return false;
}
// 验证出生日期
const birthDate = idCard.substring(6, 14);
const birthYear = parseInt(birthDate.substring(0, 4), 10);
const birthMonth = parseInt(birthDate.substring(4, 6), 10);
const birthDay = parseInt(birthDate.substring(6, 8), 10);
const birthDateObject = new Date(birthYear, birthMonth - 1, birthDay);
if (
birthDateObject.getFullYear() !== birthYear ||
birthDateObject.getMonth() + 1 !== birthMonth ||
birthDateObject.getDate() !== birthDay
) {
return false;
}
// 验证校验码
const weightFactors = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
const checkCodes = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
const sum = idCard
.substring(0, 17)
.split('')
.reduce((acc, num, index) => acc + parseInt(num, 10) * weightFactors[index], 0);
const checkCode = checkCodes[sum % 11];
return checkCode === idCard[17].toUpperCase();
};