env 增加小程序配置、增加 API 异常处理、师傅手机号登录授权解密
This commit is contained in:
parent
711a54eb0f
commit
dd1fac2198
|
|
@ -9,3 +9,7 @@ username = root
|
||||||
password = root
|
password = root
|
||||||
hostport = 3306
|
hostport = 3306
|
||||||
prefix = fa_
|
prefix = fa_
|
||||||
|
|
||||||
|
[mini_program]
|
||||||
|
app_id =
|
||||||
|
secret =
|
||||||
|
|
|
||||||
22
application/api/controller/Worker.php
Normal file
22
application/api/controller/Worker.php
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\api\controller;
|
||||||
|
|
||||||
|
use app\common\controller\Api;
|
||||||
|
|
||||||
|
class Worker extends Api
|
||||||
|
{
|
||||||
|
protected $noNeedLogin = ['phoneLogin'];
|
||||||
|
protected $noNeedRight = ['*'];
|
||||||
|
|
||||||
|
function phoneLogin()
|
||||||
|
{
|
||||||
|
$params = $this->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']));
|
||||||
|
}
|
||||||
|
}
|
||||||
52
application/api/library/ApiException.php
Normal file
52
application/api/library/ApiException.php
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\api\library;
|
||||||
|
|
||||||
|
use think\Log;
|
||||||
|
|
||||||
|
class ApiException extends \RuntimeException
|
||||||
|
{
|
||||||
|
private $statusCode;
|
||||||
|
|
||||||
|
private $headers;
|
||||||
|
|
||||||
|
private $errorCode;
|
||||||
|
|
||||||
|
public function __construct($message, $code = 0, \Exception $previous = null, array $headers = [], $statusCode = 200)
|
||||||
|
{
|
||||||
|
if ($code instanceof \Exception) {
|
||||||
|
Log::error([
|
||||||
|
$message,
|
||||||
|
$code->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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -13,6 +13,11 @@ class ExceptionHandle extends Handle
|
||||||
|
|
||||||
public function render(Exception $e)
|
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信息
|
// 在生产环境下返回code信息
|
||||||
if (!\think\Config::get('app_debug')) {
|
if (!\think\Config::get('app_debug')) {
|
||||||
$statuscode = $code = 500;
|
$statuscode = $code = 500;
|
||||||
|
|
|
||||||
20
application/api/validate/Worker.php
Normal file
20
application/api/validate/Worker.php
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\api\validate;
|
||||||
|
|
||||||
|
use think\Validate;
|
||||||
|
|
||||||
|
class Worker extends Validate
|
||||||
|
{
|
||||||
|
protected $rule = [
|
||||||
|
'code|手机号获取凭证 code ' => 'require|max:128',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $message = [
|
||||||
|
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $scene = [
|
||||||
|
'phoneLogin' => ['code'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
namespace app\common\controller;
|
namespace app\common\controller;
|
||||||
|
|
||||||
use app\common\library\Auth;
|
use app\common\library\Auth;
|
||||||
|
use app\services\BaseService;
|
||||||
use think\Config;
|
use think\Config;
|
||||||
use think\exception\HttpResponseException;
|
use think\exception\HttpResponseException;
|
||||||
use think\exception\ValidateException;
|
use think\exception\ValidateException;
|
||||||
|
|
@ -17,7 +18,7 @@ use think\Validate;
|
||||||
/**
|
/**
|
||||||
* API控制器基类
|
* API控制器基类
|
||||||
*/
|
*/
|
||||||
class Api
|
class Api extends BaseService
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
18
application/common/model/Worker.php
Normal file
18
application/common/model/Worker.php
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\common\model;
|
||||||
|
|
||||||
|
use think\Model;
|
||||||
|
|
||||||
|
class Worker extends Model
|
||||||
|
{
|
||||||
|
protected $visible = [
|
||||||
|
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $hidden = [
|
||||||
|
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -318,4 +318,8 @@ return [
|
||||||
//API接口地址
|
//API接口地址
|
||||||
'api_url' => 'https://api.fastadmin.net',
|
'api_url' => 'https://api.fastadmin.net',
|
||||||
],
|
],
|
||||||
|
'mini_program' => [
|
||||||
|
'app_id' => Env::get('mini_program.app_id', ''),
|
||||||
|
'secret' => Env::get('mini_program.secret', ''),
|
||||||
|
]
|
||||||
];
|
];
|
||||||
|
|
|
||||||
39
application/services/BaseService.php
Normal file
39
application/services/BaseService.php
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\services;
|
||||||
|
|
||||||
|
use app\api\library\ApiException;
|
||||||
|
use app\common\model\Worker;
|
||||||
|
use think\Log;
|
||||||
|
|
||||||
|
//{%add use model%}
|
||||||
|
|
||||||
|
class BaseService
|
||||||
|
{
|
||||||
|
protected function apiError($msg, $code = 0, $data = [])
|
||||||
|
{
|
||||||
|
if (!empty($data)) {
|
||||||
|
array_unshift($data, $msg);
|
||||||
|
Log::log($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new ApiException($msg, $code);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @return WorkerService
|
||||||
|
*/
|
||||||
|
protected function getWorkerService()
|
||||||
|
{
|
||||||
|
return app(WorkerService::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Worker
|
||||||
|
*/
|
||||||
|
protected function getWorkerModel()
|
||||||
|
{
|
||||||
|
return app(Worker::class, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
//{%add function code%}
|
||||||
|
}
|
||||||
30
application/services/WorkerService.php
Normal file
30
application/services/WorkerService.php
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\services;
|
||||||
|
|
||||||
|
use app\api\library\ApiException;
|
||||||
|
use EasyWeChat\Factory;
|
||||||
|
|
||||||
|
class WorkerService extends BaseService
|
||||||
|
{
|
||||||
|
public function phoneLogin(string $code)
|
||||||
|
{
|
||||||
|
$config = [
|
||||||
|
'app_id' => 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user