api/app/middleware/ApiCheck.php
2019-06-22 22:03:22 +08:00

51 lines
1.4 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: yancheng<cheng@love.xiaoyan.me>
* Date: 2019/6/19
* Time: 6:35 PM
*/
namespace app\middleware;
use app\model\UserInfo;
use app\util\ErrorCode;
use app\util\ErrorResponse;
use think\facade\Config;
use think\facade\Env;
use think\Request;
class ApiCheck
{
/**
* 在进行业务前处理用户token
* @param Request $request
* @param \Closure $next
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function handle(Request $request, \Closure $next)
{
$open_id = Env::get('app_debug') ? 'wxaffadsf31Dfaf93wxaffadsf31Dfaf93' : $request->param('open_id');//'wxaffadsf31Dfaf93';
if (empty($open_id)) {
return ErrorResponse::createError(
ErrorCode::ERROR_OPENID_REQUIRED, '缺失参数open_id'
);
}
$user = UserInfo::where('open_id', $open_id)->find();
if(empty($user)){
return ErrorResponse::createError(
ErrorCode::ERROR_USER_NOT_EXISTS, '用户不存在或者open_id错误'
);
}
$request->user = $user;
//对于 admin -> token
//对于 user -> open_id
$response = $next($request);
return $response;
}
}