import { reactive } from 'vue'; import { browser } 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 }, navBarHeight: 0, // 状态栏高度 ossUploadUrl: 'https://ks3-cn-beijing.ksyuncs.com/' }); // 预览上传到oss的地址 export const getOssUploadUrl = (bucket: string) => { const tmpBucket = bucket || 'gym'; 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 goWechatAuthTemp = (wxAppId: string, urlString?: string) => { goAuth(wxAppId, urlString); }; /** * @description 微信授权-会根据环境去判断 * @param wxAppId * @param urlString 回调链接【默认当前页面地址】 * @returns void */ export const goWechatAuth = (wxAppId: string, urlString?: string) => { // 开发环境 if (import.meta.env.DEV) { const replaceUrl = `https://kt.colexiu.com/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) => { // 用户授权 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 || '2021004100630808'; // 开发环境 // if (import.meta.env.DEV) { let url = `https://kt.colexiu.com/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 || '2021004100630808'; // 判断是否是线上 let 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); };