107 lines
2.5 KiB
PHP
107 lines
2.5 KiB
PHP
<?php
|
||
namespace app\api\wxapi\request;
|
||
|
||
use app\api\wxapi\Base;
|
||
|
||
/* $req = new CodeRequest();
|
||
* $req->setAppId($WxClient->appID);
|
||
* $req->setServerUrl($WxClient->serverUrl);
|
||
* $req->setScope(self::STATE_SNSAPI_BASE);
|
||
* $url=$req->run();
|
||
* return $this->redirect($url);
|
||
* */
|
||
class CodeRequest extends Base
|
||
{
|
||
const STATE_SNSAPI_BASE='snsapi_base'; //不弹出授权页面,直接跳转,只能获取用户openid
|
||
const STATE_SNSAPI_USERINFO='snsapi_userinfo'; //弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息
|
||
private $serverUrl='https://open.weixin.qq.com/';
|
||
private $redirect_uri;
|
||
private $apiParas = array();
|
||
private $appid;
|
||
private $scope;
|
||
private $state;
|
||
|
||
public function init(){
|
||
$callback=$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
|
||
$this->setAppId('');
|
||
$this->setRredirectUri($callback);
|
||
$this->apiParas['response_type'] = 'code';
|
||
$this->setScope(self::STATE_SNSAPI_BASE);
|
||
$this->apiParas['state'] = time();
|
||
$this->apiParas['#'] = 'wechat_redirect';
|
||
}
|
||
|
||
public function run()
|
||
{
|
||
//获取业务参数
|
||
$apiParams = $this->getApiParas();
|
||
|
||
//系统参数放入GET请求串
|
||
$requestUrl = $this->serverUrl . $this->getApiMethodName() . "?";
|
||
foreach ($apiParams as $key => $value)
|
||
{
|
||
$requestUrl .= "$key=" . $value . "&";
|
||
}
|
||
return trim($requestUrl,"&");
|
||
}
|
||
|
||
public function getApiMethodName(){
|
||
return "connect/oauth2/authorize";
|
||
}
|
||
|
||
public function getApiParas(){
|
||
return $this->apiParas;
|
||
}
|
||
|
||
public function putOtherTextParam($key, $value){
|
||
$this->apiParas[$key] = $value;
|
||
$this->$key = $value;
|
||
}
|
||
|
||
public function setAppId($appid){
|
||
$this->appid = $appid;
|
||
$this->apiParas["appid"] = $appid;
|
||
}
|
||
|
||
public function getAppId(){
|
||
return $this->appid;
|
||
}
|
||
|
||
public function setRredirectUri($redirect_uri){
|
||
$redirect_uri = urlencode($redirect_uri);
|
||
$this->redirect_uri = $redirect_uri;
|
||
$this->apiParas["redirect_uri"] = $redirect_uri;
|
||
}
|
||
|
||
public function getRredirectUri(){
|
||
return $this->redirect_uri;
|
||
}
|
||
|
||
public function setScope($scope){
|
||
$this->scope = $scope;
|
||
$this->apiParas["scope"] = $scope;
|
||
}
|
||
|
||
public function getScope(){
|
||
return $this->scope;
|
||
}
|
||
|
||
public function setServerUrl($serverUrl){
|
||
$this->serverUrl = $serverUrl;
|
||
}
|
||
|
||
public function getServerUrl(){
|
||
return $this->serverUrl;
|
||
}
|
||
|
||
public function setState($state){
|
||
$this->state = $state;
|
||
$this->apiParas["state"] = $state;
|
||
}
|
||
|
||
public function getState(){
|
||
return $this->state;
|
||
}
|
||
|
||
}
|