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); + } +}