Browse Source

手机号登录接口

master
wanghongjun 1 year ago
parent
commit
79496209d3
  1. 19
      app/controller/wechat/Login.php
  2. 44
      app/service/user/LoginService.php
  3. 2
      app/validate/Login.php

19
app/controller/wechat/Login.php

@ -55,6 +55,25 @@ class Login extends Base
} }
} }
/**
*
* @return Response
*/
public function phoneLogin(): Response
{
try {
$post = $this->request->post();
validate($this->valid)->scene('phoneLogin')->check($post);
$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());
}
}
public function getWxCode() { public function getWxCode() {
$state = md5(uniqid() . time()); $state = md5(uniqid() . time());
cache($state, $state, 300); cache($state, $state, 300);

44
app/service/user/LoginService.php

@ -17,7 +17,8 @@ class LoginService extends BaseService
* 判断登录状态 * 判断登录状态
* @return bool * @return bool
*/ */
public function isLogin() { public function isLogin()
{
if (!$this->user) { if (!$this->user) {
return false; return false;
} }
@ -32,7 +33,8 @@ class LoginService extends BaseService
* @return array * @return array
* @throws \fast\FuncException * @throws \fast\FuncException
*/ */
public function code2session($code, $iv, $encryptedData){ public function code2session($code, $iv, $encryptedData)
{
$http = new Http(); $http = new Http();
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . env("app.appid") . "&secret=" . env("app.appsecret") . "&js_code={$code}&grant_type=authorization_code"; $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . env("app.appid") . "&secret=" . env("app.appsecret") . "&js_code={$code}&grant_type=authorization_code";
@ -63,7 +65,8 @@ class LoginService extends BaseService
* @return false|string * @return false|string
* @throws \fast\FuncException * @throws \fast\FuncException
*/ */
public function decodeWechatIv($iv, $encryptedData){ public function decodeWechatIv($iv, $encryptedData)
{
$openid = session('app_openid'); $openid = session('app_openid');
$session_key = session('app_session_key'); $session_key = session('app_session_key');
if (!$openid || !$session_key) { if (!$openid || !$session_key) {
@ -102,7 +105,8 @@ class LoginService extends BaseService
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public function userLogin($phone, $openid, $unionid) { public function userLogin($phone, $openid, $unionid)
{
$field = 'id,openid,phone,nickname,sex,headimgurl'; $field = 'id,openid,phone,nickname,sex,headimgurl';
$user = WechatUser::where('phone', $phone)->where('openid', $openid)->where('delete_time', 0)->field($field)->find(); $user = WechatUser::where('phone', $phone)->where('openid', $openid)->where('delete_time', 0)->field($field)->find();
if ($user) { if ($user) {
@ -112,12 +116,32 @@ class LoginService extends BaseService
} }
/**
*
* @param $phone
* @return WechatUser|array|mixed
* @throws \fast\FuncException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function userPhoneLogin($phone)
{
$field = 'id,openid,phone,nickname,sex,headimgurl';
$user = WechatUser::where('phone', $phone)->where('delete_time', 0)->field($field)->find();
if ($user) {
return $this->userSuccess($user);
}
return $this->register($phone);
}
/** /**
* 用户登录成功 * 用户登录成功
* @param WechatUser $user * @param WechatUser $user
* @return array * @return array
*/ */
public function userSuccess(WechatUser $user) { public function userSuccess(WechatUser $user)
{
session('user', $user->toArray()); session('user', $user->toArray());
$this->userKeeplogin($user->id,$user->openid,3600 * 24 * 7); $this->userKeeplogin($user->id,$user->openid,3600 * 24 * 7);
// $user->visible(['id', 'name', 'logo']); // $user->visible(['id', 'name', 'logo']);
@ -131,7 +155,8 @@ class LoginService extends BaseService
* @param $keeptime * @param $keeptime
* @return bool * @return bool
*/ */
protected function userKeeplogin($user_id,$token,$keeptime = 0) { protected function userKeeplogin($user_id, $token, $keeptime = 0)
{
if ($keeptime) { if ($keeptime) {
$expiretime = time() + $keeptime; $expiretime = time() + $keeptime;
@ -149,15 +174,16 @@ class LoginService extends BaseService
/** /**
* 用户端注册 * 用户端注册
* @param $phone * @param $phone
* @param $openid * @param string $openid
* @param $unionid * @param string $unionid
* @return WechatUser|mixed * @return WechatUser|mixed
* @throws \fast\FuncException * @throws \fast\FuncException
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public function register($phone, $openid, $unionid) { public function register($phone, string $openid = '', string $unionid = '')
{
$add = [ $add = [
'phone' => $phone, 'phone' => $phone,

2
app/validate/Login.php

@ -18,6 +18,7 @@ class Login extends Validate
'encryptedData|encryptedData' => 'require', 'encryptedData|encryptedData' => 'require',
'state|state' => 'require', 'state|state' => 'require',
#'unionid' => 'require', #'unionid' => 'require',
'smsCode|短信验证码' => 'require|in:0000',
]; ];
/** /**
@ -39,5 +40,6 @@ class Login extends Validate
'code2session' => ['code', 'iv', 'encryptedData'], 'code2session' => ['code', 'iv', 'encryptedData'],
'login' => ['phone', 'openid', 'unionid'], 'login' => ['phone', 'openid', 'unionid'],
'wxLogin' => ['code', 'state'], 'wxLogin' => ['code', 'state'],
'phoneLogin' => ['phone', 'smsCode'],
]; ];
} }
Loading…
Cancel
Save