12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- namespace app\expand\controller;
- use app\common\service\HelperService;
- use app\common\service\wechat\JsApi_pub;
- use think\Config;
- /**
- * 微信授权接口
- * Class WeChat
- * @package app\expand\controller
- */
- class WechatAuthorization extends BaseAuth
- {
- private $_Account = null;
- private $_APPID = null;
- private $_APPSECRET = null;
- public function __construct(){
- parent::__construct();
- $this->_Account = $this->getKey($this->_apiCode);
- //验证是否具有访问这个接口的权限
- if(!isset($this->_Account['Wechat_authorization_appId'])
- || !isset($this->_Account['Wechat_authorization_appsecret'])){
- HelperService::returnJson(['code'=>400,'msg'=>'WECHAT authorization interface unauthorized access','data'=>[]]);
- }
- $this->_APPID = $this->_Account['Wechat_authorization_appId'];
- $this->_APPSECRET = $this->_Account['Wechat_authorization_appsecret'];
- Config::set('WECHAT_APPID',$this->_APPID);
- Config::set('WECHAT_APPSECRET',$this->_APPSECRET);
- }
- /**
- * 授权第一步
- */
- public function authorize(){
- $param = $this->_sysParams;
- if(!isset($param['notify_url'])){
- Header("HTTP/1.1 500 未设置同步跳转地址");
- }
- session('wx_authorize_notify',$param['notify_url']);
- require_once(APP_PATH.'/common/service/wechat/WxPayPubHelper.php');
- $JsApi = new JsApi_pub();
- //触发微信返回code码
- $url = $JsApi->createOauthOpenidAndMoreUrlForCode("http://{$_SERVER['HTTP_HOST']}/expand/WechatAuthorization/authorizeTwo", rand(100,1000));
- header("HTTP/1.1 301 Moved Permanently");
- header("Location: $url");
- exit;
- }
- /**
- * 授权(二、三、四)3步
- */
- public function authorizeTwo(){
- if(!session('wx_authorize_notify')){
- Header("HTTP/1.1 500 同步跳转地址丢失");
- }
- $params = $this->_sysParams;
- //第二步得到openid
- $code = $params['code'];
- $JsApi = new JsApi_pub();
- $JsApi->setCode($code);
- $openInfo = $JsApi->getOpenidInfo();
- //第三步授权
- $access_token = isset($openInfo['access_token'])?$openInfo['access_token']:'';
- $openid = isset($openInfo['openid'])?$openInfo['openid']:'';
- if(empty($access_token) || empty($openid)){
- Header("HTTP/1.1 500 获取OPENID失败,请检查配置!");
- }
- //第四步拉取个人信息
- $result = $JsApi->getOauthUserInfo($access_token,$openid);
- $url = base64_decode(session('wx_authorize_notify'));
- $params = @http_build_query($result);
- if(empty($result)){
- header("HTTP/1.1 301 Moved Permanently");
- header("Location: $url");
- }
- header("Location: $url?$params");
- exit;
- }
- }
|