html5 微信扫一扫功能实现

 html 

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title></title>
</head>
<body>
<input type="text" value="{$signPackage.appId}" id="id1" hidden="hidden" />
<input type="text" value="{$signPackage.timestamp}"id="id2" hidden="hidden"  />
<input type="text" value="{$signPackage.nonceStr}" id="id3" hidden="hidden"  />
<input type="text" value="{$signPackage.signature}" id="id4" hidden="hidden"  />
<button id="button" class="btn btn-success" type="button" onclick="scanCode()" hidden="hidden"/>扫一扫!</button>
</body>
<script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script src="__STATIC__/js/jquery.min.js"></script>
<script>
        // 注意:所有的JS接口只能在公众号绑定的域名下调用,公众号开发者需要先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。
        // 如果发现在 Android 不能分享自定义内容,请到官网下载最新的包覆盖安装,Android 自定义分享接口需升级至 6.0.2.58 版本及以上。
        // 完整 JS-SDK 文档地址:<a rel="nofollow" href="http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html" target="_blank">http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html</a>
        var appId=$('#id1').val();
        var timestamp=$('#id2').val();
        var nonceStr=$('#id3').val();
        var signature=$('#id4').val();
        function wxConfig() {
            wx.config({
                debug : true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
                appId : appId, // 必填,公众号的唯一标识
                timestamp : timestamp, // 必填,生成签名的时间戳
                nonceStr : nonceStr, // 必填,生成签名的随机串
                signature : signature,// 必填,签名,见附录1
                jsApiList : [  "scanQRCode" ]
                // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
            });
        }


        function scanCode() {
            wx.scanQRCode({
                needResult : 1,
                scanType : [ "qrCode", "barCode" ],
                success : function(res) {
                    console.log(res)
                    alert(JSON.stringify(res));
                    var result = res.resultStr;
                },
                fail : function(res) {
                    console.log(res)
                    //    alert(JSON.stringify(res));
alert("没有权限,请拍照上传!请尽量靠近二维码拍照!");
$("#button").hide();
                    $("#div1").show();
                    $("#div2").show();
                }
            });
        }     
</script>

</html>

 

php获取签名   

public function sys(){
    $appid = '';
    $secret = '';
    //这里获取access_token
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
    $result = $this->https_request($url);
    $json1 = json_decode($result,true);
 
    //通过access_token获取ticket
    $us = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?    access_token=".$json1['access_token']."&type=jsapi";
    $resu = $this->https_request($us);
    $json = json_decode($resu,true);

    //生成签名的时间戳
    $timestamp = time(); 

    //生成16位随机字符串
    $nonceStr = $this->createNonceStr();

    //获取当前url
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

    $string = "jsapi_ticket=".$json['ticket']."&noncestr=".$nonceStr."&timestamp=".$timestamp."&url=".$url."";

    //最重要的我也不知道叫什么
    $signature = sha1($string);

    $signPackage = array(
        "appId"=>$appid,
        "nonceStr"=>$nonceStr,
        "timestamp"=>$timestamp,
        "url"=>$url,
        "signature"=>$signature,
        "rawString"=>$string
    );

    return $signPackage;
}

//zifuchuan
public function createNonceStr($length = 16) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $str = "";
    for ($i = 0; $i < $length; $i++) {
      $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
}

//回调
public function https_request($url,$data=null){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);

    return $output;
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要PC网站上实现微信码登录功能,可以按照以下步骤进行: 1. 注册微信开放平台账号:访问微信开放平台(https://open.weixin.qq.com/),使用微信账号登录并注册一个开放平台账号。 2. 创建应用并获取应用ID和密钥:在微信开放平台上创建一个应用,并获取对应的应用ID(AppID)和密钥(AppSecret)。 3. 引入微信登录SDK:下载并引入微信登录的Java SDK,可以使用官方提供的SDK或第三方库,例如weixin-java-tools。 4. 配置回调URL:在微信开放平台上配置回调URL,该URL用于接收微信授权回调的code。 5. 实现码登录页面:在PC网站上创建一个码登录页面,可以使用HTML和CSS进行布局和样式设计。 6. 发起微信授权请求:在码登录页面中,通过调用微信登录SDK提供的接口,生成带有应用ID和回调URL的二维码图片,并显示在页面上供用户码。 7. 处理微信授权回调:在设置的回调URL对应的接口中,获取微信授权回调的code,并使用该code调用微信登录SDK提供的接口,获取access_token和openid等用户信息。 8. 实现登录逻辑:根据获取到的用户信息,可以选择将用户信息保存到数据库中,或者进行其他逻辑处理。 需要注意的是,微信授权登录涉及到用户隐私和安全,建议在实施过程中加强安全验证和保护用户信息。 以上是一个大致的步骤,具体实现过程可以参考微信开放平台的文档和示例代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值