uni-app app跳转小程序,在小程序进行微信支付,支付成功小程序再返回APP

📅 2026/6/24 3:23:36
uni-app app跳转小程序,在小程序进行微信支付,支付成功小程序再返回APP
1.APP跳转小程序goWX(payConfirmList){//小程序跳转路径 希携带参数 const miniProgramPathpayPages/pages/pay/payConfirm;const pathminiProgramPath ?payListpayConfirmList flagappselectShopJSON.stringify(uni.getStorageSync(presentShop))openiduni.getStorageSync(CUSTOMER_USER_INFO_DY).user.openid mer_iduni.getStorageSync(CUSTOMER_USER_INFO_DY).user.mer_id mer_nameuni.getStorageSync(CUSTOMER_USER_INFO_DY).user.mer_name apptokenuni.getStorageSync(CUSTOMER_USER_INFO_DY).token;console.log([goWX] launch, miniProgramId, path);this.showGoWXLoading();plus.share.getServices((services){const weixin(services||[]).find((item)item.idweixin);if(!weixin){uni.showModal({title:微信 SDK 未就绪, content:请使用「自定义调试基座」运行并确认 manifest 已配置微信分享, showCancel:false});return;}if(typeof weixin.launchMiniProgram!function){uni.showModal({title:不支持跳转小程序, content:请重新制作自定义调试基座, showCancel:false});return;}weixin.launchMiniProgram({id: miniProgramId,//需是原生gh_开头的原生微信appid path, type:2//小程序版本0-正式版1-测试版2-体验版。},(){this.hideGoWXLoading();console.log([goWX] launchMiniProgram success);},(err){this.hideGoWXLoading();console.log([goWX] launchMiniProgram fail, err);uni.showModal({title:打开小程序失败, content:(err(err.message||err.code))||JSON.stringify(err||{}), showCancel:false});});},(err){this.hideGoWXLoading();console.log([goWX] getServices fail, err);uni.showModal({title:获取分享服务失败, content: JSON.stringify(err||{}), showCancel:false});});},2.小程序获取APP参数支付onLoad获取参数创建支付订单createOrder(){uni.showLoading({title:支付中, mask:true})letdata{openid: this.openid, // openid:opP7A5b-k_ibUp0cr7RF853FCC6M, charge_list_ids: this.selectId, //物业缴费项id多个英文逗号隔开, pay_mode:扫码支付, pay_channel:wx_lite, mer_id: this.mer_id, mer_name: this.mer_name, able_pay_amt: this.totalCount, goods_title:市场管理费, goods_desc:市场管理费, device_type:1, unit_area: this.payList[0].unit_area_name, unit_location: this.payList[0].unit_location_name, unit_no: this.payList[0].unit_no, unit_id: this.payList[0].unit_id}this.Util.postRequest(index/system/wycharge/order/create, data, this.isFromApp()? this.apptoken:undefined).then(res{uni.hideLoading()if(res.status200){letpayInfores.data;if(res.message您还有待支付的订单){console.log(res)wx.requestPayment({timeStamp:payInfo.pay_info.timeStamp, //时间戳nonceStr:payInfo.pay_info.nonceStr, //随机字符串package:payInfo.pay_info.package, //prepay_id 参数值signType:payInfo.pay_info.signType,paySign:payInfo.pay_info.paySign, //签名success:(res){this.handlePaySuccess()},})}else{wx.requestPayment({timeStamp:payInfo.timeStamp, //时间戳nonceStr:payInfo.nonceStr, //随机字符串package:payInfo.package, //prepay_id 参数值signType:payInfo.signType,paySign:payInfo.paySign, //签名success:(res){this.handlePaySuccess()},fail:function(res){console.log(res)}})}}else{uni.showModal({title:提示, content: res.message, showCancel:false})}})3.支付成功返回APPhandlePaySuccess(){//区分小程序支付还是app跳转来支付if(this.isFromApp()){//返回APP通知APP支付成功参数 this.appParameterJSON.stringify({paySuccess:true})this.showAppPaySuccesstrue}else{uni.showModal({title:提示, content:支付成功, showCancel: false, success:(res){if(res.confirm){letpagesgetCurrentPages()letbeforePagepages[pages.length -2]uni.navigateBack({success:function(){beforePage.onLoad()}})}}})}},app跳转来的支付不能使用uni.showModal返回APP需要手动点击返回按钮才能正确返回APP!-- App支付成功弹框用户主动点击返回App --viewclasspay-success-maskv-ifshowAppPaySuccessclick.stopviewclasspay-success-modalviewclasspay-success-icon-wrapviewclasspay-success-icon✓/view/viewviewclasspay-success-title支付成功/viewviewclasspay-success-content请点击下方按钮返回 App/viewviewclasspay-success-footerbuttonclasslaunch-app-btnopen-typelaunchApp:app-parameterappParameterlaunchapponLaunchApperroronLaunchAppError返回 App/button/view/view/viewshowAppPaySuccess: false, appParameter:, onLaunchApp(e){console.log(launchApp success, e)this.showAppPaySuccessfalse}, onLaunchAppError(e){console.error(launchApp fail, e)uni.showModal({title:提示, content:返回App失败请手动关闭小程序, showCancel:false})},4.APP接收小程序支付成功参数miniProgramPayReturn.js//#ifdef APP-PLUS/** * 小程序 launchApp 回传 app-parameter对应微信 SDK onResp.extMsg * Android: WXEntryActivity.onResp -WXLaunchMiniProgram.Resp.extMsg * iOS: WXApiDelegate.onResp -WXLaunchMiniProgramResp.extMsg * uni-app 云端打包会把 extMsg 桥接到 JSAndroid/iOS 读取方式略有差异 */ /** 解析 extMsg / extraData 字符串 */exportfunctionparseMiniProgramExtMsg(extMsg){if(extMsgnull||extMsg)returnnullif(typeof extMsgobject)returnextMsg const rawString(extMsg).trim()if(!raw)returnnull try{returnJSON.parse(raw)}catch(e){if(raw.includes(paySuccess)){return{paySuccess: raw.includes(true)||raw.includes(paySuccess:true)||raw.includes(paySuccess1)}}returnnull}}/** Android onResp.extMsg优先 arguments后台唤醒时再从 Intent 补读 */exportfunctiongetAndroidMiniProgramExtMsg(){letextMsgplus.runtime.argumentsif(extMsg)returnextMsg try{const mainplus.android.runtimeMainActivity()const intentplus.android.invoke(main,getIntent)if(!intent)returnconst keys[_wxapi_launch_mini_program_resp_ext_msg,wx_launch_mini_program_ext_msg,extMsg,APP_PARAMETER]for(let i0;ikeys.length;i){const valueplus.android.invoke(intent,getStringExtra, keys[i])if(value)returnvalue}}catch(e){console.log([miniProgramPayReturn] android extMsg read fail, e)}return}/** iOS onResp.extMsguni 桥接至 plus.runtime.arguments */exportfunctiongetIOSMiniProgramExtMsg(){returnplus.runtime.arguments||}exportfunctionclearMiniProgramExtMsg(){plus.runtime.argumentsnull plus.runtime.arguments}/** 微信文档从 App 打开的小程序返回时 launcher 为 miniProgram */exportfunctionisFromMiniProgramReturn(){returnplus.runtime.launcherminiProgram}exportfunctiongetMiniProgramReturnDelayMs(){const osNameuni.getSystemInfoSync().osNamereturnosNameios?500:200}/** 按平台读取 onResp.extMsg 并解析为 extraData */exportfunctionreadMiniProgramPayExtraData(){const osNameuni.getSystemInfoSync().osNameletextMsgif(osNameandroid){extMsggetAndroidMiniProgramExtMsg()}elseif(osNameios){extMsggetIOSMiniProgramExtMsg()}else{extMsgplus.runtime.arguments||}if(!extMsg)returnnull // 有 extMsg 即视为小程序回传launcher 仅作辅助日志if(!isFromMiniProgramReturn()){console.log([miniProgramPayReturn] extMsg without miniProgram launcher, plus.runtime.launcher)}returnparseMiniProgramExtMsg(extMsg)}//#endifapp.vueimport{readMiniProgramPayExtraData, clearMiniProgramExtMsg, getMiniProgramReturnDelayMs}from./common/miniProgramPayReturn.jsonLaunch:function(){plus.globalEvent.addEventListener(newintent,(){this.handleMiniProgramPayReturn();});}onShow: function(options){/** App 从小程序返回Android onResp.extMsg / iOS onResp.extMsg */handleMiniProgramPayReturn(){const delaygetMiniProgramReturnDelayMs()setTimeout((){const extraDatareadMiniProgramPayExtraData()if(extraDataextraData.paySuccess){uni.$emit(miniProgramPayResult, extraData)}clearMiniProgramExtMsg()}, delay)},}跳转小程序页处理onLoad(){this.selectShopuni.getStorageSync(presentShop)this.menusList.forEach((list){list.listData[]})uni.$on(miniProgramPayResult, this.onMiniProgramPayResult)this.payDatail()},onUnload(){uni.$off(miniProgramPayResult, this.onMiniProgramPayResult)}, methods:{onMiniProgramPayResult(extraData){if(extraDataextraData.paySuccess){this.refreshAfterPaySuccess()}},refreshAfterPaySuccess(){uni.showToast({title:支付成功, icon:success})this.menusList.forEach((list){list.listData[]})if(this.companyList.length){this.payListDetail()}this.hideModal()},}