'use strict'; /** * 登录日志-数据模型 * * @author lanjianrong * @date 2020/8/31 * @version */ // const UAParser = require('ua-parser-js'); module.exports = app => { class LoginLogging extends app.BaseService { constructor(ctx) { super(ctx); this.tableName = 'login_logging'; } /** * 创建记录 * @param {Object} payload - 载荷 */ async createLog(payload) { const transaction = await this.db.beginTransaction(); try { transaction.insert(this.tableName, payload); await transaction.commit(); } catch (error) { await transaction.rollback(); throw error; } } /** * 创建登录日志 * @return {Boolean} 日志是否创建成功 * @param {Number} type - 登录类型 * @param {Number} status - 是否显示记录 */ async addLoginLog(type, status) { const { ctx } = this; const ipMsg = await ctx.helper.getUserIPMsg(); const payload = { uid: ctx.session.sessionUser.accountId, pid: ctx.session.sessionProject.id, type, show: status, }; this._.assign(payload, ipMsg); return await this.createLog(payload); } /** * 获取登录日志 * @param {Number} pid - 项目id * @param {Number} uid - 用户id * @return {Promise} 日志数组 */ async getLoginLogs(pid, uid) { return this.db.select(this.tableName, { where: { pid, uid, show: 0 }, orders: [['create_time', 'desc']], columns: ['browser', 'create_time', 'ip', 'os', 'address'], limit: 10, offset: 0, }); } } return LoginLogging; };