UNPKG

community-srv-cloud-utils

Version:

微信小程序通用工具库 - 前后端共用,社区服务项目

198 lines (171 loc) 5.5 kB
/** * 微信小程序云函数通用工具库 * 社区服务项目专用工具函数 */ /** * 时间格式化函数 - 将时间戳转换为相对时间 * @param {Date|string|number} date 时间 * @returns {string} 格式化后的时间字符串 */ function formatTimeAgo(date) { if (!date) return '' const t = (date instanceof Date) ? date.getTime() : new Date(date).getTime() const diff = Math.max(0, Date.now() - t) const d = Math.floor(diff / (24 * 60 * 60 * 1000)) if (d > 0) return `${d}天前` const h = Math.floor(diff / (60 * 60 * 1000)) if (h > 0) return `${h}小时前` const m = Math.floor(diff / (60 * 1000)) if (m > 0) return `${m}分钟前` return '刚刚' } /** * 格式化完整时间 - 显示具体日期时间 * @param {Date|string|number} date 时间 * @returns {string} 格式化后的时间字符串 */ function formatFullTime(date) { if (!date) return '' const d = new Date(date) const year = d.getFullYear() const month = String(d.getMonth() + 1).padStart(2, '0') const day = String(d.getDate()).padStart(2, '0') const hours = String(d.getHours()).padStart(2, '0') const minutes = String(d.getMinutes()).padStart(2, '0') return `${year}-${month}-${day} ${hours}:${minutes}` } /** * 生成随机字符串 * @param {number} length 长度 * @returns {string} 随机字符串 */ function generateRandomString(length = 8) { const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' let result = '' for (let i = 0; i < length; i++) { result += chars.charAt(Math.floor(Math.random() * chars.length)) } return result } /** * 验证邮箱格式 * @param {string} email 邮箱地址 * @returns {boolean} 是否为有效邮箱 */ function isValidEmail(email) { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/ return emailRegex.test(email) } /** * 验证手机号格式 * @param {string} phone 手机号 * @returns {boolean} 是否为有效手机号 */ function isValidPhone(phone) { const phoneRegex = /^1[3-9]\d{9}$/ return phoneRegex.test(phone) } /** * 计算文本相似度(简单的编辑距离算法) * @param {string} text1 文本1 * @param {string} text2 文本2 * @returns {number} 相似度(0-1) */ function calculateSimilarity(text1, text2) { if (!text1 || !text2) return 0 const len1 = text1.length const len2 = text2.length if (len1 === 0) return len2 === 0 ? 1 : 0 if (len2 === 0) return 0 // 创建编辑距离矩阵 const matrix = Array(len1 + 1).fill().map(() => Array(len2 + 1).fill(0)) // 初始化第一行和第一列 for (let i = 0; i <= len1; i++) matrix[i][0] = i for (let j = 0; j <= len2; j++) matrix[0][j] = j // 计算编辑距离 for (let i = 1; i <= len1; i++) { for (let j = 1; j <= len2; j++) { const cost = text1[i - 1] === text2[j - 1] ? 0 : 1 matrix[i][j] = Math.min( matrix[i - 1][j] + 1, // 删除 matrix[i][j - 1] + 1, // 插入 matrix[i - 1][j - 1] + cost // 替换 ) } } const maxLen = Math.max(len1, len2) const distance = matrix[len1][len2] return 1 - distance / maxLen } /** * 招聘信息数据验证 * @param {Object} data 招聘信息数据对象 * @returns {Object} 验证结果 { isValid: boolean, errors: string[] } */ function validateJobData(data) { const errors = [] if (!data.company || !data.company.trim()) { errors.push('公司名称不能为空') } if (!data.position || !data.position.trim()) { errors.push('招聘岗位不能为空') } if (!data.salary || !data.salary.trim()) { errors.push('薪资待遇不能为空') } if (!data.count || data.count <= 0) { errors.push('招聘人数必须大于0') } if (!data.address || !data.address.trim()) { errors.push('工作地址不能为空') } if (!data.contact || !data.contact.trim()) { errors.push('联系人不能为空') } if (!data.phone || !data.phone.trim()) { errors.push('联系电话不能为空') } if (!data.communities || !Array.isArray(data.communities) || data.communities.length === 0) { errors.push('请至少选择一个社区') } return { isValid: errors.length === 0, errors: errors } } /** * 验证ID参数 * @param {string} id 要验证的ID * @param {string} fieldName 字段名称(用于错误提示) * @returns {Object} 验证结果 { isValid: boolean, error: string } */ function validateId(id, fieldName = 'ID') { if (!id) { return { isValid: false, error: `${fieldName}不能为空` } } return { isValid: true, error: null } } /** * 验证状态值 * @param {string} status 要验证的状态值 * @param {Array} validStatuses 有效状态数组 * @param {string} fieldName 字段名称(用于错误提示) * @returns {Object} 验证结果 { isValid: boolean, error: string } */ function validateStatus(status, validStatuses, fieldName = '状态') { if (!validStatuses.includes(status)) { return { isValid: false, error: `无效的${fieldName}值` } } return { isValid: true, error: null } } module.exports = { formatTimeAgo, formatFullTime, generateRandomString, isValidEmail, isValidPhone, calculateSimilarity, validateJobData, validateId, validateStatus }