~~~
/**
* 第三方登录
*
* @param string $platform 平台名称
* @param string $code Code码
*/
public function third()
{
$platform = $this->request->request("platform",'wechat');
$code = $this->request->request("code");
$state = $this->request->request("state");
$config = get_addon_config('third');
if (!$config || !isset($config[$platform])) {
$this->error(__('Invalid parameters'));
}
//获取openid 和 ACCESS_TOKEN
$tokenurl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$config['wechat']['app_id']."&secret=".$config['wechat']['app_secret']."&code=".$code."&grant_type=authorization_code";
$result = geturl($tokenurl);
// 获取 access token 实例 获取关注信息
$easywechat = Factory::officialAccount(wechatConfig::load());
$accessToken = $easywechat->access_token;
$token = $accessToken->getToken(); // token 数组 token['access_token'] 字符串
$gzurl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$token['access_token']."&openid=".$result['openid']."&lang=zh_CN";
$gzdata = geturl($gzurl);
$result['subscribe'] = $gzdata['subscribe'];
//获取用户信息 https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
$userurl = "https://api.weixin.qq.com/sns/userinfo?access_token=".$result['access_token']."&openid=".$result['openid']."&lang=zh_CN";
$userdata = geturl($userurl);
if ($userdata['country'] && $userdata['province'] && $userdata['city']){
$userdata['wxarea'] = $userdata['country'].$userdata['province'].$userdata['city'];
} elseif ($userdata['country']){
$userdata['wxarea'] = $userdata['country'];
}elseif ($userdata['province']){
$userdata['wxarea'] = $userdata['province'];
}elseif ($userdata['city']){
$userdata['wxarea'] = $userdata['city'];
}else{
$userdata['wxarea'] = '';
}
$users = array_merge($userdata,$result);
$users['avatar'] = isset($users['headimgurl']) ? $users['headimgurl'] : '';
if ($result) {
$loginret = \addons\third\library\Service::connect($platform, $users);
if ($loginret) {
$data = [
'userinfo' => $this->auth->getUserinfo(),
'thirdinfo' => $userdata
];
$token = $data['userinfo']['token'];
$url = 'http://miaomu.yskxfs.com/h7/#/pages/tabbars/tab_mine?token='.$token;
$this->redirect($url);
//$this->success(__('Logged in successful'), $data);
}
}
$this->error(__('Operation failed'), $url);
}
~~~