html内核心代码
<div class="baosub">
<img src="{$public}images/down_11.png" class="zpic" alt="">
{empty name=":session('user_id')"}
<div class="subtxt" id="showmask">
立即下载
</div>
{else /}
<a href="{$info.download}" class="subtxt">
立即下载
</a>
{/empty}
</div>
//隐藏的弹窗
<div class="popmask">
<div class="tanbox">
<div class="tanbai">
<div class="tantitle">
微信扫一扫登录
</div>
<img src="{$public}images/ma_03.png" id="weixinerweima" class="ermapic" alt="">
<div class="ertxt">
微信扫码登录/注册
</div>
<div class="erditxt">
首次扫码关注公众号点击收到的链接完成登录
</div>
<img src="{$public}images/close.png" class="close" alt="">
</div>
</div>
</div>
js代码
<script>
$('#showmask').click(function(){
$.ajax({
type:'post',
url:"{:url('index/Wechat/createMpLoginQrCode')}",
dataType:"json",
success:function(data){
if(data.code == 1){
//修改二维码的图
var img=document.getElementById("weixinerweima");
img.src=data.data.url;
$(".popmask").show()
//启动定时任务请求后台接口
var cishu = 0;
var autoInterval;
autoInterval = mySetInterval();
//定义定时器
function mySetInterval(){
return setInterval(function (){
cishu = cishu+1;
$.ajax({
type:'post',
url:"{:url('index/Wechat/getLoginResult')}",
data:{"id":data.data.id},
dataType:"json",
success:function(login){
if(login.code == 1){
clearInterval(autoInterval);
layer.msg('登录成功',{icon:6,time:2000}, function(){
location.reload();
});
} else {
if(cishu>30){
clearInterval(autoInterval);
layer.open({title:"登录失败",content:'扫码超时',icon:5,anim:6});
}
}}
});
},1*1000); //秒
}
} else {
layer.open({title:"{:lang('error')}",content:data.msg,icon:5,anim:6});
}}
});
})
$('.close').click(function(){
$(".popmask").hide()
})
// wow效果启用
new WOW().init();
</script>
thinkPHP后台代码
<?php
namespace app\index\controller;
use app\Request;
use EasyWeChat\Factory;
use think\exception\HttpResponseException;
use think\facade\Cache;
use think\facade\Config;
use think\facade\View;
use think\Response;
use think\facade\Log;
use think\facade\Route;
class Weixin
{
// 这里是你设定的微信扫码通知URL
public function index()
{
//你的公众号陪诊参考EasyWeChat
$config = Config::get('app.weixingongzhonghao');
$config['response_type'] = 'array';
$app = Factory::officialAccount($config);
$app->server->push(function ($message) {
switch ($message['MsgType']) {
case 'event':
$url = Route::buildUrl('userLogin', ['id'=>$message['EventKey']])
->suffix('html')
->domain(true);
return "<a href=\"".$url."\">点击登录</a>";
break;
default:
return '欢迎关注';
break;
}
});
$response = $app->server->serve();
$empty = strpos($response, '<Content></Content>');
$success = strpos($response, 'success');
if (!$empty && !$success) {
return $response;
} else {
echo '';
}
exit;
}
public function event($data)
{
$event = $data['Event'];
$open_id = $data['FromUserName'];
switch ($event) {
case 'subscribe':
$id = explode('_', $data['EventKey'])[1];
self::userLogin($open_id, $id);
break;
case 'unsubscribe':
break;
case 'SCAN':
self::userLogin($open_id, $data['EventKey']);
break;
}
}
// 这里的$id 是咱们在第4步生成的id(用来请求登录状态那个),它通过微信的二维码带过来了,是贯穿全局的唯一标识
public function userLogin($id)
{
//未关注的用户微信通知服务器的时候EventKey前面多一个qrscene_
$id = str_replace('qrscene_','',$id);
Cache::set($id, ['status' => 1]);
return View::fetch();
}
//验证消息
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
//检查签名
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = Config::get('app.weixingongzhonghao.token');
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if($tmpStr == $signature){
return true;
}else{
return false;
}
}
/**
* 操作错误跳转
* @param mixed $msg 提示信息
* @param string $url 跳转的URL地址
* @param mixed $data 返回的数据
* @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @return void
*/
protected function error($msg = '', string $url = null, $data = '', int $wait = 3, array $header = []): Response
{
if (is_null($url)) {
$url = request()->isAjax() ? '' : 'javascript:history.back(-1);';
} elseif ($url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url);
}
$result = [
'code' => 0,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
$type = (request()->isJson() || request()->isAjax()) ? 'json' : 'html';
if ($type == 'html') {
$response = view(app('config')->get('app.dispatch_error_tmpl'), $result);
} else if ($type == 'json') {
$response = json($result);
}
throw new HttpResponseException($response);
}
/**
* 返回封装后的API数据到客户端
* @param mixed $data 要返回的数据
* @param integer $code 返回的code
* @param mixed $msg 提示信息
* @param string $type 返回数据格式
* @param array $header 发送的Header信息
* @return Response
*/
protected function result($data, int $code = 0, $msg = '', string $type = '', array $header = []): Response
{
$result = [
'code' => $code,
'msg' => $msg,
'time' => time(),
'data' => $data,
];
$type = $type ?: 'json';
$response = Response::create($result, $type)->header($header);
throw new HttpResponseException($response);
}
/**
* 操作成功跳转
* @param mixed $msg 提示信息
* @param string $url 跳转的URL地址
* @param mixed $data 返回的数据
* @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @return void
*/
protected function success($msg = '', string $url = null, $data = '', int $wait = 3, array $header = []): Response
{
if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
$url = $_SERVER["HTTP_REFERER"];
} elseif ($url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url);
}
$result = [
'code' => 1,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
$type = (request()->isJson() || request()->isAjax()) ? 'json' : 'html';
if ($type == 'html') {
$response = view(app('config')->get('app.dispatch_success_tmpl'), $result);
} else if ($type == 'json') {
$response = json($result);
}
throw new HttpResponseException($response);
}
}
<?php
namespace app\index\controller;
use think\exception\HttpResponseException;
use think\facade\Cache;
use EasyWeChat\Factory;
use think\facade\Config;
use think\facade\Request;
use think\Response;
use think\facade\Session;
class Wechat
{
// 创建登录二维码
public function createMpLoginQrCode()
{
$ip = Request::ip();
$id = self::createID($ip);
$config = Config::get('app.weixingongzhonghao');
$app = Factory::officialAccount($config);
$result = $app->qrcode->temporary($id, 120); // 生成临时二维码 有效时间2分钟,这里是秒数,可以自己修改,临时最高为1个月
if (!isset($result['ticket'])) $this->error('二维码请求失败,网络繁忙');
$url = $app->qrcode->url($result['ticket']);
$this->success('获取成功','',['url' => $url, 'id' => $id,'result'=>$result]);
}
private function createID($ip)
{
$id = md5(time() . $ip . rand(0, 999));
if (Cache::has($id)) {
return self::createID($ip);
}
return $id;
}
// 查询登录结果
public function getLoginResult()
{
$id = input('id') ?? $this->error('参数错误:id');
if (Cache::has($id)) {
Session::set('user_id', $id);
$this->success('登录成功');
}
$this->error('请在手机上完成后续操作');
}
/**
* 操作错误跳转
* @param mixed $msg 提示信息
* @param string $url 跳转的URL地址
* @param mixed $data 返回的数据
* @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @return void
*/
protected function error($msg = '', string $url = null, $data = '', int $wait = 3, array $header = []): Response
{
if (is_null($url)) {
$url = request()->isAjax() ? '' : 'javascript:history.back(-1);';
} elseif ($url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url);
}
$result = [
'code' => 0,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
$type = (request()->isJson() || request()->isAjax()) ? 'json' : 'html';
if ($type == 'html') {
$response = view(app('config')->get('app.dispatch_error_tmpl'), $result);
} else if ($type == 'json') {
$response = json($result);
}
throw new HttpResponseException($response);
}
/**
* 返回封装后的API数据到客户端
* @param mixed $data 要返回的数据
* @param integer $code 返回的code
* @param mixed $msg 提示信息
* @param string $type 返回数据格式
* @param array $header 发送的Header信息
* @return Response
*/
protected function result($data, int $code = 0, $msg = '', string $type = '', array $header = []): Response
{
$result = [
'code' => $code,
'msg' => $msg,
'time' => time(),
'data' => $data,
];
$type = $type ?: 'json';
$response = Response::create($result, $type)->header($header);
throw new HttpResponseException($response);
}
/**
* 操作成功跳转
* @param mixed $msg 提示信息
* @param string $url 跳转的URL地址
* @param mixed $data 返回的数据
* @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @return void
*/
protected function success($msg = '', string $url = null, $data = '', int $wait = 3, array $header = []): Response
{
if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
$url = $_SERVER["HTTP_REFERER"];
} elseif ($url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url);
}
$result = [
'code' => 1,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
$type = (request()->isJson() || request()->isAjax()) ? 'json' : 'html';
if ($type == 'html') {
$response = view(app('config')->get('app.dispatch_success_tmpl'), $result);
} else if ($type == 'json') {
$response = json($result);
}
throw new HttpResponseException($response);
}
}