WeChat Sweep No Refreshログイン

8592 ワード

フロントエンド(index.html):




  
  
  
          



  
  

AppID:

( http/https ):

Fork me on GitHub function GetQueryString(name) { var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if(r!=null)return unescape(r[2]); return ''; } $(function () { var api_url = "http://www.xxx.com/weixin.php"; var getCode = function (uuid, last) { $.ajax({ type: "GET", url: api_url + "&uuid=" + uuid + (last ? '&last=' + last : ''), dataType: "json", cache: !1, timeout: 6e4, xhrFields:{ withCredentials:true }, success: function (data) { console.log(data) data = data.content if (data.status === 405) { $('#code').text(' ,code = ' + data.result.code) } else if (data.status === 404) { $('#code').text(data.msg.title + ', ' + data.msg.content) getCode(uuid, data.result.wxErrCode) } else if (data.status === 403) { $('#code').text(data.msg.title + ', ' + data.msg.content) getCode(uuid, data.result.wxErrCode) } else if (data.status === 500) { getUUID() } else { setTimeout(function () { getCode(uuid) }, 2000) } }, error: function () { setTimeout(function () { getCode(uuid) }, 2000) } }) } var getUUID = function (uuid) { $.ajax({ type: "GET", url: api_url, dataType: "json", cache: !1, timeout: 6e4, xhrFields:{ withCredentials:true }, success: function (data) { data = data.content if (data.status === 1) { var uuid = data.result.wxUUID $('#qrcode').attr('src', data.result.imgData) getCode(uuid) } else { // setTimeout(function () { // window.location.reload(); // }, 2000) } }, error: function () { // setTimeout(function () { // window.location.reload(); // }, 2000) } }) } $("#appid").val(GetQueryString("appid")) $("#redirectUri").val(GetQueryString("redirect_uri")) $("#test").click(function () { window.location.href = window.location.origin + "/index.html?appid=" + $("#appid").val() + "&redirect_uri=" + $("#redirectUri").val(); }) getUUID() });

 

后端(weixin.php):

 405,
                'msg' => '    ',
                'result' => array(
                    'code' => $wxCode,
                ),
            );
            break;
        case 404:
            $r = array(
                'status' => 404,
                'msg' => array(
                    'title' => '    ',
                    'content' => '             ',
                ),
                'result' => array(
                    'wxErrCode' => $wxErrCode,
                ),
            );
            break;
        case 403:
            $r = array(
                'status' => 403,
                'msg' => array(
                    'title' => '        ',
                    'content' => '        ,     ',
                ),
                'result' => array(
                    'wxErrCode' => $wxErrCode,
                ),
            );
            break;
        case 402:
        case 500:
            $r = array(
                'status' => 500,
                'msg' => '      uuid',
            );
            break;
        case 408:
            $r = array(
                'status' => 408,
                'msg' => '      ',
            );
            break;
    }
    echo json_encode($r);
} else {
    $ch = curl_init();
    $appid = empty($_GET['appid']) ? 'wx827225356b689e24' : $_GET['appid'];
    $redirect_uri = empty($_GET['redirect_uri']) ? 'https://qq.jd.com/' : $_GET['redirect_uri'];
    $redirect_uri = urlencode(iconv('gb2312', 'UTF-8', $redirect_uri));
    curl_setopt($ch, CURLOPT_URL, 'https://open.weixin.qq.com/connect/qrconnect?appid=' . $appid . '&scope=snsapi_login&redirect_uri=' . $redirect_uri . '&state=&login_type=jssdk&self_redirect=default');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Cache-Control: no-cache', 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3522.0 Safari/537.36'));
    $output = curl_exec($ch);
    curl_close($ch);
    // var_dump($output);die;
    $preg = '/src="\/connect\/qrcode\/(.*?)" \/>/i';
    preg_match_all($preg, $output, $res);
    $uuid = $res[1][0];
    if (isset($_GET['img'])) { //     -      img  ,          ,        Chrome70            
        $r = array('status' => 1, 'result' => array('wxUUID' => $uuid, 'imgURL' => 'https://open.weixin.qq.com/connect/qrcode/' . $uuid));
    } else {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://open.weixin.qq.com/connect/qrcode/' . $uuid);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Cache-Control: no-cache', 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3522.0 Safari/537.36'));
        $output = curl_exec($ch);
        curl_close($ch);
        $r = array('status' => 1, 'result' => array('wxUUID' => $uuid, 'imgData' => 'data:image/jpeg;base64,' . base64_encode($output)));
    }
    echo json_encode($r);
}