var system = require("../../../system")
const http = require("http")
const querystring = require('querystring');
var settings = require("../../../../config/settings");
const CtlBase = require("../../ctl.base");
const logCtl = system.getObject("web.common.oplogCtl");
const md5 = require("MD5");
const uuidv4 = require('uuid/v4');

var cacheBaseComp = null;
class UserCtl extends CtlBase {
  constructor() {
    super("auth", CtlBase.getServiceName(UserCtl));
    this.redisClient = system.getObject("util.redisClient");
  }

  async login(pobj, pobj2, req, res) {
    let loginName = this.trim(pobj.loginName);
    let password = this.trim(pobj.password);

    try {
      // 查用户
      var loginUser = await this.service.findOne({userName: loginName});
      if (!loginUser || loginUser.password != this.encryptPasswd(password)) {
        return system.getResult(null, "用户名或密码错误");
      }
      var sid = await this.setLogin(loginUser);
      return system.getResultSuccess(sid);
    } catch (error) {
      console.log(error);
      return system.getResultFail(500, "接口异常：" + error.message);
    }
  }

  async setLogin(user) {
    var sid = uuidv4();
    sid = "3cb49932-fa02-44f0-90db-9f06fe02e5c7";
    await this.redisClient.setWithEx(sid, JSON.stringify(user), 60 * 60);
    return sid;
    // return bdanalysisid;
  }

}
module.exports = UserCtl;