Skip to content

Instantly share code, notes, and snippets.

@yyang
Created September 22, 2016 07:36
Show Gist options
  • Select an option

  • Save yyang/8cb39f6058820eae3f863b274c7c9029 to your computer and use it in GitHub Desktop.

Select an option

Save yyang/8cb39f6058820eae3f863b274c7c9029 to your computer and use it in GitHub Desktop.

Revisions

  1. yyang created this gist Sep 22, 2016.
    90 changes: 90 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    // when your webapp is loaded, before adding listener for weixing js bridge, check if it's already initialized:
    var timeoutID = 0;

    if (typeof WeixinJSBridge == "undefined") {
    alert('Already in WeChat!');
    } else {
    // setup a time out of let's say 5 seconds, to wait for the bridge:
    timeoutID = window.setTimeout(WeChatBridgeTimeout, 5000);
    // now add listener for the bridge:
    document.addEventListener('WeixinJSBridgeReady', WeChatBridgeReady, false);
    }

    // Now in bridge time out:
    function WeChatBridgeTimeout() {
    // Just to be sure that bridge was not initialized just before the line we added the listener (since it's a separate process than JS), let's check for it again:
    if (typeof WeixinJSBridge !== "undefined") {
    alert('Yes, we are finally in WeChat!');
    } else {
    alert('Seems we are out of WeChat');
    }
    }

    // And in event handled:
    function WeChatBridgeReady() {
    // remove listener timeout
    window.clearTimeout(timeoutID);
    alert('Yes, we jumped into WeChat!');
    // WeChat JS bridge initialized.
    }

    // Deal with Redirect

    /**
    * GET params by its name
    * @param {String} name Name of the GET param
    * @param {String} url URL to parse, otherwise use current url
    * @return {String} Value of the GET param
    */
    function getParameterByName(name, url) {
    url = url || window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    let regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)");
    let results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
    }

    // OAuth

    if (getParameterByName('state')) {
    alert('Code: ' + getParameterByName('code') + ' State: ' + getParameterByName('state'));
    } else {
    location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx608050d8426ae8ce&redirect_uri=http%3A%2F%2Fjoyseeedu.cn%2Fwechat-auth-test%2F&response_type=code&scope=snsapi_base&state=joyseelogin#wechat_redirect';
    }

    // Get Access Token

    // let code = getParameterByName('code');
    // let url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx608050d8426ae8ce&secret=<SECRET>&code=' + code + '&grant_type=authorization_code';

    // let request = new XMLHttpRequest();
    // request.open('GET', url, false); // `false` makes the request synchronous
    // request.send(null);

    // if (request.status === 200) {
    // alert(request.responseText);
    // }
    //

    let payargs = { appId: 'wx608050d8426ae8ce',
    timeStamp: '1474526592',
    nonceStr: '6nPDGFyXNMKKhufIpOqzu27L17ucp3zc',
    signType: 'MD5',
    package: 'prepay_id=wx201609221443127be9df85840672782175',
    paySign: 'F80FBEC0EB496219F3E13AC26443C5AF',
    timestamp: '1474526592' };

    document.addEventListener('WeixinJSBridgeReady', () => {
    WeixinJSBridge.invoke('getBrandWCPayRequest', payargs, function(res) {
    if (res.err_msg === "get_brand_wcpay_request:ok") {
    alert("支付成功");
    // 这里可以跳转到订单完成页面向用户展示
    }else{
    alert("支付失败,请重试");
    }
    });

    }, false);