基于TP6+Uniapp开发微信支付商家转账API最新对接教程(2025年11月)下篇:微信小程序客户端开发实践(uni-app)

  • 原创
  • 作者:程序员三丰
  • 发布时间:2025-12-04 13:59
  • 浏览量:39
最近刚对接了微信官方商家转账API,基于此整理了3篇系列文档,从阅读文档到获取配置,再到开发实践整个过程,以作备忘。本文主要分享基于unia-app开发微信小程序客户商户提现功能页面。

UI效果

template 模板代码

<template>
    <view class="page-wrapper">
        <view class="flex-row justify-end pd-t30 pd-r28">
            <view>
                <u-text text="查看提现记录" size="12" type="primary" @click="gotoWithdrawDetails"></u-text>
            </view>
        </view>
        <view class="pd-t30 pd-b10 pd-l20 pd-r20">
            <view class="card-block flex-row justify-between items-center balance">
                <view>
                    <u--text size="13" text="可提现余额" margin="0 0 10px 0"></u--text>
                    <view>
                        <u--text mode="price" size="22" bold :text="merchant_info.balance"></u--text>
                    </view>
                </view>
            </view>

            <view class="card-block mt-20">
                <view class="">
                    <u--text size="13" text="提现金额" margin="0 0 10px 0"></u--text>
                    <u-input v-model="withdraw_amount" type="digit" placeholder="请输入提现金额" border="bottom" clearable>
                        <template slot="prefix">
                            <u--text text="¥" slot="prefix" margin="0 3px 0 0"></u--text>
                        </template>
                    </u-input>
                </view>
            </view>

            <view class="card-block mt-20">
                <view class="">
                    <u--text size="13" text="真实姓名" margin="0 0 10px 0"></u--text>
                    <u-input v-model="realname" placeholder="请输入真实姓名" border="bottom" clearable></u-input>
                </view>
            </view>

            <view class="card-block mt-20">
                <u--text size="13" text="提现到:" margin="0 0 16px 0"></u--text>
                <view class="flex-row justify-between items-center">
                    <view class="flex-row justify-between items-center">
                        <image src="/static/wechat-pay.png" style="width: 16px; height: 16px; margin-right: 4px;"
                            mode="heightFix" />
                            <u--text size="13" text="微信零钱"></u--text>
                    </view>
                    <view>
                        <u-radio-group v-model="pay_type" iconPlacement="right">
                            <u-radio shape="circle" activeColor="#ff622e" name="wechat"></u-radio>
                        </u-radio-group>
                    </view>
                </view>
            </view>
        </view>

        <!-- 底部工具栏 -->
        <view class="bottom-operation-bar">
            <view class="submit-button" @click="handleSubmit()">
                <text>立即申请</text>
            </view>
        </view>
    </view>
</template>

style 样式代码

<style scoped lang="scss">
.page-wrapper {
    background-color: #f4f4f4;
    min-height: 100vh;
    padding-bottom: 130rpx;


    .card-block {
        background-color: #fff;
        padding: 26rpx 30rpx;
        border-radius: 20rpx;
    }

    .balance {
        ::v-deep .u-text {
            align-items: baseline;

            .u-text__price {
                font-size: 18px !important;
            }
        }
    }

    .bottom-operation-bar {
        position: fixed;
        width: 100vw;
        box-sizing: border-box;
        bottom: 0;
        background: #ffffff;
        padding: 20rpx 40rpx;
        z-index: 99;

        .submit-button {
            padding: 18rpx 0;
            background-color: #ff622e;
            border-radius: 52rpx;
            height: 90rpx;
            color: #fff;
            font-size: 35rpx;
            font-family: PingFangSC;
            text-align: center;
        }
    }
}
</style>

script 脚本代码

