/*
 * @Author: lixg lixg@dongwu-inc.com
 * @Date: 2023-01-17 19:47:07
 * @LastEditors: lixg lixg@dongwu-inc.com
 * @LastEditTime: 2023-03-07 20:25:25
 * @FilePath: /stu_h5/src/api/utils.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {watch, ref} from "vue";
import {boeRequest} from "@/api/request";
import {GET_USER_LIST} from "@/api/ThirdApi";

export function useImage(src) {
  return new URL(`../assets/image/${src}`, import.meta.url).href;
}

export function setCookie(name, value, perpetual) {
  const d = new Date();
  d.setDate(perpetual * 24 * 60 * 60 * 1000);
  document.cookie = `${name}=${value};expires=${d.toGMTString()};path=/`;
}

export function getCookie(name) {
  return document.cookie?.split(";").find(e => e.includes(name))?.replace(`${name}=`, "") || "";
}

export function delCookie() {
  setCookie("token", "", -1);
  localStorage.removeItem("userInfo");
}

export function useUserInfo(id) {
  const userInfo = ref({});
  watch(id, () => {
    id.value && boeRequest(GET_USER_LIST, { id: id.value }).then(res => {
      if(res.result.userInfoList[0]){
        userInfo.value = res.result.userInfoList[0];
        userInfo.value.avatar = userInfo.value.avatar ? userInfo.value.avatar.includes(import.meta.env.VITE_FILE_PATH) ? userInfo.value.avatar : (import.meta.env.VITE_FILE_PATH + userInfo.value.avatar) : "/800e23f7-b58c-4192-820d-0c6a2b7544cc.png";
      }
    });
  });
  return userInfo;
}