React Js 微信支付 简单封装

本文出自:

http://blog.csdn.net/wyk304443164/

/**
 * Created by wuyakun on 2017/5/8.
 */

import common from './common';
import Fetch from './FetchIt';
import API_URL from "./url";

let Pay = {};

/**
 * 支付的回调
 */
let payCallback;

/**
 * 开始支付--这个方法其实是从后台请求微信支付签名。成功继续,失败回调
 * @param _order_id 传order_id即可
 * @param _thisObj 传this即可
 * @param _payType 支付类型--暂时只有微信
 * @param _callback 回调 true or false
 */

Pay.done = function (_order_id, _thisObj, _payType, _callback) {
    let postData = {
        order_id: _order_id,
    };
    if (typeof _callback === "function") {
        payCallback = _callback;
    }
    Fetch.postObjData(API_URL.mobile.payUrl, postData).then((data) => {
        this.doneResponse(data);
    }).catch(() => {
        payCallback(false);
    });
};

Pay.doneResponse = function (result) {
    this.callPay(result);
};

Pay.callPay = function (code) {
    if (typeof WeixinJSBridge === "undefined") {
        if (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', this.jsApiCall, false);
        } else if (document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', this.jsApiCall);
            document.attachEvent('onWeixinJSBridgeReady', this.jsApiCall);
        }
    } else {
        this.jsApiCall(code);
    }
};

Pay.jsApiCall = function (code) {
    WeixinJSBridge.invoke('getBrandWCPayRequest', code, function (res) {
        if (!common.isNull(res) && !common.isNull(res.err_msg) && res.err_msg.indexOf('ok') > 0) {
            payCallback(true);
        } else {
            payCallback(false);
        }
    });
};

//退款---------------------没有了!!!!!!!!!!
let Refund = {};

//退款
Refund.done = function (order_id, callback) {
    let postData = {
        order_id: order_id,
    };
    Fetch.postObjData(API_URL.mobile.refundUrl, postData).then((data) => {
        console.log(data);
        if (typeof callback === "function") {
            callback();
        }
        //需要判断是否成功
    });
};

export {Pay as default, Refund}

调用也很简单

import Pay from '../../common/PayRefund';

startPay = () => {
        let id = '7018253979853';
        let _that = this;

        Pay.done(id, _that, 1, is_true => {
            if (is_true) {
                // 支付成功后的逻辑
                alert('支付成功');
            } else {
                // 支付失败后的逻辑
                alert('支付失败');
            }
        });
    };

其中this和payType其实并没有用到,只是方便以后使用,退款是直接访问后台,功能去掉了。 我就没写。需要的小伙伴自己添加即可。别忘了引入

<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
React封装微信小程序授权函数通常涉及以下几个步骤: 1. 创建一个函数来处理授权逻辑,这个函数可以调用微信小程序提供的API,比如`wx.getSetting`和`wx.authorize`。 2. 在函数中,首先调用`wx.getSetting`来获取用户的授权设置,此API会返回用户的权限状态。 3. 如果用户已经授权,则直接返回授权结果;如果用户未授权,则通过`wx.authorize`请求用户授权。 4. 在`wx.authorize`中,可以设定`scope`参数,指定需要请求的权限。 5. 设置授权成功或失败的回调函数,以处理授权结果,并将结果返回给调用者。 以下是一个简单封装示例: ```javascript const useWeChatAuthorize = (scope = 'scope.userLocation') => { const [authorizeStatus, setAuthorizeStatus] = useState('unknown'); const handleAuthorize = () => { wx.getSetting({ success(res) { if (!res.authSetting(scope)) { // 请求用户授权 wx.authorize({ scope, success() { setAuthorizeStatus('authorized'); }, fail() { setAuthorizeStatus('denied'); }, }); } else { // 已经授权 setAuthorizeStatus('authorized'); } }, }); }; // 封装一个调用授权的函数 const authorize = () => { handleAuthorize(); }; return { authorize, authorizeStatus, }; }; // 使用封装的函数 const Component = () => { const { authorize, authorizeStatus } = useWeChatAuthorize(); return ( <View> {authorizeStatus === 'unknown' && <Button onClick={authorize}>授权位置信息</Button>} {authorizeStatus === 'authorized' && <Text>已授权</Text>} {authorizeStatus === 'denied' && <Text>用户拒绝授权</Text>} </View> ); }; ``` 在这个示例中,`useWeChatAuthorize`是一个自定义的Hook,它封装了授权的逻辑,并返回了一个`authorize`函数用于请求授权,以及一个`authorizeStatus`状态用于表示授权结果。组件可以使用这个Hook来获取授权状态,并根据状态作出相应的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值