|
@@ -5,10 +5,20 @@ import { useUserStore } from './store/user';
|
|
|
import { initDebug } from './utils/debug';
|
|
import { initDebug } from './utils/debug';
|
|
|
import { initAnalytics } from './utils/analytics';
|
|
import { initAnalytics } from './utils/analytics';
|
|
|
import MiniPlayer from './components/MiniPlayer.vue';
|
|
import MiniPlayer from './components/MiniPlayer.vue';
|
|
|
|
|
+import CreditInsufficientModal from './components/CreditInsufficientModal.vue';
|
|
|
|
|
|
|
|
// 初始化全局调试工具
|
|
// 初始化全局调试工具
|
|
|
initDebug();
|
|
initDebug();
|
|
|
|
|
|
|
|
|
|
+// ==================== 微信环境检测 ====================
|
|
|
|
|
+function isInWechatBrowser(): boolean {
|
|
|
|
|
+ // #ifdef H5
|
|
|
|
|
+ const ua = navigator.userAgent.toLowerCase();
|
|
|
|
|
+ return ua.includes('micromessenger');
|
|
|
|
|
+ // #endif
|
|
|
|
|
+ return false;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// ==================== 微信 OAuth 回调处理 ====================
|
|
// ==================== 微信 OAuth 回调处理 ====================
|
|
|
async function handleWechatOAuthCallback() {
|
|
async function handleWechatOAuthCallback() {
|
|
|
// 只在 H5 环境处理
|
|
// 只在 H5 环境处理
|
|
@@ -17,7 +27,11 @@ async function handleWechatOAuthCallback() {
|
|
|
const code = searchParams.get('code');
|
|
const code = searchParams.get('code');
|
|
|
const state = searchParams.get('state');
|
|
const state = searchParams.get('state');
|
|
|
|
|
|
|
|
- if (!code) return; // 不是 OAuth 回调,跳过
|
|
|
|
|
|
|
+ console.log('[App] handleWechatOAuthCallback 触发', { code: !!code, fullUrl: window.location.href });
|
|
|
|
|
+ if (!code) {
|
|
|
|
|
+ console.log('[App] 无 code 参数,跳过 OAuth 处理');
|
|
|
|
|
+ return; // 不是 OAuth 回调,跳过
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
console.log('[App] 检测到微信 OAuth 回调,处理 code...');
|
|
console.log('[App] 检测到微信 OAuth 回调,处理 code...');
|
|
|
uni.showLoading({ title: '授权中...' });
|
|
uni.showLoading({ title: '授权中...' });
|
|
@@ -35,18 +49,15 @@ async function handleWechatOAuthCallback() {
|
|
|
await userStore.loginByOpenid(result.openid);
|
|
await userStore.loginByOpenid(result.openid);
|
|
|
console.log('[App] OpenID 已绑定,自动登录成功');
|
|
console.log('[App] OpenID 已绑定,自动登录成功');
|
|
|
|
|
|
|
|
- // 清除 URL 参数并跳转首页
|
|
|
|
|
|
|
+ // 清除 URL 参数,停留在当前页
|
|
|
searchParams.delete('code');
|
|
searchParams.delete('code');
|
|
|
searchParams.delete('state');
|
|
searchParams.delete('state');
|
|
|
const newSearch = searchParams.toString();
|
|
const newSearch = searchParams.toString();
|
|
|
const newUrl = window.location.origin + window.location.pathname
|
|
const newUrl = window.location.origin + window.location.pathname
|
|
|
- + (newSearch ? '?' + newSearch : '');
|
|
|
|
|
|
|
+ + (newSearch ? '?' + newSearch : '') + window.location.hash;
|
|
|
window.history.replaceState({}, '', newUrl);
|
|
window.history.replaceState({}, '', newUrl);
|
|
|
uni.hideLoading();
|
|
uni.hideLoading();
|
|
|
uni.showToast({ title: '自动登录成功', icon: 'success' });
|
|
uni.showToast({ title: '自动登录成功', icon: 'success' });
|
|
|
- setTimeout(() => {
|
|
|
|
|
- uni.switchTab({ url: '/pages/index/index' });
|
|
|
|
|
- }, 500);
|
|
|
|
|
return;
|
|
return;
|
|
|
} catch (e: any) {
|
|
} catch (e: any) {
|
|
|
if (e?.message?.includes('未绑定') || e?.code === -1) {
|
|
if (e?.message?.includes('未绑定') || e?.code === -1) {
|
|
@@ -80,7 +91,7 @@ async function handleWechatOAuthCallback() {
|
|
|
searchParams.delete('state');
|
|
searchParams.delete('state');
|
|
|
const newSearch = searchParams.toString();
|
|
const newSearch = searchParams.toString();
|
|
|
const newUrl = window.location.origin + window.location.pathname
|
|
const newUrl = window.location.origin + window.location.pathname
|
|
|
- + (newSearch ? '?' + newSearch : '');
|
|
|
|
|
|
|
+ + (newSearch ? '?' + newSearch : '') + window.location.hash;
|
|
|
window.history.replaceState({}, '', newUrl);
|
|
window.history.replaceState({}, '', newUrl);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -112,8 +123,80 @@ onLaunch(() => {
|
|
|
|
|
|
|
|
// 处理微信 OAuth 回调(必须在 initUser 之后)
|
|
// 处理微信 OAuth 回调(必须在 initUser 之后)
|
|
|
handleWechatOAuthCallback();
|
|
handleWechatOAuthCallback();
|
|
|
|
|
+
|
|
|
|
|
+ // 全局自动登录检查(微信环境下,有 openid 但未登录时尝试自动登录)
|
|
|
|
|
+ // #ifdef H5
|
|
|
|
|
+ checkAutoLogin();
|
|
|
|
|
+ // #endif
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+// ==================== 全局自动登录检查 ====================
|
|
|
|
|
+async function checkAutoLogin() {
|
|
|
|
|
+ // 只在微信环境下处理
|
|
|
|
|
+ if (!isInWechatBrowser()) {
|
|
|
|
|
+ console.log('[App] 非微信环境,跳过自动登录检查');
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const userStore = useUserStore();
|
|
|
|
|
+
|
|
|
|
|
+ // 如果已经登录,跳过
|
|
|
|
|
+ if (userStore.isLoggedIn) {
|
|
|
|
|
+ console.log('[App] 已登录,跳过自动登录检查');
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 检查 localStorage 是否有 openid
|
|
|
|
|
+ const wxOpenid = localStorage.getItem('wx_openid');
|
|
|
|
|
+ if (!wxOpenid) {
|
|
|
|
|
+ console.log('[App] 微信环境无 openid,触发 OAuth 授权');
|
|
|
|
|
+ // 触发 OAuth 授权
|
|
|
|
|
+ await triggerWechatOAuth();
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ console.log('[App] 检测到 openid,尝试自动登录:', wxOpenid.substring(0, 8) + '...');
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ await userStore.loginByOpenid(wxOpenid);
|
|
|
|
|
+ console.log('[App] 自动登录成功!');
|
|
|
|
|
+ uni.showToast({ title: '自动登录成功', icon: 'success' });
|
|
|
|
|
+ } catch (e: any) {
|
|
|
|
|
+ console.log('[App] 自动登录失败(openid 未绑定账号):', e?.message);
|
|
|
|
|
+ // 不做处理,让用户手动登录
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// ==================== 触发微信 OAuth 授权 ====================
|
|
|
|
|
+async function triggerWechatOAuth() {
|
|
|
|
|
+ // #ifdef H5
|
|
|
|
|
+ uni.showLoading({ title: '正在跳转微信授权...' });
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ const { get } = await import('./utils/request');
|
|
|
|
|
+
|
|
|
|
|
+ // 获取当前页面路径,用于授权后返回
|
|
|
|
|
+ const pages = getCurrentPages();
|
|
|
|
|
+ const currentPage = pages[pages.length - 1];
|
|
|
|
|
+ const route = currentPage?.route || 'pages/index_index';
|
|
|
|
|
+ const currentPath = `/${route}${window.location.hash}`;
|
|
|
|
|
+ const redirectUrl = `${window.location.origin}${currentPath}`;
|
|
|
|
|
+ console.log('[App] 授权回调地址:', redirectUrl);
|
|
|
|
|
+
|
|
|
|
|
+ // 调用后端获取微信授权链接
|
|
|
|
|
+ const result = await get<{ oauthUrl: string }>(`/payment/wechat/oauth-url?redirect=${encodeURIComponent(redirectUrl)}`);
|
|
|
|
|
+ console.log('[App] 微信授权链接:', result.oauthUrl);
|
|
|
|
|
+
|
|
|
|
|
+ // 跳转到微信授权页
|
|
|
|
|
+ window.location.href = result.oauthUrl;
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ console.error('[App] 获取授权链接失败:', e);
|
|
|
|
|
+ uni.hideLoading();
|
|
|
|
|
+ uni.showToast({ title: '授权失败,请重试', icon: 'none' });
|
|
|
|
|
+ }
|
|
|
|
|
+ // #endif
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
onShow(() => {
|
|
onShow(() => {
|
|
|
console.log('App Show');
|
|
console.log('App Show');
|
|
|
});
|
|
});
|
|
@@ -139,6 +222,7 @@ function applyDarkMode() {
|
|
|
<view id="app">
|
|
<view id="app">
|
|
|
<router-view />
|
|
<router-view />
|
|
|
<MiniPlayer />
|
|
<MiniPlayer />
|
|
|
|
|
+ <CreditInsufficientModal />
|
|
|
</view>
|
|
</view>
|
|
|
</template>
|
|
</template>
|
|
|
|
|
|