zoukankan      html  css  js  c++  java
  • 微信公众支付(H5)工具类

    import request from '@/utils/request'
    
    // 微信支付(未调用后台接口)
    export function wxPay(url, param, methodType, successCallBack, failCallBack) {
        if (!isWechat()) return
        const promise = initiateRequest(url, param, methodType)
        if (promise) {
            promise.then(res => {
                if (res) {
                    initAndInvokeWxJSBridge(res, successCallBack, failCallBack)
                }
            })
        }
    }
    
    // 微信支付(已调用后台接口,paySign为后台接口返回数据)
    export function wxPayByPaySign(paySign, successCallBack, failCallBack) {
        if (!isWechat()) return
        if (paySign) {
            initAndInvokeWxJSBridge(paySign, successCallBack, failCallBack)
        }
    }
    
    function invokeWxPay(resData, successCallBack, failCallBack) {
        WeixinJSBridge.invoke('getBrandWCPayRequest', resData,
            res => {
                const msg = res.err_msg ? res.err_msg : res.errMsg;
                switch (msg) {
                    case 'get_brand_wcpay_request:ok':
                        if(successCallBack) {
                            successCallBack(res)
                        }
                        break
                    default:
                        WeixinJSBridge.log('支付失败!'+msg+',请返回重试.');
                        if(failCallBack){
                            failCallBack(res)
                        }
                        break
                }
            })
    }
    
    function initAndInvokeWxJSBridge(resData, successCallBack, failCallBack) {
    
        if (typeof WeixinJSBridge === 'undefined') {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', invokeWxPay(resData, successCallBack, failCallBack), false)
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', invokeWxPay(resData, successCallBack, failCallBack))
                document.attachEvent('onWeixinJSBridgeReady', invokeWxPay(resData, successCallBack, failCallBack))
            }
        } else {
            invokeWxPay(resData, successCallBack, failCallBack)
        }
    }
    
    function isWechat() {
        const ua = window.navigator.userAgent.toLowerCase();
        return ua.match(/micromessenger/i) == 'micromessenger'
    }
    
    function initiateRequest(url, param, methodType) {
        if (methodType && url) {
            const t = methodType.toLowerCase()
            switch (t) {
                case 'post':
                    return request.post({
                        url: url,
                        params: param
                    })
                case 'get':
                    return request.get({
                        url: url,
                        params: param
                    })
                case 'put':
                    return request.put({
                        url: url,
                        params: param,
                        header: {
                            "content-type": "application/x-www-form-urlencoded"
                        }
                    })
                default :
                    return request.post({
                        url: url,
                        params: param
                    })
            }
        }
        return null
    }
    

      

  • 相关阅读:
    Eclipse / android studio 添加第三方jar包 步骤
    Android checkbox 自定义点击效果
    Android 程序打包和安装过程
    Android 基础
    (转)Genymotion安装virtual device的“unable to create virtual device, Server returned Http status code 0”的解决方法
    (转)eclipse 导入Android 项目 步骤
    微信开放平台注册 步骤
    Android Studio 初级安装
    数组
    作用域问题代码
  • 原文地址:https://www.cnblogs.com/rain-in-summer/p/14372305.html
Copyright © 2011-2022 走看看