From 50b3e2452cce0a3d129eb7f7110394d57e5afb1a Mon Sep 17 00:00:00 2001 From: moyayu <1623253835@qq.com> Date: Fri, 15 May 2026 19:33:56 +0800 Subject: [PATCH] i18n --- .husky/commit-msg | 2 +- src/api/wx.js | 8 ++++++-- src/hooks/usePayment.js | 23 +++++++++++++++-------- src/i18n/instance.js | 24 ++++++++++++++++++++++-- src/pages/cart/espier-checkout.js | 3 +++ 5 files changed, 47 insertions(+), 13 deletions(-) diff --git a/.husky/commit-msg b/.husky/commit-msg index fe4c17a22..0ae4bda55 100755 --- a/.husky/commit-msg +++ b/.husky/commit-msg @@ -1,4 +1,4 @@ #!/bin/sh . "$(dirname "$0")/_/husky.sh" -npx --no-install commitlint --edit "" +npx --no-install commitlint --edit "${1}" diff --git a/src/api/wx.js b/src/api/wx.js index b24f3943b..283563b2b 100644 --- a/src/api/wx.js +++ b/src/api/wx.js @@ -10,8 +10,12 @@ export function getOpenid({ code }) { return req.get('/oauth/getopenid', { code }) } -export function getredirecturl({ url }) { - return req.get('/oauth/getredirecturl', { url }) +export async function getredirecturl({ url }) { + const data = await req.get('/oauth/getredirecturl', { url }) + /* 调试:getredirecturl 已返回,调用方赋值 window.location 前暂停,可查看 data / redirect_url */ + // eslint-disable-next-line no-debugger + debugger + return data } export function info(data) { diff --git a/src/hooks/usePayment.js b/src/hooks/usePayment.js index 9456c8b47..822507385 100644 --- a/src/hooks/usePayment.js +++ b/src/hooks/usePayment.js @@ -189,23 +189,30 @@ export default (props = {}) => { // 微信H5 JSDK const wxpayjsPay = async (params, orderInfo) => { - // const $instance = getCurrentInstance() || {} - const { order_id, code } = router?.params + const route = router?.params || {} + const oid = route.order_id || orderInfo?.order_id || route.id + const { code, source: routeSource } = route + if (!code) { - // 微信客户端code授权 + // 微信客户端 code 授权(订单详情页等场景 URL 仅有 order_id,无 code) const loc = window.location - // const url = `${loc.protocol}//${loc.host}/pages/cart/cashier-result?order_id=${orderId}` - const url = `${loc.protocol}//${loc.host}/pages/cart/cashier-weapp?order_id=${orderId}&source=${source}` - let { redirect_url } = await api.wx.getredirecturl({ url }) + const sourceQuery = + routeSource != null && String(routeSource) !== '' + ? `&source=${encodeURIComponent(String(routeSource))}` + : '' + const url = `${loc.protocol}//${loc.host}/pages/cart/cashier-weapp?order_id=${oid}${sourceQuery}` + const { redirect_url } = await api.wx.getredirecturl({ url }) window.location.href = redirect_url + return } + const { open_id } = await api.wx.getOpenid({ code }) const { pay_channel } = params const { pay_type, order_type = 'normal' } = orderInfo let query = { pay_type, pay_channel, - order_id, + order_id: oid, order_type, open_id } @@ -227,7 +234,7 @@ export default (props = {}) => { function (res) { console.log(res) const loc = window.location - window.location.href = `${loc.protocol}//${loc.host}${cashierResultUrl}?order_id=${order_id}` + window.location.href = `${loc.protocol}//${loc.host}${cashierResultUrl}?order_id=${oid}` } ) } diff --git a/src/i18n/instance.js b/src/i18n/instance.js index 6b0652d5b..8541ba3ce 100644 --- a/src/i18n/instance.js +++ b/src/i18n/instance.js @@ -2,7 +2,7 @@ * Copyright © ShopeX (http://www.shopex.cn). All rights reserved. * See LICENSE file for license details. * - * i18next 单例:语言资源通过 subpages/i18n/resources 分包异步加载。 + * i18next 单例:weapp 经 subpages/i18n/resources 分包异步加载;H5 主包同步 require 同目录 resources。 */ /* eslint-disable import/no-named-as-default-member -- i18next 默认导出即实例,需调用 .use / .changeLanguage */ import i18n from 'i18next' @@ -126,7 +126,27 @@ function loadLocalePackage() { }) } - return Promise.reject(new Error('i18n subPackage resource loading is only enabled for weapp')) + // H5:语言包由 webpack 打进主包,同步 require 即可(weapp 依赖分包异步加载,勿在此 require 以免主包膨胀) + if (process.env.TARO_ENV === 'h5') { + const Taro = getTaro() + try { + const resources = require('../subpages/i18n/resources') + if (Taro && resources && (resources.zhcn || resources.en || resources.ar)) { + Taro.__i18nResources = { + ...(Taro.__i18nResources || {}), + ...resources + } + return Promise.resolve() + } + return Promise.reject(new Error('i18n H5 resources empty')) + } catch (e) { + return Promise.reject(e) + } + } + + return Promise.reject( + new Error(`i18n resource loading unsupported for TARO_ENV=${process.env.TARO_ENV}`) + ) } if (!i18n.isInitialized) { diff --git a/src/pages/cart/espier-checkout.js b/src/pages/cart/espier-checkout.js index 5a1271f40..1f158e516 100644 --- a/src/pages/cart/espier-checkout.js +++ b/src/pages/cart/espier-checkout.js @@ -378,6 +378,9 @@ function CartCheckout(props) { params.pay_channel == 'wx_pub' && isWxWeb) ) { + /* 调试:公众号 JSAPI(wxpayjs) — 下单成功 → 跳转 OAuth/收银台前暂停 */ + // eslint-disable-next-line no-debugger + debugger // 微信客户端code授权 const loc = window.location // const url = `${loc.protocol}//${loc.host}/pages/cart/cashier-result?order_id=${orderId}`