//临时二维码
public function getQrls()
{
$user = $this->auth->getUserinfo();
$src = Cache::get('srcimg');
if (!empty($src)) {
$this->success('成功', ['img' => $src]);
}
$user_model = new \app\admin\model\User();
$userOpenid = $user_model->where('id',$user['id'])->value('openid');
$accessToken = Cache::get('access_token');
if (!$accessToken){
$accessToken = $this->_getWxAccessToken($userOpenid);
}
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$accessToken;
$postArr = [
"action_name" => "QR_SCENE",
"expire_seconds" => 604800,
"action_info" => [
'scene' => ['scene_id' => 2000],
],
];
$postJson = json_encode($postArr);
$res = $this->ch($url, 'post', 'json', $postJson);
$ticket = $res['ticket'];
$url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);
$src = "<img src='".$url."' alt='关注公众号' width='100%'>";
Cache::set('srcimg',$src,600000);
$this->success('成功', ['img' => $src]);
}
//永久二维码
public function getQryj()
{
$accessToken = $this->_getWxAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={$accessToken}";
$postArr = [
"action_name" => "QR_LIMIT_SCENE",
"action_info" => [
'scene' => ['scene_id' => 3000],
],
];
$postJson = json_encode($postArr);
$res = $this->ch($url, 'post', 'json', $postJson);
$ticket = $res['ticket'];
$url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);
echo "<img src='".$url."'>";
}
//url请求
private function ch($url, $type='get', $res='json', $arr='')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
if ($type == 'post') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
}
$cnt = curl_exec($ch);
if (curl_errno($ch)) {
return ;
}
curl_close($ch);
if ($res == 'json') {
return json_decode($cnt, true);
}
return $cnt;
}
private function _getWxAccessToken($appid){
$url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=". Config::get("wechat.AppId") ."&secret=". Config::get("wechat.AppSecret");
$data = $this->ch($url,'get');
Cache::set('access_token',$data['access_token'],7100);
return $data['access_token'];
}