import querystring from "querystring";
import md5 from "md5";
import createHash from "sha.js";

import ajax from "@/libs/Ajax";

// 密码加密统一算法
function encodePass(pwd) {
  let sha256 = createHash("sha256");
  return md5(sha256.update(pwd).digest("hex"));
}

import config from "@/config";

export default {
  /**
   * 用户登录
   * @param userid 用户ID
   * @param passwd 密码
   * @param captcha 验证码
   * @return {Promise}
   */
  login({userid, passwd, captcha}) {
    passwd = encodePass(passwd);
    return ajax.request({
      message: true,
      method: "post",
      url: `${config.loginUrl}/login`,
      data: querystring.stringify({userid, passwd, captcha}),
      headers: {"Content-type": "application/x-www-form-urlencoded"}
    });
  },
  /**
   * 检查这个用户登录状态.
   * @param {String} userid
   */
  logck(userid) {
    return ajax.get(`${config.loginUrl}/logck`, {params: {userid}});
  },
  /**
   * 登出
   * @return {Promise}
   */
  logout() {
    return ajax.get(`${config.loginUrl}/logout`);
  },
  /**
   * 获取用户登录信息
   */
  getLoginInfo() {
    return ajax.get("/base/init/lastLoginInfo");
  },
  /**
   * 获取系统菜单
   */
  getRootMenu() {
    return ajax.get("/sys/init/getRootMenu");
  },

  /**
   * 修改密码
   *  @param {String} oldpasswd
   *  @param {String} passwd
   */
  changePwd(oldpasswd, passwd) {
    passwd = encodePass(passwd);
    oldpasswd = encodePass(oldpasswd);
    return ajax.post("/base/init/changePwd", {oldpasswd, passwd});
  },

  reporttest() {
    return ajax.post("/decm/report");
  }
};
