import { useAppStore } from "@/stores/app";
import { checkLogin } from "./login";
import { login } from "../api/public";
import Cache from "../utils/cache";
import { STATE_R_KEY, USER_INFO, EXPIRES_TIME } from "@/config/cache";
// const appStore = useAppStore() // 在这里使用会报错，Pinia在main.js还未挂载
class Routine {
  constructor() {
    this.scopeUserInfo = "scope.userInfo";
  }

  async getUserCode() {
    let isAuth = await this.isAuth(),
      code = "";
    if (isAuth) code = await this.getCode();
    return code;
  }

  /**
   * 获取用户信息
   */
  getUserProfile() {
    let code = this.getUserCode();
    return new Promise((resolve, reject) => {
      uni.getUserProfile({
        lang: "zh_CN",
        desc: "用于完善会员资料", // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success(user) {
          if (code) user.code = code;
          resolve({ userInfo: user, islogin: false });
        },
        fail(res) {
          reject(res);
        },
      });
    });
  }

  /**
   * 获取用户信息
   */
  authorize() {
    return new Promise((resolve, reject) => {
      if (checkLogin())
        return resolve({
          userInfo: Cache.get(USER_INFO, true),
          islogin: true,
        });
      uni.authorize({
        scope: this.scopeUserInfo,
        success() {
          resolve({ islogin: false });
        },
        fail(res) {
          reject(res);
        },
      });
    });
  }

  async getCode() {
    let provider = await this.getProvider();
    return new Promise((resolve, reject) => {
      uni.login({
        provider: provider,
        success(res) {
          if (res.code) Cache.set(STATE_R_KEY, res.code, 10800);
          return resolve(res.code);
        },
        fail() {
          return reject(null);
        },
      });
    });
  }

  /**
   * 获取服务供应商
   */
  getProvider() {
    return new Promise((resolve, reject) => {
      uni.getProvider({
        service: "oauth",
        success(res) {
          resolve(res.provider);
        },
        fail() {
          resolve(false);
        },
      });
    });
  }

  /**
   * 是否授权
   */
  isAuth() {
    return new Promise((resolve, reject) => {
      uni.getSetting({
        success(res) {
          if (!res.authSetting[this.scopeUserInfo]) {
            resolve(true);
          } else {
            resolve(true);
          }
        },
        fail() {
          resolve(false);
        },
      });
    });
  }
  /**
   * 小程序登录
   */
  authUserInfo(code, data) {
    return new Promise((resolve, reject) => {
      login(code, data)
        .then((res) => {
          if (res.data.type === "login") {
            const appStore = useAppStore();
            appStore.LOGIN({ token: res.data.token });
            appStore.SETUID(res.data.uid);
            // 保存过期时间（7天后过期）
            const expiresTime =
              Math.round(new Date() / 1000) + 7 * 24 * 60 * 60;
            Cache.set(EXPIRES_TIME, expiresTime, 0);
          }
          return resolve(res);
        })
        .catch((res) => {
          return reject(res);
        });
    });
  }
}

export default new Routine();
