You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
2.9 KiB
113 lines
2.9 KiB
<?php
|
|
declare (strict_types=1);
|
|
/**
|
|
* 工程基类
|
|
* @since 2017/02/28 创建
|
|
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
|
*/
|
|
|
|
namespace app\controller\wechat;
|
|
|
|
use app\BaseController;
|
|
use app\service\user\LoginService;
|
|
use app\util\ReturnCode;
|
|
use fast\FuncException;
|
|
use think\App;
|
|
use think\facade\Env;
|
|
use think\facade\Request;
|
|
use think\facade\Session;
|
|
use think\Response;
|
|
|
|
class Base extends BaseController {
|
|
|
|
private $debug = [];
|
|
protected $noNeedLogin = [];
|
|
protected $user = [];
|
|
public $user_id = '';
|
|
|
|
// public function __construct()
|
|
// {
|
|
// $app = new App();
|
|
// parent::__construct($app);
|
|
//
|
|
// try {
|
|
// if(!$this->user){
|
|
// $bool = (new LoginService())->userAutologin();
|
|
// if($bool){
|
|
// $this->user = session('user');
|
|
// }
|
|
// }
|
|
// if ($this->user){
|
|
// $this->user_id = $this->user['id'];
|
|
// }
|
|
//// //需要登录接口进行校验
|
|
// if (!$this->match($this->noNeedLogin)){
|
|
// $this->checklogin();
|
|
// }
|
|
// } catch (\Exception $e) {
|
|
// return $this->buildFailed($e->getCode() ?: 400,$e->getMessage());
|
|
// }
|
|
// }
|
|
|
|
/**
|
|
* 关联检测是否包含该请求是否包含该方法
|
|
* @param $arr
|
|
* @return bool
|
|
*/
|
|
public function match($arr = []) {
|
|
$request = Request::instance();
|
|
$arr = is_array($arr) ? $arr : explode(',', $arr);
|
|
if (! $arr) {
|
|
return false;
|
|
}
|
|
$arr = array_map('strtolower', $arr);
|
|
// 是否存在
|
|
if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr)) {
|
|
return true;
|
|
}
|
|
// 没找到匹配
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 用户登录检测
|
|
*/
|
|
public function checkLogin() {
|
|
$login = new LoginService();
|
|
if (!$login->isLogin()){
|
|
throw new FuncException('用户未登录',302);
|
|
}
|
|
}
|
|
|
|
public function buildSuccess(array $data = [], string $msg = '操作成功', int $code = ReturnCode::SUCCESS): Response {
|
|
$return = [
|
|
'code' => $code,
|
|
'msg' => $msg,
|
|
'data' => $data
|
|
];
|
|
if (Env::get('APP_DEBUG') && $this->debug) {
|
|
$return['debug'] = $this->debug;
|
|
}
|
|
|
|
return json($return);
|
|
}
|
|
|
|
public function buildFailed(int $code, string $msg = '操作失败', array $data = []): Response {
|
|
$return = [
|
|
'code' => $code,
|
|
'msg' => $msg,
|
|
'data' => $data
|
|
];
|
|
if (Env::get('APP_DEBUG') && $this->debug) {
|
|
$return['debug'] = $this->debug;
|
|
}
|
|
|
|
return json($return);
|
|
}
|
|
|
|
protected function debug($data): void {
|
|
if ($data) {
|
|
$this->debug[] = $data;
|
|
}
|
|
}
|
|
}
|
|
|