这版是公共号授权版
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"navigationBarTitleText": "授权登录",
|
||||
"usingComponents": {}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import { BASE_URL } from "../../env";
|
||||
|
||||
Page({
|
||||
data: {
|
||||
webViewUrl: "",
|
||||
loading: true,
|
||||
backUrl: "",
|
||||
code: "",
|
||||
authCompleted: false, // 防止重复处理
|
||||
},
|
||||
|
||||
onLoad(options: any) {
|
||||
if (options.back) {
|
||||
this.setData({ backUrl: decodeURIComponent(options.back) });
|
||||
}
|
||||
const code = options.code || "";
|
||||
this.setData({ code })
|
||||
|
||||
setTimeout(() => {
|
||||
const authUrl = `${BASE_URL}/h5/oauth/auth-success.html?entryCode=${encodeURIComponent(code)}`
|
||||
this.setData({
|
||||
webViewUrl: authUrl,
|
||||
loading: true,
|
||||
});
|
||||
}, 50);
|
||||
},
|
||||
|
||||
|
||||
/** web-view 首次加载完成,隐藏骨架屏 */
|
||||
onWebViewLoad() {
|
||||
this.setData({ loading: false });
|
||||
},
|
||||
|
||||
|
||||
onMessage(e: any) {
|
||||
|
||||
const dataList = e.detail?.data || [];
|
||||
console.log("onMessage===========》", dataList);
|
||||
if (dataList.length === 0) return;
|
||||
|
||||
const data = dataList.reduce((acc: any, curr: any) => ({ ...acc, ...curr }), {});
|
||||
console.log("onMessage", data);
|
||||
// 先存储缓存,再操作页面状态
|
||||
const fields: Record<string, string> = {
|
||||
openId: data.openId || '',
|
||||
unionId: data.unionId || '',
|
||||
appId: data.appId || '',
|
||||
userName: data.nickname || '微信用户',
|
||||
avatarUrl: data.avatar || '',
|
||||
code: data.code || '',
|
||||
userId: data.userId || '',
|
||||
};
|
||||
|
||||
Object.entries(fields).forEach(([key, value]) => {
|
||||
if (value) {
|
||||
try {
|
||||
wx.setStorageSync(key, value);
|
||||
} catch (err: any) {
|
||||
console.log("缓存写入失败 key:", key, "error:", err?.message || err);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
getApp<IAppOption>().globalData.isLogin = true;
|
||||
|
||||
getApp<IAppOption>().globalData.store = {
|
||||
...getApp<IAppOption>().globalData.store,
|
||||
...fields,
|
||||
};
|
||||
|
||||
try { this.setData({ authCompleted: true }); } catch (_) { }
|
||||
},
|
||||
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
<!--pages/webview-auth/index.wxml-->
|
||||
<view class="container">
|
||||
<view class="loading" wx:if="{{loading && webViewUrl}}">
|
||||
<view class="loading-spinner"></view>
|
||||
<text class="loading-text">正在加载授权页面...</text>
|
||||
</view>
|
||||
<web-view
|
||||
wx:if="{{webViewUrl}}"
|
||||
src="{{webViewUrl}}"
|
||||
bindmessage="onMessage"
|
||||
bindload="onWebViewLoad"
|
||||
></web-view>
|
||||
</view>
|
||||
@@ -0,0 +1,61 @@
|
||||
/* pages/webview-auth/index.wxss */
|
||||
.container {
|
||||
width: 100%;
|
||||
height: 100vh;
|
||||
overflow: hidden;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.loading {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background: #fff;
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
.loading-spinner {
|
||||
width: 40rpx;
|
||||
height: 40rpx;
|
||||
border: 4rpx solid #e5e5e5;
|
||||
border-top-color: #07c160;
|
||||
border-radius: 50%;
|
||||
animation: spin 0.8s linear infinite;
|
||||
}
|
||||
|
||||
.loading-text {
|
||||
margin-top: 20rpx;
|
||||
font-size: 28rpx;
|
||||
color: #999;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
to { transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
.cancel-btn {
|
||||
position: fixed;
|
||||
bottom: 40rpx;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
width: 300rpx;
|
||||
height: 72rpx;
|
||||
line-height: 72rpx;
|
||||
text-align: center;
|
||||
background: #e64340;
|
||||
color: #fff;
|
||||
font-size: 28rpx;
|
||||
border-radius: 36rpx;
|
||||
z-index: 999;
|
||||
box-shadow: 0 4rpx 12rpx rgba(230, 67, 64, 0.3);
|
||||
}
|
||||
|
||||
.cancel-btn:active {
|
||||
opacity: 0.8;
|
||||
}
|
||||
Reference in New Issue
Block a user