feat: add wechat miniprogram webview login
This commit is contained in:
@@ -1,10 +1,28 @@
|
||||
const { WEB_VIEW_ENTRY_URL, WEB_VIEW_SOURCE_QUERY } = require('../../config');
|
||||
const {
|
||||
API_BASE_URL,
|
||||
MINI_PROGRAM_APP_ID,
|
||||
MINI_PROGRAM_ENV,
|
||||
WEB_VIEW_ENTRY_URL,
|
||||
WEB_VIEW_SOURCE_QUERY,
|
||||
} = require('../../config');
|
||||
|
||||
const MINI_PROGRAM_CLIENT_TYPE = 'mini_program';
|
||||
const MINI_PROGRAM_CLIENT_RUNTIME = 'wechat_mini_program';
|
||||
const CLIENT_INSTANCE_STORAGE_KEY = 'genarrative:mini-program-client-instance-id';
|
||||
|
||||
function isConfiguredEntryUrl(value) {
|
||||
const trimmed = String(value || '').trim();
|
||||
return /^https:\/\/[^/]+/i.test(trimmed);
|
||||
}
|
||||
|
||||
function trimTrailingSlash(value) {
|
||||
return String(value || '').trim().replace(/\/+$/u, '');
|
||||
}
|
||||
|
||||
function isConfiguredApiBaseUrl(value) {
|
||||
return /^https:\/\/[^/]+/i.test(String(value || '').trim());
|
||||
}
|
||||
|
||||
function appendQuery(url, query) {
|
||||
const pairs = Object.keys(query)
|
||||
.filter((key) => query[key])
|
||||
@@ -20,25 +38,188 @@ function appendQuery(url, query) {
|
||||
return `${url}${url.includes('?') ? '&' : '?'}${pairs.join('&')}`;
|
||||
}
|
||||
|
||||
function resolveWebViewUrl() {
|
||||
function appendHashParams(url, params) {
|
||||
const pairs = Object.keys(params)
|
||||
.filter((key) => params[key])
|
||||
.map(
|
||||
(key) =>
|
||||
`${encodeURIComponent(key)}=${encodeURIComponent(String(params[key]))}`,
|
||||
);
|
||||
if (pairs.length === 0) {
|
||||
return url;
|
||||
}
|
||||
|
||||
const hashIndex = url.indexOf('#');
|
||||
const baseUrl = hashIndex >= 0 ? url.slice(0, hashIndex) : url;
|
||||
const rawHash = hashIndex >= 0 ? url.slice(hashIndex + 1) : '';
|
||||
const separator = rawHash ? '&' : '';
|
||||
return `${baseUrl}#${rawHash}${separator}${pairs.join('&')}`;
|
||||
}
|
||||
|
||||
function resolveWebViewUrl(authResult) {
|
||||
const entryUrl = String(WEB_VIEW_ENTRY_URL || '').trim();
|
||||
if (!isConfiguredEntryUrl(entryUrl)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return appendQuery(entryUrl, WEB_VIEW_SOURCE_QUERY);
|
||||
const sourcedUrl = appendQuery(entryUrl, WEB_VIEW_SOURCE_QUERY);
|
||||
if (!authResult || !authResult.token) {
|
||||
return sourcedUrl;
|
||||
}
|
||||
|
||||
return appendHashParams(sourcedUrl, {
|
||||
auth_provider: 'wechat',
|
||||
auth_token: authResult.token,
|
||||
auth_binding_status: authResult.bindingStatus,
|
||||
});
|
||||
}
|
||||
|
||||
function getClientInstanceId() {
|
||||
const stored = wx.getStorageSync(CLIENT_INSTANCE_STORAGE_KEY);
|
||||
if (stored) {
|
||||
return String(stored);
|
||||
}
|
||||
|
||||
const nextId = `wxmp_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`;
|
||||
wx.setStorageSync(CLIENT_INSTANCE_STORAGE_KEY, nextId);
|
||||
return nextId;
|
||||
}
|
||||
|
||||
function resolveClientPlatform() {
|
||||
const info = wx.getSystemInfoSync();
|
||||
const platform = String(info.platform || '').toLowerCase();
|
||||
if (platform === 'ios') {
|
||||
return 'ios';
|
||||
}
|
||||
if (platform === 'android') {
|
||||
return 'android';
|
||||
}
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
function wxLogin() {
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.login({
|
||||
success(result) {
|
||||
if (result.code) {
|
||||
resolve(result.code);
|
||||
return;
|
||||
}
|
||||
reject(new Error('微信登录未返回 code'));
|
||||
},
|
||||
fail(error) {
|
||||
reject(new Error(error.errMsg || '微信登录失败'));
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function requestMiniProgramLogin(code) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const apiBaseUrl = trimTrailingSlash(API_BASE_URL);
|
||||
if (!isConfiguredApiBaseUrl(apiBaseUrl)) {
|
||||
reject(new Error('请先配置 API_BASE_URL'));
|
||||
return;
|
||||
}
|
||||
|
||||
wx.request({
|
||||
url: `${apiBaseUrl}/api/auth/wechat/miniprogram-login`,
|
||||
method: 'POST',
|
||||
data: { code },
|
||||
header: {
|
||||
'content-type': 'application/json',
|
||||
'x-client-type': MINI_PROGRAM_CLIENT_TYPE,
|
||||
'x-client-runtime': MINI_PROGRAM_CLIENT_RUNTIME,
|
||||
'x-client-platform': resolveClientPlatform(),
|
||||
'x-client-instance-id': getClientInstanceId(),
|
||||
'x-mini-program-app-id': MINI_PROGRAM_APP_ID,
|
||||
'x-mini-program-env': MINI_PROGRAM_ENV,
|
||||
},
|
||||
success(response) {
|
||||
if (response.statusCode >= 200 && response.statusCode < 300) {
|
||||
resolve(response.data);
|
||||
return;
|
||||
}
|
||||
const message =
|
||||
response.data &&
|
||||
response.data.error &&
|
||||
response.data.error.message
|
||||
? response.data.error.message
|
||||
: `微信登录失败:${response.statusCode}`;
|
||||
reject(new Error(message));
|
||||
},
|
||||
fail(error) {
|
||||
reject(new Error(error.errMsg || '微信登录请求失败'));
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function resolveAuthResult() {
|
||||
const code = await wxLogin();
|
||||
const response = await requestMiniProgramLogin(code);
|
||||
if (!response || !response.token) {
|
||||
throw new Error('服务器未返回登录态');
|
||||
}
|
||||
return {
|
||||
token: response.token,
|
||||
bindingStatus: response.bindingStatus || 'pending_bind_phone',
|
||||
};
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
errorMessage: '',
|
||||
loading: true,
|
||||
webViewUrl: '',
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
async onLoad() {
|
||||
// 中文注释:web-view 只能打开已配置业务域名;未配置时展示本地提示,避免空白页误判。
|
||||
if (!isConfiguredEntryUrl(WEB_VIEW_ENTRY_URL)) {
|
||||
this.setData({
|
||||
errorMessage: '请先在 miniprogram/config.js 填写 WEB_VIEW_ENTRY_URL。',
|
||||
loading: false,
|
||||
webViewUrl: '',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isConfiguredApiBaseUrl(API_BASE_URL)) {
|
||||
this.setData({
|
||||
errorMessage: '请先在 miniprogram/config.js 填写 API_BASE_URL。',
|
||||
loading: false,
|
||||
webViewUrl: '',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const authResult = await resolveAuthResult();
|
||||
this.setData({
|
||||
errorMessage: '',
|
||||
loading: false,
|
||||
webViewUrl: resolveWebViewUrl(authResult),
|
||||
});
|
||||
} catch (error) {
|
||||
this.setData({
|
||||
errorMessage:
|
||||
error && error.message
|
||||
? error.message
|
||||
: '微信登录失败,请稍后重试。',
|
||||
loading: false,
|
||||
webViewUrl: '',
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
handleRetryLogin() {
|
||||
this.setData({
|
||||
webViewUrl: resolveWebViewUrl(),
|
||||
errorMessage: '',
|
||||
loading: true,
|
||||
webViewUrl: '',
|
||||
});
|
||||
this.onLoad();
|
||||
},
|
||||
|
||||
handleWebViewLoad(event) {
|
||||
|
||||
Reference in New Issue
Block a user