import { reactive } from 'vue' import { browser, setAuth } from './helpers/utils' import { postMessage } from './helpers/native-message' type status = 'init' | 'login' | 'logout' | 'error' export const state = reactive({ user: { status: 'init' as status, data: {} as any }, platformType: 'STUDENT' as 'STUDENT' | 'TEACHER' | 'SCHOOL', clientId: { STUDENT: 'jmedu-student', TEACHER: 'jmedu-teacher', SCHOOL: 'jmedu-school' }, platformApi: '/api-student' as '/api-student' | '/api-teacher' | '/api-school', version: '', // 版本号 例如: 1.0.0 navBarHeight: 0, // 状态栏高度 ossUploadUrl: 'https://ks3-cn-beijing.ksyuncs.com/', musicCertStatus: false as boolean, // 是否音乐认证 openLiveStatus: false as boolean // 是否开通直播 }) // 预览上传到oss的地址 export const getOssUploadUrl = (bucket: string) => { const tmpBucket = bucket || 'gyt' return `https://${tmpBucket}.ks3-cn-beijing.ksyuncs.com/` } export const setLoginInit = () => { state.user.status = 'init' state.user.data = null } export const setLogin = (data: any) => { state.user.status = 'login' state.user.data = data } export const setLogout = () => { state.user.status = 'logout' state.user.data = null } export const setLoginError = () => { state.user.status = 'error' state.user.data = null } // 用于处理跳转地址,如果是在app内,则打开一个新的webview, 否则跳转连接 export const openDefaultWebView = (url?: string, callBack?: any) => { if (browser().isApp) { postMessage({ api: 'openWebView', content: { url, orientation: 1, isHideTitle: false } }) } else { callBack && callBack() } } /** * @description 微信授权-会根据环境去判断 * @param wxAppId * @param urlString 回调链接【默认当前页面地址】 * @returns void */ export const goWechatAuth = (wxAppId: string, urlString?: string) => { // 开发环境 if (import.meta.env.DEV) { const replaceUrl = `https://online.lexiaoya.cn/getWxCode?appid=${ wxAppId || 'wx8654c671631cfade' }&state=STATE&redirect_uri=` + encodeURIComponent(urlString || window.location.href) window.location.replace(replaceUrl) } // 生产环境 if (import.meta.env.PROD) { goAuth(wxAppId, urlString) } } const goAuth = (wxAppId: string, urlString?: string) => { // 用户授权 console.log(urlString || window.location.href, 'urlString || window.location.href') const urlNow = encodeURIComponent(urlString || window.location.href) console.log(urlNow, 'urlNow') const scope = 'snsapi_base' //snsapi_userinfo //静默授权 用户无感知 const appid = wxAppId || 'wx8654c671631cfade' const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=STATE&connect_redirect=1#wechat_redirect` window.location.replace(url) } /** * @description 支付宝授权-会根据环境去判断 * @param wxAppId * @param urlString 回调链接【默认当前页面地址】 * @returns void */ export const goAliAuth = (alipayAppId: string, urlString?: string) => { // 支付宝授权 const urlNow = encodeURIComponent(urlString || window.location.href) const appid = alipayAppId || '2021004113639386' // 开发环境 if (import.meta.env.DEV) { const url = `https://online.lexiaoya.cn/getAliCode?app_id=${appid}&state=STATE&redirect_uri=${urlNow}` window.location.replace(url) } // 生产环境 if (import.meta.env.PROD) { alipayAuth(alipayAppId, urlString) } } const alipayAuth = (alipayAppId: string, urlString?: string) => { // 用户授权 const urlNow = encodeURIComponent(urlString || window.location.href) const scope = 'auth_base' //snsapi_userinfo //静默授权 用户无感知 const appid = alipayAppId || '2021004113639386' // 判断是否是线上 const url = `https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=${appid}&redirect_uri=${urlNow}&response_type=auth_code&scope=${scope}&state=STATE` window.location.replace(url) }