module.exports = { /** * 是否正整数(不能以0开头) */ isInteger: s => /^[1-9]\d*$/.test(s), isMD5: s => /^[a-f0-9]{32}$/.test(s), isMobile: s => /^1(3[0-9]|4[579]|5[012356789]|6[267]|7[01235678]|8[0-9]|9[1589])\d{8}$/.test(s), isEmail: s => /^[\w-.]+@([\da-zA-Z-]+\.)+[\da-zA-Z-]{2,3}$/.test(s), isIdList: s => /^([1-9]\d*,)*[1-9]\d*$/.test(s), isDate: s => /^(19|20)\d{2}-((0?[1-9])|(1[0-2]))-((0?[1-9])|([1-2]\d)|30|31)$/.test(s), MD5: s => { const crypto = require('crypto') return crypto.createHash('md5').update(s.toString()).digest('hex') }, /** * 随机数字(返回字符串类型),传1个参数为位数(最大16位),传2个参数为范围(含两端) */ RndNum: (min, max) => min <= max ? Math.floor(Math.random() * (max - min + 1) + min).toString() : Math.random().toString().slice(-min), /** * 客户端IP地址 */ GuestIP: ctx => { let ip = ctx.header['x-real-ip'] || ctx.header['x-forwarded-for'] || ctx.request.ip ip.includes(':') && (ip = ip.split(':').pop()) return ip.substring(0, 15) } }