From dd1fac2198247d7422ab8131891da8e2309bdf28 Mon Sep 17 00:00:00 2001 From: gcd Date: Fri, 28 Mar 2025 00:35:18 +0800 Subject: [PATCH] =?UTF-8?q?env=20=E5=A2=9E=E5=8A=A0=E5=B0=8F=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E9=85=8D=E7=BD=AE=E3=80=81=E5=A2=9E=E5=8A=A0=20API=20?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=E3=80=81=E5=B8=88=E5=82=85?= =?UTF-8?q?=E6=89=8B=E6=9C=BA=E5=8F=B7=E7=99=BB=E5=BD=95=E6=8E=88=E6=9D=83?= =?UTF-8?q?=E8=A7=A3=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.sample | 4 ++ application/api/controller/Worker.php | 22 +++++++++ application/api/library/ApiException.php | 52 +++++++++++++++++++++ application/api/library/ExceptionHandle.php | 5 ++ application/api/validate/Worker.php | 20 ++++++++ application/common/controller/Api.php | 3 +- application/common/model/Worker.php | 18 +++++++ application/config.php | 4 ++ application/services/BaseService.php | 39 ++++++++++++++++ application/services/WorkerService.php | 30 ++++++++++++ 10 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 application/api/controller/Worker.php create mode 100644 application/api/library/ApiException.php create mode 100644 application/api/validate/Worker.php create mode 100644 application/common/model/Worker.php create mode 100644 application/services/BaseService.php create mode 100644 application/services/WorkerService.php diff --git a/.env.sample b/.env.sample index ccd0f29..214e5d6 100755 --- a/.env.sample +++ b/.env.sample @@ -9,3 +9,7 @@ username = root password = root hostport = 3306 prefix = fa_ + +[mini_program] +app_id = +secret = diff --git a/application/api/controller/Worker.php b/application/api/controller/Worker.php new file mode 100644 index 0000000..70aa643 --- /dev/null +++ b/application/api/controller/Worker.php @@ -0,0 +1,22 @@ +request->request(); + $validate = $this->validate($params, \app\api\validate\Worker::class . '.phoneLogin'); + if ($validate !== true) { + $this->error($validate); + } + + $this->success('登录成功', $this->getWorkerService()->phoneLogin($params['code'])); + } +} diff --git a/application/api/library/ApiException.php b/application/api/library/ApiException.php new file mode 100644 index 0000000..1950fda --- /dev/null +++ b/application/api/library/ApiException.php @@ -0,0 +1,52 @@ +getMessage(), + $code->getFile(), + $code->getLine(), + ]); + $code = 0; + } + + if ($code === null) { + $code = 0; + } + + $this->statusCode = $statusCode; + $this->headers = $headers; + $this->errorCode = $code; + + parent::__construct($message, $statusCode, $previous); + } + + public function getStatusCode() + { + return $this->statusCode; + } + + public function getHeaders() + { + return $this->headers; + } + + public function getErrorCode() + { + return $this->errorCode; + } +} diff --git a/application/api/library/ExceptionHandle.php b/application/api/library/ExceptionHandle.php index 852f7ef..7c11118 100755 --- a/application/api/library/ExceptionHandle.php +++ b/application/api/library/ExceptionHandle.php @@ -13,6 +13,11 @@ class ExceptionHandle extends Handle public function render(Exception $e) { + // API 异常 + if ($e instanceof ApiException) { + return json(['code' => $e->getErrorCode(), 'msg' => $e->getMessage(), 'time' => time(), 'data' => null], $e->getStatusCode()); + } + // 在生产环境下返回code信息 if (!\think\Config::get('app_debug')) { $statuscode = $code = 500; diff --git a/application/api/validate/Worker.php b/application/api/validate/Worker.php new file mode 100644 index 0000000..496db24 --- /dev/null +++ b/application/api/validate/Worker.php @@ -0,0 +1,20 @@ + 'require|max:128', + ]; + + protected $message = [ + + ]; + + protected $scene = [ + 'phoneLogin' => ['code'], + ]; +} diff --git a/application/common/controller/Api.php b/application/common/controller/Api.php index f685ca2..1ca0c4e 100755 --- a/application/common/controller/Api.php +++ b/application/common/controller/Api.php @@ -3,6 +3,7 @@ namespace app\common\controller; use app\common\library\Auth; +use app\services\BaseService; use think\Config; use think\exception\HttpResponseException; use think\exception\ValidateException; @@ -17,7 +18,7 @@ use think\Validate; /** * API控制器基类 */ -class Api +class Api extends BaseService { /** diff --git a/application/common/model/Worker.php b/application/common/model/Worker.php new file mode 100644 index 0000000..d6ebe9b --- /dev/null +++ b/application/common/model/Worker.php @@ -0,0 +1,18 @@ + 'https://api.fastadmin.net', ], + 'mini_program' => [ + 'app_id' => Env::get('mini_program.app_id', ''), + 'secret' => Env::get('mini_program.secret', ''), + ] ]; diff --git a/application/services/BaseService.php b/application/services/BaseService.php new file mode 100644 index 0000000..56e3b1f --- /dev/null +++ b/application/services/BaseService.php @@ -0,0 +1,39 @@ + config('mini_program.app_id'), + 'secret' => config('mini_program.secret'), + ]; + $app = Factory::miniProgram($config); + $phoneInfo = $app->getPhoneNumber($code); + + if (empty($phoneInfo)) { + $this->apiError('手机号登录失败', 0, $phoneInfo); + } + + if ($phoneInfo['errcode'] !== 0) { + $this->apiError('手机号登录失败', 0, $phoneInfo); + } + + $phone = $phoneInfo['phone_info']['phoneNumber']; + dump($phone); + } +}