From 69e8b41587ea4f7650cc34bd66d3803f96ec9ac9 Mon Sep 17 00:00:00 2001 From: yin5th <541304803@qq.com> Date: Tue, 26 Sep 2023 09:59:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=BC=80=E6=94=BE=E7=BB=99=E7=AC=AC?= =?UTF-8?q?=E4=B8=89=E6=96=B9=E6=8E=A5=E5=8F=A3=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/app/api/controller/Account.php | 27 +++++++++++++--- server/app/api/logic/LoginLogic.php | 41 ++++++++++++++++++++++++ server/app/common/enum/ClientEnum.php | 5 +++ server/app/common/model/user/UserApi.php | 9 ++++++ 4 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 server/app/common/model/user/UserApi.php diff --git a/server/app/api/controller/Account.php b/server/app/api/controller/Account.php index 21a293d8..03214f71 100644 --- a/server/app/api/controller/Account.php +++ b/server/app/api/controller/Account.php @@ -27,11 +27,13 @@ use app\api\validate\RegisterValidate; use app\api\validate\LoginValidate; use think\exception\ValidateException; use app\api\validate\OaLoginValidate; +use think\response\Json; class Account extends Api { - public $like_not_need_login = ['silentLogin', 'authLogin', 'register', 'login','mnplogin', 'codeurl', 'oalogin', 'oplogin','logout','smslogin', 'uinAppLogin', 'silentLogin', 'authLogin', 'scanCode', 'scanLogin']; + public $like_not_need_login = ['silentLogin', 'authLogin', 'register', 'login','mnplogin', 'codeurl', 'oalogin', + 'oplogin','logout','smslogin', 'uinAppLogin', 'silentLogin', 'authLogin', 'scanCode', 'scanLogin', 'apiLogin']; /** * 注册 @@ -167,7 +169,7 @@ class Account extends Api /*** * 短信登录 - * @return \think\response\Json + * @return Json */ public function smsLogin() { @@ -195,7 +197,7 @@ class Account extends Api /** * @notes 扫码登录 - * @return \think\response\Json + * @return Json * @author 段誉 * @date 2021/10/29 11:50 */ @@ -211,7 +213,7 @@ class Account extends Api /** * @notes PC端扫码登录 - * @return \think\response\Json + * @return Json * @author 段誉 * @date 2021/10/29 18:00 */ @@ -225,5 +227,20 @@ class Account extends Api return JsonServer::success('', $result); } - + /** + * AppID登录 + * 开放给第三方使用 + */ + public function apiLogin(): Json + { + $post = $this->request->post(); + if (empty($post['app_id']) || empty($post['app_secret'])) { + return JsonServer::error('参数错误'); + } + $data = LoginLogic::apiLogin($post); + if (false === $data) { + return JsonServer::error(LoginLogic::getError() ?? '未知错误'); + } + return JsonServer::success('登录成功',$data); + } } \ No newline at end of file diff --git a/server/app/api/logic/LoginLogic.php b/server/app/api/logic/LoginLogic.php index 90ee9f6b..705e0d62 100644 --- a/server/app/api/logic/LoginLogic.php +++ b/server/app/api/logic/LoginLogic.php @@ -24,6 +24,7 @@ use app\api\server\UserServer; use app\common\basics\Logic; use app\common\enum\ClientEnum; use app\common\model\distribution\Distribution; +use app\common\model\user\UserApi; use app\common\server\WeChatServer; use app\common\server\ConfigServer; use app\common\model\Client_; @@ -624,5 +625,45 @@ class LoginLogic extends Logic } } + /** + * 第三方接口登录 APPID和appSecret + */ + public static function apiLogin($post) + { + try { + if (!$userApi = UserApi::where('app_id', $post['app_id'])->where('app_secret', $post['app_secret'])->find()) { + throw new \Exception('账号错误'); + } + $user = User::field(['id', 'nickname', 'avatar', 'level', 'disable', 'distribution_code', 'is_api', 'del']) + ->where(['id' => $userApi['id']]) + ->findOrEmpty()->toArray(); + if ($user['disable'] > 0) { + throw new \Exception('账号已禁用'); + } + + if ($user['del'] == 1) { + throw new \Exception('账号异常,请联系管理员'); + } + + if ($user['is_api'] != 1) { + throw new \Exception('账号API授权已关闭'); + } + + unset($user['del']); + + $user['token'] = self::createSession($user['id'], ClientEnum::api); + if (empty($user['avatar'])) { + $user['avatar'] = UrlServer::getFileUrl(ConfigServer::get('website', 'user_image')); + } else { + $user['avatar'] = UrlServer::getFileUrl($user['avatar']); + } + return $user; + } catch (\Exception $e) { + self::$error = $e->getMessage(); + return false; + } + + } + } diff --git a/server/app/common/enum/ClientEnum.php b/server/app/common/enum/ClientEnum.php index 3f7bedd3..4f378ab7 100644 --- a/server/app/common/enum/ClientEnum.php +++ b/server/app/common/enum/ClientEnum.php @@ -30,6 +30,7 @@ class ClientEnum const pc = 5; const h5 = 6;//h5(非微信环境h5) const backend = 7;//PC后台 + const api = 8;//第三方API登录 function getName($value) { @@ -54,6 +55,9 @@ class ClientEnum case self::backend: $name = '后台'; break; + case self::api: + $name = 'API'; + break; } return $name; } @@ -69,6 +73,7 @@ class ClientEnum self::ios => '苹果APP商城', self::android => '安卓APP商城', self::backend => '后台', + self::api => 'API', ]; if ($type === true) { return $desc; diff --git a/server/app/common/model/user/UserApi.php b/server/app/common/model/user/UserApi.php new file mode 100644 index 00000000..c5e25a0d --- /dev/null +++ b/server/app/common/model/user/UserApi.php @@ -0,0 +1,9 @@ +