WxPay.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <template>
  2. <view class="pay-container">
  3. <!-- 加载状态 -->
  4. <view v-if="loading" class="loading-wrap flex-center">
  5. <uni-load-more type="loading" color="#f8c008"></uni-load-more>
  6. <text class="loading-text">正在初始化支付...</text>
  7. </view>
  8. <!-- 订单信息 & 支付区域 -->
  9. <view v-else-if="orderInfo" class="pay-content">
  10. <view class="order-card">
  11. <view class="order-item price-highlight">
  12. <text class="label">支付金额:</text>
  13. <view class="value price">
  14. <text class="currency">¥</text>
  15. <text class="amount-text">{{ orderInfo.amount }}</text>
  16. <text class="unit">元</text>
  17. </view>
  18. </view>
  19. <view class="order-item" v-if="orderInfo.description">
  20. <text class="label">订单描述:</text>
  21. <text class="value">{{ orderInfo.description }}</text>
  22. </view>
  23. <view class="order-item" v-if="orderInfo.orderNo">
  24. <text class="label">订单号:</text>
  25. <text class="value">{{ orderInfo.orderNo }}</text>
  26. </view>
  27. </view>
  28. <!-- 支付按钮 -->
  29. <view class="pay-btn-wrap flex-center" @click="handlePay">
  30. <button class="pay-btn" :disabled="paying">
  31. <text v-if="!paying" class="btn-text">立即支付</text>
  32. <text v-else class="btn-text">支付中...</text>
  33. </button>
  34. </view>
  35. </view>
  36. <!-- 异常提示 -->
  37. <view v-else class="error-wrap flex-center">
  38. <text class="error-text">{{ errorMsg }}</text>
  39. <button class="back-btn" @click="goBackToWebview">返回上一页</button>
  40. </view>
  41. <!-- 微信支付组件-->
  42. <WechatPayment ref="wechatPaymentRef" />
  43. </view>
  44. </template>
  45. <script setup>
  46. import { ref } from "vue";
  47. import { onLoad } from "@dcloudio/uni-app";
  48. import { useAppStore } from "@/stores/app";
  49. import { getUserOpenId } from "@/api/user";
  50. import { Calc } from "@/utils/util";
  51. import { goldPrincipalCompleteOrder } from "@/api/payment";
  52. import {
  53. recyclePreOrderSuccessAPI,
  54. postalDepositCallbackAPI,
  55. } from "@/api/functions";
  56. import WechatPayment from "@/components/payment/WechatPayment.vue";
  57. // 状态管理
  58. const appStore = useAppStore();
  59. const wechatPaymentRef = ref(null);
  60. // 页面核心数据
  61. const loading = ref(true);
  62. const paying = ref(false);
  63. const amount = ref(0);
  64. const orderNo = ref(0);
  65. const description = ref("");
  66. const returnUrl = ref("");
  67. const orderPrefix = ref("");
  68. const orderInfo = ref(null);
  69. const errorMsg = ref("");
  70. onLoad(async (options) => {
  71. try {
  72. console.log("options", options);
  73. amount.value = Number(options.amount) || 0.01;
  74. // console.log("amount", amount.value);
  75. description.value = decodeURIComponent(options.description || "");
  76. orderPrefix.value = options.orderPrefix || "H5";
  77. orderNo.value = options.orderNo || "";
  78. returnUrl.value = options.returnUrl || "";
  79. if (isNaN(amount.value) || amount.value <= 0) {
  80. throw new Error("支付金额异常(需大于0分)");
  81. }
  82. if (!description.value && !orderNo.value) {
  83. throw new Error("订单描述或订单号其中一项不能为空");
  84. }
  85. if (!returnUrl.value) {
  86. throw new Error("支付完成返回地址不能为空");
  87. }
  88. orderInfo.value = {
  89. amount: Calc.div(amount.value, 100).truncate().valueOf(),
  90. description: description.value,
  91. orderNo: orderNo.value,
  92. };
  93. console.log("orderInfo", Calc.div(1, 100).truncate().valueOf());
  94. loading.value = false;
  95. } catch (err) {
  96. loading.value = false;
  97. errorMsg.value = err.message || "页面参数异常";
  98. console.error("页面初始化失败:", err);
  99. }
  100. });
  101. const handlePay = async () => {
  102. if (paying.value) return;
  103. if (!appStore.isLogin) {
  104. uni.showToast({ title: "请先登录", icon: "none" });
  105. return;
  106. }
  107. const userOpenId = appStore.$userInfo?.openId;
  108. if (!userOpenId) {
  109. uni.showToast({ title: "用户信息异常,请重新登录", icon: "none" });
  110. return;
  111. }
  112. paying.value = true;
  113. try {
  114. wechatPaymentRef.value.createUniPay({
  115. amount: amount.value,
  116. description: description.value,
  117. openId: userOpenId,
  118. orderPrefix: orderPrefix.value,
  119. orderNo: orderNo.value,
  120. onUniPayCreate: async (payRes) => {
  121. console.log("支付订单创建成功:", payRes);
  122. },
  123. onUniPaySuccess: async (payStatusRes) => {
  124. console.log("支付成功:", payStatusRes);
  125. await processOrderBizChange(payStatusRes);
  126. paying.value = false;
  127. uni.showToast({
  128. title: "您已支付成功,将返回订单列表",
  129. icon: "success",
  130. });
  131. setTimeout(() => {
  132. goBackToWebview();
  133. }, 1300);
  134. },
  135. onUniPayCancel: () => {
  136. paying.value = false;
  137. uni.showToast({ title: "你已取消支付", icon: "none" });
  138. },
  139. onUniPayFail: (err) => {
  140. paying.value = false;
  141. const errMsg = err || "支付失败,请重试";
  142. uni.showToast({ title: errMsg, icon: "none" });
  143. console.error("支付失败:", err);
  144. },
  145. });
  146. } catch (err) {
  147. paying.value = false;
  148. uni.showToast({ title: "支付接口调用失败", icon: "none" });
  149. console.error("支付调用异常:", err);
  150. }
  151. };
  152. const processOrderBizChange = async (payStatusRes) => {
  153. try {
  154. switch (payStatusRes.orderType) {
  155. case "recyle":
  156. await recyclePreOrderSuccessAPI({
  157. orderNo: payStatusRes.outTradeNo,
  158. payType: "weixin",
  159. });
  160. break;
  161. case "youji":
  162. await postalDepositCallbackAPI({
  163. orderNo: payStatusRes.outTradeNo,
  164. payType: "weixin",
  165. });
  166. break;
  167. case "tl":
  168. await goldPrincipalCompleteOrder({
  169. orderNo: payStatusRes.outTradeNo,
  170. });
  171. break;
  172. case "svip":
  173. break;
  174. }
  175. } catch (error) {
  176. uni.showToast({ title: error || "业务订单状态更改失败", icon: "none" });
  177. console.error("业务订单状态更改失败:", error);
  178. }
  179. };
  180. const goBackToWebview = () => {
  181. if (!returnUrl.value) {
  182. uni.showToast({ title: "返回地址异常", icon: "none" });
  183. return;
  184. }
  185. let finalUrl = returnUrl.value;
  186. try {
  187. if (finalUrl.includes("%")) {
  188. finalUrl = decodeURIComponent(finalUrl);
  189. console.log("finalUrl", finalUrl);
  190. }
  191. } catch (e) {
  192. console.warn("URL解码失败,使用原始URL:", e);
  193. }
  194. // return console.log("goBackToWebview", finalUrl);
  195. uni.redirectTo({
  196. url: `/pages/webview/index?path=${finalUrl}`,
  197. });
  198. };
  199. </script>
  200. <style scoped lang="scss">
  201. $primary: #f8c008;
  202. $primary-dark: #e6b007;
  203. $primary-light: #fff5cc;
  204. $primary-disabled: #fad966;
  205. .pay-container {
  206. min-height: 100vh;
  207. background-color: #fafafa;
  208. padding: 30rpx;
  209. }
  210. // 加载状态
  211. .loading-wrap {
  212. flex-direction: column;
  213. height: 60vh;
  214. .loading-text {
  215. margin-top: 20rpx;
  216. font-size: 28rpx;
  217. color: #666;
  218. }
  219. }
  220. // 订单信息卡片
  221. .pay-content {
  222. .order-card {
  223. background-color: #fff;
  224. border-radius: 20rpx;
  225. padding: 40rpx 32rpx;
  226. margin-bottom: 80rpx;
  227. box-shadow: 0 6rpx 30rpx rgba(0, 0, 0, 0.04);
  228. border: 1px solid $primary-light;
  229. .order-item {
  230. display: flex;
  231. align-items: center;
  232. margin-bottom: 28rpx;
  233. &:last-child {
  234. margin-bottom: 0;
  235. }
  236. .label {
  237. font-size: 28rpx;
  238. color: #777;
  239. width: 160rpx;
  240. font-weight: 500;
  241. }
  242. .value {
  243. font-size: 28rpx;
  244. color: #333;
  245. flex: 1;
  246. word-break: break-all;
  247. }
  248. }
  249. // 🔥 高亮金额区域
  250. .price-highlight {
  251. padding: 12rpx 0;
  252. margin: 12rpx 0 32rpx;
  253. .value.price {
  254. display: flex;
  255. align-items: baseline;
  256. gap: 6rpx;
  257. .currency {
  258. font-size: 32rpx;
  259. color: $primary;
  260. font-weight: 600;
  261. }
  262. // 核心金额数字(最大、最亮)
  263. .amount-text {
  264. font-size: 48rpx;
  265. font-weight: 700;
  266. color: $primary;
  267. letter-spacing: 1rpx;
  268. }
  269. .unit {
  270. font-size: 28rpx;
  271. color: $primary;
  272. font-weight: 500;
  273. margin-left: 4rpx;
  274. }
  275. }
  276. }
  277. }
  278. }
  279. // 支付按钮
  280. .pay-btn-wrap {
  281. .pay-btn {
  282. width: 100%;
  283. height: 96rpx;
  284. line-height: 96rpx;
  285. background-color: $primary;
  286. border: none;
  287. border-radius: 48rpx;
  288. box-shadow: 0 8rpx 24rpx rgba($primary, 0.25);
  289. transition: all 0.2s ease;
  290. .btn-text {
  291. font-size: 34rpx;
  292. color: #fff;
  293. font-weight: 600;
  294. letter-spacing: 2rpx;
  295. }
  296. &:not(:disabled):active {
  297. background-color: $primary-dark;
  298. box-shadow: 0 4rpx 12rpx rgba($primary, 0.2);
  299. transform: translateY(2rpx);
  300. }
  301. &:disabled {
  302. background-color: $primary-disabled;
  303. box-shadow: none;
  304. }
  305. }
  306. }
  307. // 异常提示
  308. .error-wrap {
  309. flex-direction: column;
  310. height: 60vh;
  311. .error-text {
  312. font-size: 30rpx;
  313. color: #e64340;
  314. margin-bottom: 40rpx;
  315. text-align: center;
  316. padding: 0 30rpx;
  317. line-height: 44rpx;
  318. }
  319. .back-btn {
  320. width: 280rpx;
  321. height: 76rpx;
  322. line-height: 76rpx;
  323. background-color: #fff;
  324. border: 1px solid $primary;
  325. border-radius: 38rpx;
  326. font-size: 28rpx;
  327. color: $primary;
  328. transition: all 0.2s ease;
  329. &:active {
  330. background-color: $primary-light;
  331. }
  332. }
  333. }
  334. .flex-center {
  335. display: flex;
  336. justify-content: center;
  337. align-items: center;
  338. }
  339. </style>