"use client"; import React, { useState, useEffect } from "react"; import { useRouter, useSearchParams } from "next/navigation"; import { Toast } from "antd-mobile"; import { generateSignature } from "@/utils/crypto"; export default function InWeixin({ params }) { const router = useRouter(); const searchParams = useSearchParams(); //微信支付jsapi支付参数 const [jsapiParams, setJsapiParams] = useState(); //用户支付成功 const [success, setSuccess] = useState(false); //获取微信支付jsapi支付参数 useEffect(() => { const createOrder = async () => { //获取code const code = searchParams.get("code"); //构建body const strBody = decodeURIComponent(params.body); const temBody = JSON.parse(strBody); const body = { ...temBody, wechat_auth_code: code }; const signature = generateSignature(body); try { const response = await fetch( `/api/vas/create_order?signature=${signature}`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(body), } ); const data = await response.json(); if (data.ret === -1) { Toast.show({ content: data.msg, }); return; } setJsapiParams(data.data.wxpay_jsapi_param_obj); } catch (error) { console.error(error); } }; createOrder(); }, []); //调起收银台 useEffect(() => { if (!jsapiParams) return; function onBridgeReady() { WeixinJSBridge.invoke( "getBrandWCPayRequest", { appId: jsapiParams.appId, //公众号ID,由商户传入 timeStamp: jsapiParams.timeStamp, //时间戳,自1970年以来的秒数 nonceStr: jsapiParams.nonceStr, //随机串 package: jsapiParams.package, signType: jsapiParams.signType, //微信签名方式: paySign: jsapiParams.paySign, //微信签名 }, function (res) { if (res.err_msg == "get_brand_wcpay_request:ok") { setSuccess(true); } } ); } if (typeof WeixinJSBridge == "undefined") { if (document.addEventListener) { document.addEventListener("WeixinJSBridgeReady", onBridgeReady, false); } else if (document.attachEvent) { document.attachEvent("WeixinJSBridgeReady", onBridgeReady); document.attachEvent("onWeixinJSBridgeReady", onBridgeReady); } } else { onBridgeReady(); } }, [jsapiParams]); return (

微信安全支付


{success ? "充值成功,请返回APP查看结果" : "正在跳转..."}

); }