const Token = require('./token'); const User = require('./user'); class AuthHandler{ constructor(ctx) { this.ctx = ctx; this._logined = false; this._error = ''; this._user = null; this.keeptime = 2592000; // 30 days in seconds this.rules = []; this.config = []; this.options = []; this.instance = null; this._wms_user = null; this.app = ctx.app; this.Token = new Token(this.app); this.UserHand = new User(this.app); } async init(token){ if(!token){ return false; } let data =await this.Token._get(token); if(!data){ return false; } let user_id = parseInt(data.user_id); if(user_id>0){ let user = await this.UserHand._get(user_id); if(!user){ await this.setError('Account not exist'); return false; } if(user.status !='normal'){ await this.setError('Account is locked'); return false; } let wms_user = await this.UserHand._getWmsUser(user.wms_userid); this._user = user; this._wms_user = wms_user; this._logined = true; this._token = token; return true; }else{ await this.setError('You are not logged in'); return false; } return true; } async getRuleList(){ if(this._rules){ return this._rules; } let group = this._user.group; if(!group){ return []; } console.log(group.rules); this.rules = this.app.mysql.get("dblocal").query(`select id,pid,name,title,ismenu from ye4s_user_rule where status="normal" and id in (${group.rules})`); return this.rules; } async setError(_error){ this._error = _error; } async getError(){ return this._error; } async getUser(){ return this._user; } async getWmsUser(){ return this._wms_user; } } module.exports = AuthHandler