~~~
<?php
namespace app\api\controller\yunyi;
use app\api\service\CutImgService;
use app\api\unity\RedisCli;
use app\api\unity\Uilt;
use think\App;
use think\admin\Controller;
class Login extends Controller
{
/**
* 获取acc_token
*/
public function getAccessToken(){
$appid = 'wx946c9******';
if(!RedisCli::bulidRedis()->get('gzhAccessToken'.$appid)){
$secret = '******';
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
$http = new Uilt();
$data = json_decode($http->get($url),true);
RedisCli::bulidRedis()->set('gzhAccessToken'.$appid,$data['access_token'],$data['expires_in']);
}
return RedisCli::bulidRedis()->get('gzhAccessToken'.$appid);
}
/**
* 获取二维码url
*/
public function getQrCode(){
$key = date('dhis') . rand(10,99);;
$access_token = $this->getAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
$postArr = array(
'expire_seconds' => 604800, //二维码过时时间 不填默认30s
'action_name' => "QR_SCENE", //场景
'action_info' => array(
'scene' =>array('scene_id' => $key,),
),
);
$postJson = json_encode($postArr);
$http = new Uilt();
$res = json_decode($http->post($url,$postJson),true);
if(empty($res['ticket']))
$this->error("错误!");
$ticket = $res['ticket'];
$url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);
if(empty($url))
$this->error("错误!");
$data = [
'token' => $this->encryption($key),
'url' => $url
];
$this->success('成功', $data, 200);
}
/**
* @return void接收微信事件推送
*/
public function getLoginInfo(){
$echoStr = input('echostr','');
if(!empty($echoStr)){
echo $echoStr;
exit;
}
RedisCli::bulidRedis()->set('test12848',123,30);
//接收微信服务器回调的数据流
if (!$xml = file_get_contents('php://input')) {
$this->error("错误!");
}
$data = $this->fromXml($xml);
RedisCli::bulidRedis()->set('test12311',$data,300);
if(!empty($data['EventKey'])){
RedisCli::bulidRedis()->set($this->encryption($data['EventKey']),$data,86400 * 7);
}
$toUser = $data['FromUserName'];
$fromUser = $data['ToUserName'];
$time = time();
$msgType = 'text';
$content = '欢迎关注我们的微信公众账号';
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
echo $info;
// echo 'success';
exit;
}
public function test1(){
$data1 = RedisCli::bulidRedis()->get('test12848');
$data = RedisCli::bulidRedis()->get('test12311');
print_r($data);
print_r($data1);
}
/**
* @return void查询登录状态
*/
public function getLoginStaus(){
$token = input('token');
if(!$token){
$this->error("未获取到token!",'',101);
}
$data = RedisCli::bulidRedis()->get($token);
if(RedisCli::bulidRedis()->get($token)){
$this->success('登录成功', $data, 200);
}
$this->error("未登录!",'',101);
}
/**
* 将xml转为array
*/
private function fromXml($xml)
{
// 禁止引用外部xml实体
libxml_disable_entity_loader(true);
return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
}
/**
* 生成加密
*/
private function encryption($key){
$token = ******;
return $token;
}
}
~~~