You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
181 lines
6.2 KiB
181 lines
6.2 KiB
<?php
|
|
|
|
namespace app\controller\wechat;
|
|
|
|
use app\model\WechatUser;
|
|
use app\service\user\LoginService;
|
|
use app\util\AuthCodeUtil;
|
|
use app\util\ReturnCode;
|
|
use think\cache\driver\Redis;
|
|
use think\facade\Cache;
|
|
use think\facade\Request;
|
|
use think\Response;
|
|
|
|
class Login extends Base
|
|
{
|
|
/**
|
|
* @var
|
|
*/
|
|
public $auth;
|
|
protected $valid = \app\validate\Login::class;
|
|
|
|
/**
|
|
* @title 登录凭证校验
|
|
* @return \think\Response|void
|
|
*/
|
|
public function code2session(){
|
|
|
|
try {
|
|
validate($this->valid)->scene('code2session')->check(Request::post());
|
|
$code = $this->request->param('code');
|
|
$iv = $this->request->param('iv');
|
|
$encryptedData = $this->request->param('encryptedData');
|
|
$loginService = new LoginService();
|
|
$user = $loginService->code2session($code, $iv, $encryptedData);
|
|
return $this->buildSuccess($user);
|
|
} catch (\Exception $e) { //错误消息 $e->getMessage()
|
|
return $this->buildFailed($e->getCode() ?: 400,$e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @title 登录
|
|
* @return \think\Response|void
|
|
*/
|
|
public function login(){
|
|
try {
|
|
validate($this->valid)->scene('login')->check(Request::post());
|
|
$phone = $this->request->post('phone');
|
|
$openid = $this->request->post('openid');
|
|
$unionid = $this->request->post('unionid');
|
|
$loginService = new LoginService();
|
|
$user = $loginService->userLogin($phone, $openid, $unionid);
|
|
$user['token'] = $this->signToken($user);
|
|
return $this->buildSuccess($user);
|
|
} catch (\Exception $e) { //错误消息 $e->getMessage()
|
|
return $this->buildFailed($e->getCode() ?: 400,$e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return Response
|
|
*/
|
|
public function phoneLogin(): Response
|
|
{
|
|
try {
|
|
$post = $this->request->post();
|
|
validate($this->valid)->scene('phoneLogin')->check($post);
|
|
$res = (new AuthCodeUtil(new Redis()))->verifyCode($post['phone'],$post['smsCode']);
|
|
if (!$res) {
|
|
throw new \Exception('短信验证码错误');
|
|
}
|
|
$loginServer = new LoginService();
|
|
$user = $loginServer->userPhoneLogin($post['phone']);
|
|
$user['token'] = $this->signToken($user);
|
|
return $this->buildSuccess($user);
|
|
}catch (\Exception $e) {
|
|
return $this->buildFailed(ReturnCode::INVALID, $e->getMessage());
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 账号密码登录
|
|
* @return Response
|
|
*/
|
|
public function accountLogin(): Response
|
|
{
|
|
try {
|
|
$post = $this->request->post();
|
|
validate($this->valid)->scene('accountLogin')->check($post);
|
|
$loginServer = new LoginService();
|
|
$user = $loginServer->userAccountLogin($post['account'], $post['password']);
|
|
$user['token'] = $this->signToken($user);
|
|
return $this->buildSuccess($user);
|
|
} catch (\Exception $e) {
|
|
return $this->buildFailed(ReturnCode::INVALID, $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function getWxCode() {
|
|
$state = md5(uniqid() . time());
|
|
cache($state, $state, 300);
|
|
|
|
return $this->buildSuccess([
|
|
'appId' => env('wechat.appid'),
|
|
'redirectUri' => urlencode(env('wechat.redirect_uri')),
|
|
'state' => $state
|
|
]);
|
|
}
|
|
|
|
public function wx(): Response {
|
|
validate($this->valid)->scene('wxLogin')->check(Request::get());
|
|
$state = $this->request->param('state', '');
|
|
$code = $this->request->param('code', '');
|
|
|
|
//验证合法性
|
|
$cacheData = Cache::has($state);
|
|
if (!$cacheData) {
|
|
return $this->buildFailed(ReturnCode::SESSION_TIMEOUT, 'state已过期');
|
|
} else {
|
|
cache($state, null);
|
|
}
|
|
|
|
//获取AccessToken
|
|
$getAccessTokenUrl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' .
|
|
env('wechat.appid') . '&secret=' . env('wechat.appsecret') . '&code=' . $code .
|
|
'&grant_type=authorization_code';
|
|
|
|
$tokenArr = file_get_contents($getAccessTokenUrl);
|
|
$accessTokenArr = json_decode($tokenArr, true);
|
|
if (!$accessTokenArr || isset($accessTokenArr['errcode'])) {
|
|
return $this->buildFailed(ReturnCode::SESSION_TIMEOUT, '获取AccessToken失败');
|
|
}
|
|
|
|
//获取openId
|
|
$getUserIdUrl = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $accessTokenArr['access_token'] . '&openid=' . $accessTokenArr['openid'];
|
|
$userIdArr = file_get_contents($getUserIdUrl);
|
|
$userIdArr = json_decode($userIdArr, true);
|
|
if (!$userIdArr || isset($userIdArr['errcode'])) {
|
|
return $this->buildFailed(ReturnCode::SESSION_TIMEOUT, '获取用户信息失败');
|
|
}
|
|
|
|
return $this->doLogin($userIdArr['openid'], [
|
|
'nickname' => $userIdArr['nickname'],
|
|
'head_img' => $userIdArr['headimgurl']
|
|
]);
|
|
}
|
|
|
|
private function doLogin(string $openid, array $userDetail): Response {
|
|
$userInfo = (new WechatUser())->openIdUserInfo($openid);
|
|
if (empty($userInfo)) {
|
|
$userInfo = WechatUser::create([
|
|
'nickname' => $userDetail['nickname'],
|
|
'openid' => $openid,
|
|
'sex' => $userDetail['sex'] ?? 0,
|
|
'unionid' => $userDetail['unionid'] ?? '',
|
|
'create_time' => time(),
|
|
'headimgurl' => $userDetail['head_img'],
|
|
'last_login_time' => time()
|
|
]);
|
|
$data = (new WechatUser())->getUserInfo($userInfo['id']);
|
|
} else {
|
|
if (!$userInfo['status']) {
|
|
return $this->buildFailed(ReturnCode::LOGIN_ERROR, '用户已被封禁,请联系管理员');
|
|
}
|
|
unset($userInfo['status']);
|
|
(new WechatUser())->addLoginTime($userInfo['id']);
|
|
$data = $userInfo;
|
|
}
|
|
|
|
|
|
$data['token'] = $this->signToken($data);
|
|
return $this->buildSuccess($data->toArray(), '登录成功');
|
|
}
|
|
|
|
public function userLogout()
|
|
{
|
|
|
|
}
|
|
}
|
|
|