/**
* Undocumented function
* 第三方登录[绑定] 小程序
* @return void
*/
public function wxLogin()
{
$code = $this->request->param("code");
$WxUser = $this->request->param("rawData/a", '', 'trim');
// if (!$code || !$WxUser) {
// $this->error("参数不正确");
// }
$third = get_addon_info('third');
if (!$third || !$third['state']) {
$this->error("请在后台插件管理安装并配置第三方登录插件");
}
$config = get_addon_config('third');
$wechatConfig = $config['wechat'];
$params = [
'appid' => $wechatConfig['app_id'],
'secret' => $wechatConfig['app_secret'],
'js_code' => $code,
'grant_type' => 'authorization_code'
];
$result = Http::sendRequest("https://api.weixin.qq.com/sns/jscode2session", $params, 'GET');
if ($result['ret']) {
$json = (array)json_decode($result['msg'], true);
if (isset($json['openid'])) {
$userinfo = [
'platform' => 'wechat',
'apptype' => 'miniapp',
'openid' => $json['openid'],
'userinfo' => [
'nickname' => $WxUser['nickName'],
'avatar' => $WxUser['avatarUrl']
],
'openname' => $WxUser['nickName'],
'access_token' => $json['session_key'],
'refresh_token' => '',
'expires_in' => isset($json['expires_in']) ? $json['expires_in'] : 0,
'unionid' => isset($json['unionid']) ? $json['unionid'] : ''
];
$third = [
'nickname' => $WxUser['nickName'],
'avatar' => $WxUser['avatarUrl']
];
$user = null;
if ($this->auth->isLogin() || Service::isBindThird($userinfo['platform'], $userinfo['openid'], $userinfo['apptype'], $userinfo['unionid'])) {
Service::connect($userinfo['platform'], $userinfo);
$user = $this->auth->getUserinfo();
$user['avatar'] = cdnurl($user['avatar'],true);
$user['avatar'] = cdnurl($user['avatar'],true);
$user['nickname'] = $WxUser['nickName'];
$user['avatar'] = $WxUser['avatarUrl'];
} else {
cache('third-userinfo', $userinfo);
}
$this->success('授权成功!', ['user' => $user, 'third' => $third]);
}
}
$this->error("授权失败,".$result['msg']);
}
//小程序使用微信手机号码授权绑定
public function getWechatMobile()
{
$encryptedData = $this->request->post('encryptedData');
$iv = $this->request->post('iv');
$code = $this->request->post("code");
$pid = $this->request->post('pid', 0);
if (!$encryptedData || !$iv) {
$this->error('参数错误!');
}
if (strlen($iv) != 24) {
$this->error('iv不正确!');
}
$third = cache('third-userinfo');
$params = [
'appid' => config('site.wechat_appid'),
'secret' => config('site.wechat_appsecret'),
'js_code' => $code,
'grant_type' => 'authorization_code'
];
$result = Http::sendRequest("https://api.weixin.qq.com/sns/jscode2session", $params, 'GET');
if ($result['ret']) {
$json = (array)json_decode($result['msg'], true);
$aesIV = base64_decode($iv);
$aesCipher = base64_decode($encryptedData);
$aesKey = base64_decode($json['session_key']);
$re = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
$data = json_decode($re, true);
if ($data) {
$user = \app\common\model\User::getByMobile($data['phoneNumber']);
if ($user) {
if ($user->status != 'normal') {
$this->error(__('Account is locked'));
}
//如果已经有账号则直接登录
$ret = $this->auth->direct($user->id);
} else {
$isrepeat = true;
while($isrepeat)
{
$code = Random::alnum();
$total = \app\common\model\User::where('invite_code', $code)->count();
if(!$total)
{
$isrepeat = false;
}
}
$ret = $this->auth->register($data['phoneNumber'], Random::alnum(), '', $data['phoneNumber'], ['avatar'=>$third['userinfo']['avatar'], 'nickname'=>$third['userinfo']['nickname'], 'pid'=>$pid, 'invite_code'=>$code] );
}
if ($ret) {
//绑定第三方
Service::connect('wechat', $third);
$data = ['token' => $this->auth->getToken()];
$data['userinfo'] = $this->auth->getUserinfo();
$this->success(__('Logged in successful'), $data);
} else {
$this->error($this->auth->getError());
}
} else {
$this->error("配置参数错误");
}
} else {
$this->error("授权失败");
}
}