微信授权
作者:互联网
//唤起扫码页面
public function componentloginpageOp()
{
$param = model('parameter')->getval();
$this->grant = new grant($param->token, $param->encodingAesKey, $param->appid, $param->appsecret);
$getctoken = model('parameter')->getctoken();
if ($getctoken['code'] == '0') {
return callbackfun('error', $getctoken['msg']);
} else {
$component_access_token = $getctoken['component_access_token'];
}
return $this->redirect($this->grant->rqcode($component_access_token)); //重定向
}
//回调页面
public function callbackOp()
{
$param = model('parameter')->getval();
$this->grant = new grant($param->token, $param->encodingAesKey, $param->appid, $param->appsecret);
$auth_code = input('auth_code'); //微信授权码
$getctoken = model('parameter')->getctoken();
if ($getctoken['code'] == '0') {
return callbackfun('error', $getctoken['msg']);
} else {
$component_access_token = $getctoken['component_access_token'];
}
//4,使用授权码换取公众号或小程序的接口调用凭据和授权信息
$authInfo = $this->grant->apiQueryAuth($component_access_token, $auth_code);
if (!array_key_exists('authorization_info', $authInfo)) {
return callbackfun('error', '使用授权码换取公众号信息失败', url('togrant'));
}
$authInfo = $authInfo['authorization_info'];
//6 .获取授权方的帐号基本信息
$account_information = $this->grant->apiGetAuthorizerInfo($component_access_token, $authInfo);
$authorizer_info = $account_information['authorizer_info'];
$authorization_info = $account_information['authorization_info'];
$id = model('wx_authorization_information')->where(['authorizer_appid' => $authorization_info['authorizer_appid'], 'uid' => $this->userinfo['uid'], 'user_name' => $authorizer_info['user_name']])->value('id');
foreach ($authorization_info['func_info'] as $k => $v) {
$func_info[] = $v['funcscope_category']['id'];
}
$data = [
'authorizer_appid' => $authorization_info['authorizer_appid'],
'nick_name' => $authorizer_info['nick_name'],
'uid' => $this->userinfo['uid'],
'kefuid' => $this->userinfo['rulerid'],
'head_img' => $authorizer_info['head_img'],
'user_name' => $authorizer_info['user_name'],//原始id
'service_type_info' => $authorizer_info['service_type_info']['id'], //授权方公众号类型
'verify_type_info' => $authorizer_info['verify_type_info']['id'],//授权方认证类型
'business_info' => json_encode($authorizer_info['business_info']),
'func_info' => json_encode($func_info),
'datetime' => time(),//授权时间
];
if ($id == null) {
$type = false;
} else {
$type = true;
$data['id'] = $id;
}
$result = model('wx_authorization_information')->allowField(true)->isUpdate($type)->save($data);
$authInfo['to_user_name'] = $authorizer_info['user_name'];
//5.获取(刷新)授权公众号或小程序的接口调用凭据(令牌)
$tokenArr = $this->grant->apiAuthorizerToken($component_access_token, $authInfo, $this->userinfo['uid']);
//7.获取授权方的选项设置信息
$option_gettings_information = $this->grant->apiGetAuthorizerOption($component_access_token, $account_information);
// 8、设置授权方的选项信息
$option_settings_information = $this->grant->apiSetAuthorizerOption($component_access_token, $option_gettings_information);
return $this->redirect("/kefu/wechat/toGrant"); //重定向
//
//令牌未缓存。
}
class grant { private $token; private $encodingAesKey; private $appid; private $appsecret; /** * 构造函数 * @param $token string 公众平台上,开发者设置的token * @param $encodingAesKey string 公众平台上,开发者设置的EncodingAESKey * @param $appId string 公众平台的appId */ public function __construct($token, $encodingAesKey, $appid, $appsecret) { $this->token = $token; $this->encodingAesKey = $encodingAesKey; $this->appid = $appid; $this->appsecret = $appsecret; } public function rqcode($component_access_token) { $preAuthCode = $this->preAuthCode($component_access_token); $redirect_uri = "http://chat.wenidc.com/kefu/wechat/callback"; //回调地址 $url = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=$this->appid&pre_auth_code=${preAuthCode}&redirect_uri=${redirect_uri}"; return $url; } //2,获取第三方平台component_access_token public function componentAccessToken($component_verify_ticket) { $url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token"; $data = array( "component_appid" => $this->appid, "component_appsecret" => $this->appsecret, "component_verify_ticket" => $component_verify_ticket ); return json_decode($this->https_request($url, json_encode($data)), true); } //3,获取预授权码pre_auth_code public function preAuthCode($component_access_token) { $url = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=$component_access_token"; $data = array( "component_appid" => $this->appid, ); $send_result = $this->https_request($url, json_encode($data)); $send_result = json_decode($send_result, true); if (!array_key_exists('errcode', $send_result)) { return $send_result['pre_auth_code'];// 微信预授权码 } else { return ''; } } //4,使用授权码换取公众号或小程序的接口调用凭据和授权信息 public function apiQueryAuth($component_access_token, $auth_code) { $url = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=$component_access_token"; $data = array( "component_appid" => $this->appid, "authorization_code" => $auth_code, ); $send_result = json_decode($this->https_request($url, json_encode($data)), true); return $send_result; }
标签:info,authorizer,微信,component,access,token,appid,授权 来源: https://blog.csdn.net/joker6295/article/details/89435663