1. 程式人生 > >呼叫微信掃碼實現掃一掃簽到

呼叫微信掃碼實現掃一掃簽到

1、首先生成二維碼

include('phpqrcode.php');
$url_code = "";//掃碼之後的業務邏輯層
$level = 0; //容錯級別
$size
= 9; //生成圖片的大小
$margin
= 4; //二維碼周圍邊框空白區域間距值
$saveandprint = true; //是否儲存二維碼並顯示
$qrpath
= ''; //二維碼圖片存放路徑
$filename
= '隨機字串' .'.png';//生成的二維碼圖片名
if (is_dir($qrpath) == false) {
mkdir(
$qrpath, 0777, true);
}

QRcode::
png($url_code
, $qrpath . $filename, $level, $size, $margin, $saveandprint); 2、定義一個點選掃一掃的入口頁面
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>掃碼</title>
</head>
<body>
    <a href="saoma.html">掃一掃</a>
</body>
</html>

3、點選掃一掃之後跳到呼叫掃一掃介面的html頁面(重要)
<?php
require_once "jssdk.php";
$jssdk = new JSSDK($appid,$appsecret);$signPackage = $jssdk->GetSignPackage();
?><!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <script src="/static/default/js/jquery1.8.3.min.js"
></script> <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> <script type="text/javascript"> var openid = '<?php echo $openid;?>';//事先獲取到的openid wx.config({ debug: false, appId: '<?php echo $signPackage["appId"]; ?>', timestamp: <?php echo $signPackage["timestamp"]; ?>, nonceStr: '<?php echo $signPackage["nonceStr"]; ?>', signature: '<?php echo $signPackage["signature"]; ?>', jsApiList: [ 'scanQRCode' ] }); wx.ready(function () { wx.scanQRCode({ needResult: 1, desc: 'scanQRCode desc', success: function (res) { //alert(JSON.stringify(res));//走到這一步你可以看看彈出的是什麼東西 if(res.errMsg === "scanQRCode:ok"){ var url = res.resultStr+'&openid='+openid; location.href=url;} } }); }); wx.error(function (res) { alert(res.errMsg); }); </script> </html> 謝謝