/**
* 登录二维码
*/
public function qrcode()
{
//生成数据
$dataInfo = [
'ticket' => '',
'is_login' => 0,
'user_id' => 0,
'createtime' => time(),
'updatetime' => time()
];
$xh_ticket_id = $this->addOne('xh_ticket', $dataInfo);
if ($xh_ticket_id !== false) {
try {
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" . $this->getAccessToken();
$datawx = [
"expire_seconds" => 604800,
"action_name" => "QR_SCENE",
"action_info" => [
"scene" => [
"scene_id" => $xh_ticket_id
]
]
];
$result = $this->https_post($url, json_encode($datawx));
$jsonwx = json_decode($result, true);
if ($jsonwx) {
//更新
$ticket = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" . urlencode($jsonwx['ticket']);
$this->saveData('xh_ticket', ['id' => $xh_ticket_id], ['ticket' => $jsonwx['ticket'], 'updatetime' => time()]);
$res = ["scene_id" => $xh_ticket_id, "ticket" => $ticket];
exit(json_encode(["data" => $res, "code" => 1]));
} else {
$this->result([], 0, '登录获取二维码失败1');
}
} catch (\Exception $e) {
$this->result([], 0, '登录获取二维码失败2');
}
$this->result([], 0, '登录获取二维码失败3');
} else {
$this->result([], 0, '登录获取二维码失败4');
}
}
/**
* POST方法
*/
public function https_post($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;
}
/**
* 登录轮询
*/
public function login()
{
if ($this->request->isPost()) {
$param = $this->request->param();
if (empty($param['scene_id'])) {
$this->result([], 0, 'scene_id错误');
}
$xh_ticket = $this->getOne('xh_ticket', ['id' => $param['scene_id'], 'is_login' => 1, 'deletetime' => null]);
if ($xh_ticket) {
//登录成功
session('user_id', $xh_ticket['user_id']);
$this->result([], 2, '登录成功');
} else {
$this->result([], 1, '登录中');
}
}
}
/**
* 公众号
* 获取AccessToken
*/
public function getAccessToken()
{
$data = json_decode(file_get_contents("access_token.json"));
if (!$data || $data->expire_time < time()) {
$appid = $this->appid;
$secret = $this->appsecret;
$url = str_replace("##APPID##", $appid, "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=##APPID##&secret=##APPSECRET##");
$url = str_replace("##APPSECRET##", $secret, $url);
$html = file_get_contents($url);
$output = json_decode($html, true);
$access_token = $output['access_token'];
if ($access_token) {
$data = new \stdClass();
$data->expire_time = time() + 7000;
$data->access_token = $access_token;
$fp = fopen("access_token.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
} else {
$access_token = $data->access_token;
}
return $access_token;
}
CREATE TABLE `n_xh_ticket` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`ticket` varchar(255) NOT NULL DEFAULT '' COMMENT '船票',
`is_login` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '登录状态:0未登录,1已登录',
`user_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '登录用户UID',
`createtime` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
`updatetime` int(10) NOT NULL DEFAULT '0' COMMENT '更新时间',
`deletetime` int(10) DEFAULT NULL COMMENT '删除时间',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=26 DEFAULT CHARSET=utf8mb4 COMMENT='扫码关注公众号登录';