<script>
    export default {
        data() {
            return {
                merchant_info: {},
                withdraw_amount: null,
                realname: '',
                pay_type: 'wechat',
                request_lock: false,
                trans_resp_data: {}
            }
        },
        onLoad(option) {
            console.log('商户提现 onload: ', option);
            if (option?.merchant_id) {
                this.merchant_id = option.merchant_id;
            }
        },
        onShow() {
            this.getMerchantInfo();
            this.withdraw_amount = '';
            this.realname = '';
        },
        methods: {
            getMerchantInfo() {
                // 获取商户信息,主要是获取商户余额用于提现
                this.$api.sendRequest({
                    url: '/api/merchant/detail',
                    data: { merchant_id: this.merchant_id },
                    success: res => {
                        if (res.code == 0) {
                            this.merchant_info = res.data;
                        }
                    }
                });
            },
            handleSubmit() {
                const amount = parseFloat(this.withdraw_amount)
                if (isNaN(amount) || (amount) <= 0) {
                    this.$util.showToast({ title: '请输入提现金额' });
                    return false;
                }

                if (amount > this.merchant_info.balance) {
                    this.$util.showToast({ title: '提现金额不能大于可提现余额' });
                    return false;
                }

                if (this.realname.trim() == '') {
                    this.$util.showToast({ title: '请输入真实姓名' });
                    return false;
                }

                if (this.request_lock) {
                    return false;
                }
                this.request_lock = true;

                uni.showLoading({
                    title: '请求提现中'
                })

                this.$api.sendRequest({
                    url: '/api/merchant/withdraw',
                    data: {
                        amount,
                        realname: this.realname
                    },
                    success: res => {
                        this.request_lock = false;
                        uni.hideLoading();
                        if (res.code == 0) {
                            // 返回结果中必须包含跳转微信支付收款页的 package_info 信息,JSAPI调起用户确认收款时需要使用该参数。
                            this.trans_resp_data = res.data;

                            // 发起提现
                            this.callbackUserConfirm();
                        } else {
                            this.$util.showToast({ title: res.data ? res.data : res.message });
                        }
                    }
                });
            },
            callbackUserConfirm() {
                if (!this.trans_resp_data.mch_id || !this.trans_resp_data.package_info) {
                    this.$util.showToast({ title: 'api异常,提现操作失败' });
                    return false;
                }
                const _this = this;
                // #ifdef MP-WEIXIN
                if (wx.canIUse('requestMerchantTransfer')) {
                    wx.requestMerchantTransfer({
                        mchId: this.trans_resp_data.mch_id,
                        appId: wx.getAccountInfoSync().miniProgram.appId,
                        package: this.trans_resp_data.package_info,
                        success: (res) => {
                            // res.err_msg将在页面展示成功后返回应用时返回ok,并不代表付款成功(一般情况下都能成功,更可靠的解决方案是:结合异步通知,以及下一步跳转到转账结果页后查询转账状态)
                            console.log('success:', res);

                            // 跳转自定义的转账结果页
                            _this.gotoResult();
                        },
                        fail: (res) => {
                            console.log('fail:', res);
                        },
                    });
                } else {
                    wx.showModal({
                        content: '你的微信版本过低,请更新至最新版本。',
                        showCancel: false,
                    });
                }
                // #endif
            },
            gotoResult() {
                // 跳转到提现结果页面
                this.$util.redirectTo('/pagesMerchant/mcenter/withdraw_result', { withdraw_amount: this.withdraw_amount })
            },
            gotoWithdrawDetails() {
                // 跳转到提现记录页面
                this.$util.redirectTo('/pagesMerchant/mcenter/withdraw_details');
            }
        }
    }
</script>
声明:本文为原创文章,51blog.xyz和作者拥有版权,如需转载,请注明来源于51blog.xyz并保留原文链接:https://mp.51blog.xyz/article/99.html

文章归档

推荐文章

buildadmin logo
Thinkphp8 Vue3 Element PLus TypeScript Vite Pinia

🔥BuildAdmin是一个永久免费开源,无需授权即可商业使用,且使用了流行技术栈快速创建商业级后台管理系统。

热门标签

PHP ThinkPHP ThinkPHP5.1 Go Mysql Mysql5.7 Redis Linux CentOS7 Git HTML CSS CSS3 Javascript JQuery Vue LayUI VMware Uniapp 微信小程序 docker wiki Confluence7 学习笔记 uView ES6 Ant Design Pro of Vue React ThinkPHP6.0 chrome 扩展 翻译工具 Nuxt SSR 服务端渲染 scrollreveal.js ThinkPHP8.0 Mac webman 跨域CORS vscode GitHub ECharts Canvas vue3 three.js 微信支付 PHP全栈开发