fix: switch miniprogram channel by env version
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
|
||||
const {
|
||||
API_BASE_URL,
|
||||
DEV_API_BASE_URL,
|
||||
DEV_WEB_VIEW_ENTRY_URL,
|
||||
MINI_PROGRAM_APP_ID,
|
||||
MINI_PROGRAM_ENV,
|
||||
WEB_VIEW_ENTRY_URL,
|
||||
@@ -105,6 +107,68 @@ function parseBooleanQueryFlag(value) {
|
||||
return value === true || value === '1' || value === 'true' || value === 'yes';
|
||||
}
|
||||
|
||||
function normalizeMiniProgramEnv(value) {
|
||||
const normalized = String(value || '').trim().toLowerCase();
|
||||
if (normalized === 'release') {
|
||||
return 'release';
|
||||
}
|
||||
if (normalized === 'trial') {
|
||||
return 'trial';
|
||||
}
|
||||
if (
|
||||
normalized === 'develop' ||
|
||||
normalized === 'development' ||
|
||||
normalized === 'dev'
|
||||
) {
|
||||
return 'dev';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
function readMiniProgramEnvVersion() {
|
||||
if (typeof wx.getAccountInfoSync !== 'function') {
|
||||
return '';
|
||||
}
|
||||
try {
|
||||
const accountInfo = wx.getAccountInfoSync();
|
||||
return (
|
||||
accountInfo &&
|
||||
accountInfo.miniProgram &&
|
||||
accountInfo.miniProgram.envVersion
|
||||
);
|
||||
} catch (error) {
|
||||
console.warn('[web-view] read mini program env failed', error);
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
function resolveMiniProgramRuntimeConfig() {
|
||||
const miniProgramEnv =
|
||||
normalizeMiniProgramEnv(readMiniProgramEnvVersion()) ||
|
||||
normalizeMiniProgramEnv(MINI_PROGRAM_ENV) ||
|
||||
'release';
|
||||
const useReleaseChannel = miniProgramEnv === 'release';
|
||||
const webViewEntryUrl = useReleaseChannel
|
||||
? WEB_VIEW_ENTRY_URL
|
||||
: DEV_WEB_VIEW_ENTRY_URL || WEB_VIEW_ENTRY_URL;
|
||||
const apiBaseUrl = useReleaseChannel
|
||||
? API_BASE_URL
|
||||
: DEV_API_BASE_URL || API_BASE_URL;
|
||||
const sourceQuery = {
|
||||
...WEB_VIEW_SOURCE_QUERY,
|
||||
};
|
||||
if (!useReleaseChannel) {
|
||||
sourceQuery.miniProgramEnv = miniProgramEnv;
|
||||
}
|
||||
|
||||
return {
|
||||
apiBaseUrl,
|
||||
miniProgramEnv,
|
||||
sourceQuery,
|
||||
webViewEntryUrl,
|
||||
};
|
||||
}
|
||||
|
||||
function shouldStartAuthFromQuery(query) {
|
||||
return String((query && query.authAction) || '').trim() === AUTH_ACTION_LOGIN;
|
||||
}
|
||||
@@ -114,12 +178,13 @@ function shouldReturnToPreviousPage(query) {
|
||||
}
|
||||
|
||||
function resolveWebViewUrl(authResult) {
|
||||
const entryUrl = String(WEB_VIEW_ENTRY_URL || '').trim();
|
||||
const runtimeConfig = resolveMiniProgramRuntimeConfig();
|
||||
const entryUrl = String(runtimeConfig.webViewEntryUrl || '').trim();
|
||||
if (!isConfiguredEntryUrl(entryUrl)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const sourcedUrl = appendQuery(entryUrl, WEB_VIEW_SOURCE_QUERY);
|
||||
const sourcedUrl = appendQuery(entryUrl, runtimeConfig.sourceQuery);
|
||||
if (!authResult || !authResult.token) {
|
||||
return sourcedUrl;
|
||||
}
|
||||
@@ -205,7 +270,8 @@ function wxLogin() {
|
||||
|
||||
function requestMiniProgramLogin(code) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const apiBaseUrl = trimTrailingSlash(API_BASE_URL);
|
||||
const runtimeConfig = resolveMiniProgramRuntimeConfig();
|
||||
const apiBaseUrl = trimTrailingSlash(runtimeConfig.apiBaseUrl);
|
||||
if (!isConfiguredApiBaseUrl(apiBaseUrl)) {
|
||||
reject(new Error('请先配置 API_BASE_URL'));
|
||||
return;
|
||||
@@ -222,7 +288,7 @@ function requestMiniProgramLogin(code) {
|
||||
'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,
|
||||
'x-mini-program-env': runtimeConfig.miniProgramEnv,
|
||||
},
|
||||
success(response) {
|
||||
if (response.statusCode >= 200 && response.statusCode < 300) {
|
||||
@@ -246,7 +312,8 @@ function requestMiniProgramLogin(code) {
|
||||
|
||||
function requestMiniProgramBindPhone(authToken, wechatPhoneCode) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const apiBaseUrl = trimTrailingSlash(API_BASE_URL);
|
||||
const runtimeConfig = resolveMiniProgramRuntimeConfig();
|
||||
const apiBaseUrl = trimTrailingSlash(runtimeConfig.apiBaseUrl);
|
||||
if (!isConfiguredApiBaseUrl(apiBaseUrl)) {
|
||||
reject(new Error('请先配置 API_BASE_URL'));
|
||||
return;
|
||||
@@ -264,7 +331,7 @@ function requestMiniProgramBindPhone(authToken, wechatPhoneCode) {
|
||||
'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,
|
||||
'x-mini-program-env': runtimeConfig.miniProgramEnv,
|
||||
},
|
||||
success(response) {
|
||||
if (response.statusCode >= 200 && response.statusCode < 300) {
|
||||
@@ -312,8 +379,9 @@ Page({
|
||||
async onLoad(query = {}) {
|
||||
this._lastLaunchQuery = query;
|
||||
showWebViewShareMenu();
|
||||
const runtimeConfig = resolveMiniProgramRuntimeConfig();
|
||||
// 中文注释:web-view 只能打开已配置业务域名;未配置时展示本地提示,避免空白页误判。
|
||||
if (!isConfiguredEntryUrl(WEB_VIEW_ENTRY_URL)) {
|
||||
if (!isConfiguredEntryUrl(runtimeConfig.webViewEntryUrl)) {
|
||||
this.setData({
|
||||
errorMessage: '请先在 miniprogram/config.js 填写 WEB_VIEW_ENTRY_URL。',
|
||||
loading: false,
|
||||
@@ -336,7 +404,7 @@ Page({
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isConfiguredApiBaseUrl(API_BASE_URL)) {
|
||||
if (!isConfiguredApiBaseUrl(runtimeConfig.apiBaseUrl)) {
|
||||
this.setData({
|
||||
errorMessage: '请先在 miniprogram/config.js 填写 API_BASE_URL。',
|
||||
loading: false,
|
||||
|
||||
Reference in New Issue
Block a user