diff --git a/miniprogram/shell/webView.js b/miniprogram/shell/webView.js index a3acaa47d..68e83f50e 100644 --- a/miniprogram/shell/webView.js +++ b/miniprogram/shell/webView.js @@ -26,6 +26,7 @@ const PAY_RESULT_RECHECK_DELAY_MS = 120; const WEB_VIEW_SHARE_TITLE = '陶泥儿'; const WECHAT_LOGIN_UNAVAILABLE_MESSAGE = '微信登录失败,请稍后重试。'; const WECHAT_BIND_PHONE_UNAVAILABLE_MESSAGE = '绑定手机号失败,请稍后重试。'; +const WECHAT_BIND_PHONE_AUTH_REQUIRED_MESSAGE = '需要授权手机号后才能完成绑定。'; function showWebViewShareMenu() { if (typeof wx.showShareMenu !== 'function') { @@ -593,8 +594,9 @@ function createWechatWebViewPage() { const detail = event.detail || {}; if (!detail.code) { + console.error('[web-view] bind phone auth declined', detail); this.setData({ - errorMessage: detail.errMsg || '需要授权手机号后才能完成绑定。', + errorMessage: WECHAT_BIND_PHONE_AUTH_REQUIRED_MESSAGE, }); return; } diff --git a/scripts/check-native-shells.mjs b/scripts/check-native-shells.mjs index 193557412..ec8e329de 100644 --- a/scripts/check-native-shells.mjs +++ b/scripts/check-native-shells.mjs @@ -1955,11 +1955,14 @@ function assertWechatAuthFailureBoundaries() { for (const snippet of [ "WECHAT_LOGIN_UNAVAILABLE_MESSAGE = '微信登录失败,请稍后重试。'", "WECHAT_BIND_PHONE_UNAVAILABLE_MESSAGE = '绑定手机号失败,请稍后重试。'", + "WECHAT_BIND_PHONE_AUTH_REQUIRED_MESSAGE = '需要授权手机号后才能完成绑定。'", "console.error('[web-view] wx.login failed', error)", "console.error('[web-view] mini program login request failed', error)", "console.error('[web-view] mini program bind phone failed', response)", + "console.error('[web-view] bind phone auth declined', detail)", 'errorMessage: WECHAT_LOGIN_UNAVAILABLE_MESSAGE', 'errorMessage: WECHAT_BIND_PHONE_UNAVAILABLE_MESSAGE', + 'errorMessage: WECHAT_BIND_PHONE_AUTH_REQUIRED_MESSAGE', ]) { if (!webViewShellSource.includes(snippet)) { throw new Error(`wechat auth shell must include ${snippet}`); @@ -1970,6 +1973,7 @@ function assertWechatAuthFailureBoundaries() { "reject(new Error(error.errMsg || '微信登录请求失败'))", "reject(new Error(error.errMsg || '绑定手机号请求失败'))", "error && error.message ? error.message : '微信登录失败,请稍后重试。'", + "detail.errMsg || '需要授权手机号后才能完成绑定。'", 'response.data.error.message', ]) { if (webViewShellSource.includes(forbiddenSnippet)) { @@ -1979,8 +1983,10 @@ function assertWechatAuthFailureBoundaries() { for (const snippet of [ '微信登录失败不向页面透出原生错误', '绑定手机号失败不向页面透出后端错误体', + '拒绝手机号授权不向页面透出微信原生错误', "expect(page.data.errorMessage).toBe('微信登录失败,请稍后重试。')", "expect(page.data.errorMessage).toBe('绑定手机号失败,请稍后重试。')", + "expect(page.data.errorMessage).toBe('需要授权手机号后才能完成绑定。')", ]) { if (!authTestSource.includes(snippet)) { throw new Error(`wechat auth boundary test must include ${snippet}`); diff --git a/scripts/miniprogram-web-view-auth.test.ts b/scripts/miniprogram-web-view-auth.test.ts index ae9dcd066..4a692985c 100644 --- a/scripts/miniprogram-web-view-auth.test.ts +++ b/scripts/miniprogram-web-view-auth.test.ts @@ -26,6 +26,9 @@ type MiniProgramPage = { onShareTimeline: () => Record; onShow: () => void; consumePayResult: () => void; + handleGetPhoneNumber: (event: { + detail?: Record; + }) => Promise; }; function createWxMock() { @@ -355,4 +358,27 @@ describe('mini-program web-view auth page', () => { }), ); }); + + test('拒绝手机号授权不向页面透出微信原生错误', async () => { + const wxMock = createWxMock(); + const page = loadWebViewPage(wxMock); + page.data.authResult = { + token: 'jwt-pending-wechat', + bindingStatus: 'pending_bind_phone', + }; + const authDeclined = { + errMsg: 'getPhoneNumber:fail private native detail', + }; + + await page.handleGetPhoneNumber({ + detail: authDeclined, + }); + + expect(page.data.errorMessage).toBe('需要授权手机号后才能完成绑定。'); + expect(page.data.errorMessage).not.toContain('private native detail'); + expect(console.error).toHaveBeenCalledWith( + '[web-view] bind phone auth declined', + authDeclined, + ); + }); });