'use strict'; /** * 辅助方法扩展 * * @author CaiAoLin * @date 2017/9/28 * @version */ module.exports = { /** * 生成随机字符串 * * @param {Number} length - 需要生成字符串的长度 * @param {Number} type - 1为数字和字符 2为纯数字 3为纯字母 * @return {String} - 返回生成结果 */ generateRandomString(length, type = 1) { length = parseInt(length); length = isNaN(length) ? 1 : length; let randSeed = []; let numberSeed = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; let stringSeed = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']; switch (type) { case 1: randSeed = stringSeed.concat(numberSeed); stringSeed = numberSeed = null; break; case 2: randSeed = numberSeed; break; case 3: randSeed = stringSeed; break; default: break; } const seedLength = randSeed.length - 1; let result = ''; for (let i = 0; i < length; i++) { const index = Math.ceil(Math.random() * seedLength); result += randSeed[index]; } return result; }, /** * 显示排序符号 * * @param {String} field - 字段名称 * @return {String} - 返回字段排序的符号 */ showSortFlag(field) { const sort = this.ctx.sort; if (!(sort instanceof Array) || sort.length !== 2) { return ''; } sort[1] = sort[1].toUpperCase(); return (sort[0] === field && sort[1] === 'DESC') ? '' : '-'; }, /** * 判断是否为ajax请求 * * @param {Object} request - 请求数据 * @return {boolean} 判断结果 */ isAjax(request) { let headerInfo = request.headers['x-requested-with'] === undefined ? '' : request.headers['x-requested-with']; headerInfo = headerInfo.toLowerCase(); return headerInfo === 'xmlhttprequest'; }, /** * 模拟发送请求 * * @param {String} url - 请求地址 * @param {Object} data - 请求数据 * @param {String} type - 请求类型(POST) POST | GET * @param {String} dataType - 数据类型 json|text * @return {Object} - 请求结果 */ async sendRequest(url, data, type = 'POST', dataType = 'json') { // 发起请求 const response = await this.ctx.curl(url, { method: type, data, dataType, }); if (response.status !== 200) { throw '请求失败'; } return response.data; }, /** * 深度验证数据 * * @param {Object} rule - 数据规则 * @return {void} */ validate(rule) { // 先用内置的验证器验证数据 this.ctx.validate(rule); // 然后再验证是否有多余的数据 const postData = this.ctx.request.body; delete postData._csrf; const postDataKey = Object.keys(postData); const ruleKey = Object.keys(rule); // 自动增加字段则填充上,以防判断出错 if (postData.create_time !== undefined) { ruleKey.push('create_time'); } for (const tmp of postDataKey) { // 规则里面没有定义则抛出异常 if (ruleKey.indexOf(tmp) < 0) { throw '参数不正确'; } } }, /** * 拆分path * * @param {String|Array} paths - 拆分字符 * @param {String} symbol - 拆分符号 * @return {Array} - 拆分结果 */ explodePath(paths, symbol = '.') { const result = []; paths = paths instanceof Array ? paths : [paths]; for (const path of paths) { // 拆分数据 const pathArray = path.split(symbol); // 用户缓存循环的数据 const tmpArray = []; for (const tmp of pathArray) { // 每次循环都追加一个数据进去 tmpArray.push(tmp); const tmpPathString = tmpArray.join(symbol); // 判断是否已经存在有对应数据 if (result.indexOf(tmpPathString) >= 0) { continue; } result.push(tmpPathString); } } return result; }, /** * 判断当前用户是否有指定权限 * * @param {Number|Array} permission - 权限id * @return {Boolean} - 返回判断结果 */ hasPermission(permission) { let result = false; try { const sessionUser = this.ctx.session.sessionUser; if (sessionUser.permission === undefined) { throw '不存在权限数据'; } let currentPermission = sessionUser.permission; if (currentPermission === '') { throw '权限数据为空'; } // 管理员则直接返回结果 if (currentPermission === 'all') { return true; } currentPermission = currentPermission.split(','); permission = permission instanceof Array ? permission : [permission]; let counter = 0; for (const tmp of permission) { if (currentPermission[tmp] !== undefined) { counter++; } } result = counter === permission.length; } catch (error) { result = false; } return result; }, };