import router from "../router"
// 判断是否是移动端
export const isMobile=()=>{
  return !(window.innerWidth>768)
}

/**
 * 复制文本内容
 * @param {*} text 文本内容
 * @returns 
 */
export const copyText=(text)=>{
  return new Promise((resolve,reject)=>{
    try {
      if (navigator.clipboard && window.isSecureContext) {
        // navigator clipboard 向剪贴板写文本
        navigator.clipboard.writeText(text).then(() => {
          resolve()
        })
      }else{
        const input = document.createElement('input');
        input.setAttribute('readonly', 'readonly');
        input.setAttribute('value', text);
        document.body.appendChild(input);
        input.setSelectionRange(0, input.value.length);
        input.select();
        document.execCommand('copy');
        document.body.removeChild(input);
        resolve()
      }
    } catch (error) {
      console.error(error.message);
      reject(error)
    }
  })
}

// 去中文研报
export const goYBPCCN=()=>{
  let href = import.meta.env.VITE_CN_YB_PC
  window.open(href,'_blank');
}
// 去用户信息页面 - 移动端
export const userInfoGo=()=>{
  let {href} = router.resolve("/my");
  const yb_token = localStorage.getItem('yben_token')
  if(yb_token){
    window.open(href,'_blank');
  }else{
    //未注册直接走退出登录
    loginOut()
  }
  
}
// 去绑定手机页面
export const bindPhone=()=>{
  let {href} = router.resolve({path:"/bindPhoneNo"});
  window.open(href,'_blank');
}
// 退出登录
export const loginOut=()=>{
  localStorage.removeItem('yben_token')
  localStorage.removeItem('user_info')
  router.push('/login')
}
// 去修改密码页面
export const passwordChange=()=>{
  let {href} = router.resolve({path:"/passwordChange"});
  window.open(href,'_blank');
}

//去注册页(PC)
export const signUp=()=>{
    localStorage.removeItem('yben_token')
    localStorage.removeItem('user_info')
    isMobile()?router.push({path:'/login',query:{isRegister:1}}):router.push('/register')
}