~~~
<?php
~~~
/**
* H5获取授权链接
* @return void
*/
public function getAuthUrl()
{
$url = $this->request->param('url');
$platform = $this->request->param('platform','wechat');
if (!$url || !$platform || !isset($this->config[$platform])) {
$this->error('参数错误');
}
$this->config[$platform]['callback'] = $url;
$this->app = new Application($this->config); //
if (!$this->app->{$platform}) {
$this->error(__('Invalid parameters'));
}
$this->success('', $this->app->{$platform}->getAuthorizeUrl());
}
~~~
/**
* 微信
*/
class Wechat
{
const GET_AUTH_CODE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize";
/**
* 获取authorize_url
*/
public function getAuthorizeUrl()
{
$state = md5(uniqid(rand(), true));
Session::set('state', $state);
$queryarr = array(
"appid" => $this->config['app_id'],
"redirect_uri" => $this->config['callback'],
"response_type" => "code",
"scope" => $this->config['scope'],
"state" => $state,
);
request()->isMobile() && $queryarr['display'] = 'mobile';
$url = self::GET_AUTH_CODE_URL . '?' . http_build_query($queryarr) . '#wechat_redirect';
return $url;
}
}
~~~