'use strict'; // 基类服务 const BaseService = require('./base.js'); /** * [exports 后台管理经理基类服务] * @type {[type]} */ module.exports = class ManagerService extends BaseService { /* ==================================session相关=====================================*/ /** * [ActionAdminUserId 当前后台操作的管理员ID] * @author szjcomo * @date 2020-11-02 */ ActionAdminUserId() { const that = this; const token = that.ctx.request.header.authorization; const user = that.app.jwt.verify(token, that.app.config.jwt.secret); return user.admin_id; } /** * [ActionAdminUser 获取当前管理员] * @author szjcomo * @date 2021-02-27 */ ActionAdminUser() { const that = this; const token = that.ctx.request.header.authorization; const user = that.app.jwt.verify(token, that.app.config.jwt.secret); return user; } /** * [getRoleAuth 获取角色权限] * @author szjcomo * @date 2020-11-02 * @param {[type]} role_id [description] * @param {Boolean} is_checked [description] * @param {Number} mobile [是否手机端访问] * @return {[type]} [description] */ async getRoleAuth(role_id, is_checked = false, mobile = 0) { const that = this; // const seq = that.app.Sequelize; let idxs = []; let result = []; const options = { attributes: [ 'access_id', 'access_name', 'access_icon', 'pid', 'is_nav', 'router_name', 'router_path', 'vuecomponent', 'mobile_show' ], raw: true, where: { access_status: 1 }, order: [[ 'access_sort', 'asc' ], [ 'access_id', 'asc' ]], }; if (mobile) options.where.mobile_show = 1; const access = await that.app.model.Accesss.findAll(options); if (role_id > 0) { const tmpOptions = { where: { role_id }, attributes: [ 'access_id' ], raw: true }; const tmpAccess = await that.app.model.AccessRole.findOne(tmpOptions); if (tmpAccess) idxs = (tmpAccess.access_id.split(',')); } else { result = access; } if (result.length == 0) { access.forEach(item => { if (that.app.szjcomo.inArray(idxs, `${item.access_id}`)) { item.checked = true; } else { item.checked = false; } if (is_checked == false) { result.push(item); } else { if (item.checked) result.push(item); } }); } const finalResult = that.app.szjcomo.arrayRecursion(result, 0, 'pid', 'access_id', 'child'); return finalResult; } /** * [getLevel 获取分类节点等级] * @author szjcomo * @date 2020-10-26 * @param {Number} pid [description] * @param {[type]} actionModel [description] * @param {Object} options [description] * @return {[type]} [description] */ async getLevel(pid = 0, actionModel = null, options = {}) { if (pid == 0) return 1; const result = await actionModel.findOne(options); return (result.level + 1); } /* ==================================临时方法=====================================*/ /** * [console 控制台数据] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ async console() { const that = this; const cacheKey = `${process.env.APP_CUSTOME}_console_data`; let result = await that.service.redis.get(cacheKey); if (!result) { result = { /** * [quick_data 快捷处理] * @type {Array} */ quick_data: [ { icon: '', title: '控制台', router_name: 'system_dashboard' }, { icon: '', title: '管理员列表', router_name: 'admin_user' }, { icon: '', title: '订单列表', router_name: 'select_orders' }, { icon: '', title: '商品列表', router_name: 'select_products' }, { icon: '', title: '配置设置', router_name: 'config_setting' }, { icon: '', title: '用户列表', router_name: 'select_users' }, { icon: '', title: '支付配置', router_name: 'select_pays_config' }, { icon: '', title: '商品分类', router_name: 'select_product_category' }, ], baseData: await that.service.order.orderDataCount(), saleData: await that.service.order.saleDataCount(), loginData: await that.userLoginDataCount(), }; await that.service.redis.set(cacheKey, result, 10 * 60); } return result; } /** * [adminConfig 后台管理配置] * @author szjcomo * @date 2021-08-08 * @return {[type]} [description] */ async adminConfig() { const that = this; const configs = await that.app.model.Configs.findAll({ where: { field_index: { [that.app.Sequelize.Op.in]: [ 'website_title', 'files_upload_uri', 'image_ocr_uri', 'editor_templates_uri' ] } }, attributes: [ 'field_value', 'field_index' ], raw: true, }); const result = {}; configs.forEach(item => { result[item.field_index] = item.field_value; }); return result; } /** * [userLoginDataCount 最近每日用户登录数量统计] * @return {[type]} [description] */ async userLoginDataCount(day = 20) { const that = this; const curTime = that.app.szjcomo.time(); const seq = that.app.Sequelize; const times = []; for (let i = 0; i < day; i++) { const tmpTime = curTime - i * 24 * 60 * 60; times.push([ `${that.app.szjcomo.date('Y-m-d', tmpTime)} 00:00:00`, `${that.app.szjcomo.date('Y-m-d', tmpTime)} 23:59:59` ]); } const users = await that.app.model.UserLoginLogs.findAll({ where: { update_ttime: { [seq.Op.between]: [ times[day - 1][0], times[0][1] ] }, }, raw: true, }); const result = []; times.forEach(item => { const startTime = that.app.szjcomo.strToTime(item[0]); const endTime = that.app.szjcomo.strToTime(item[1]); const tmpData = { date: that.app.szjcomo.date('m-d', startTime), count: 0 }; users.forEach(childItem => { const tmpTime = that.app.szjcomo.strToTime(childItem.update_ttime); if (tmpTime >= startTime && tmpTime <= endTime) { tmpData.count += 1; } }); result.push(tmpData); }); return result; } };