"use strict";
const common_vendor = require("../common/vendor.js");
const utils_storage = require("../utils/storage.js");
const utils_constant = require("../utils/constant.js");
const utils_auth = require("../utils/auth.js");
const utils_request = require("../utils/request.js");
const useUserStore = common_vendor.defineStore("user", {
  state: () => ({
    token: utils_auth.getToken(),
    name: utils_storage.storage.get(utils_constant.constant.name),
    avatar: utils_storage.storage.get(utils_constant.constant.avatar),
    roles: utils_storage.storage.get(utils_constant.constant.roles),
    permissions: utils_storage.storage.get(utils_constant.constant.permissions)
  }),
  actions: {
    // 登录
    Login(userInfo) {
      return new Promise((resolve, reject) => {
        utils_request.request({
          url: "/authorize/sysWorkUser/login",
          data: {
            loginName: userInfo.username.trim(),
            password: userInfo.password,
            clientType: "pc_web"
          },
          method: "POST",
          headers: {
            isToken: false
          }
        }).then((res) => {
          utils_auth.setToken(res);
          this.token = res;
          resolve(res);
        }).catch((err) => {
          reject(err);
        });
      });
    },
    // 获取用户信息
    // GetInfo() {
    //   return new Promise((resolve, reject) => {
    //     getInfo()
    //       .then((res) => {
    //         const user = res.user
    //         const avatar =
    //           user == null || user.avatar == '' || user.avatar == null
    //             ? ''
    //             : baseUrl + user.avatar
    //         const username =
    //           user == null || user.userName == '' || user.userName == null
    //             ? ''
    //             : user.userName
    //         if (res.roles && res.roles.length > 0) {
    //           this.roles = res.roles
    //           storage.set(constant.roles, res.roles)
    //           this.permissions = res.permissions
    //           storage.set(constant.permissions, res.permissions)
    //         } else {
    //           this.roles = ['ROLE_DEFAULT']
    //           storage.set(constant.roles, ['ROLE_DEFAULT'])
    //         }
    //         resolve(res)
    //       })
    //       .catch((error) => {
    //         reject(error)
    //       })
    //   })
    // },
    // 退出系统
    LogOut() {
      return new Promise((resolve, reject) => {
        this.roles = "";
        utils_storage.storage.set(utils_constant.constant.roles, "");
        this.permissions = "";
        utils_storage.storage.set(utils_constant.constant.permissions, "");
        utils_auth.removeToken();
        utils_storage.storage.clean();
        resolve("退出登录成功~");
      });
    }
  }
});
exports.useUserStore = useUserStore;
