作者 root

gx

... ... @@ -5,10 +5,48 @@ namespace App\Http\Controllers\Aside;
use App\Enums\Common\Code;
use App\Http\Controllers\Controller;
use App\Utils\EncryptUtils;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache;
class BaseController extends Controller
{
protected $param = [];//所有请求参数
protected $token = ''; //token
protected $request = [];//助手函数
protected $allCount = 0;//总条数
protected $p = 1;//当前页
protected $row = 20;//每页条数
protected $header = [];//设置请求头参数
protected $order = 'id';
protected $map = [];//处理后的参数
protected $uid = 0;
/**
* 获取所有参数
*/
public function __construct(Request $request)
{
$this->request = $request;
$this->param = $this->request->all();
$this->token = $this->request->header('token');
$this->get_param();
$this->auth_token();
}
/**
* @name
* @return void
* @author :liyuhang
* @method
*/
public function auth_token(){
$info = Cache::get($this->token);
if(isset($info) && !empty($info)){
$this->uid = $info['id'];
}
}
/**
* 成功返回
* @param array $data
... ... @@ -36,6 +74,88 @@ class BaseController extends Controller
$response = [
'p' => (new EncryptUtils())->openssl_en($response, $k, $i)];
}
return response()->json($response);
return response()->json($response)->header($this->header);
}
/**
* @name 参数过滤
* @return void
* @author :liyuhang
* @method
*/
public function get_param(){
$param = $this->param;
foreach ($param as $k => $v){
if(is_array($v)){
continue;
}
switch ($k){
case "order":
$this->order = $v;
break;
case 'p':
$this->p = $v;
break;
case 'row':
$this->row = $v;
break;
case "create_at":
$this->_btw[0] = $v;
$this->_btw[1] = date('Y-m-d H:i:s',time());
$this->map['create_at'] = ['between', $this->_btw];
break;
case "update_at":
$this->_btw[1] = $v;
$this->map['update_at'] = ['between', $this->_btw];
break;
default:
if (!empty($v)) {
$this->map[$k] = $v;
}
break;
}
}
}
/**
* @name 统一返回参数
* @return void
* @author :liyuhang
* @method
*/
public function response($msg,$code = 200,$data = [],$result_code = null,$type = 'application/json'){
$result_code === null && $result_code = $code;
$result = [
'msg' =>$msg,
'code'=>$result_code,
'data'=>$data
];
$this->header['Content-Type'] = $type;
$this->header['token'] = $this->token;
$response = Response::create(json_encode($result),$code,$this->header);
throw new HttpResponseException($response);
}
/**
* 方法请求输出数据(带分页参数)
* @param $data
* @return
*/
protected function result($lists) {
$data['data'] = $lists;
$data['page'] = $this->setPages();
$this->response('success', 200, $data);
}
/**
* 设置分页返回参数()
*/
protected function setPages() {
$page_count = $this->allCount > $this->row ? ceil($this->allCount / $this->row) : 1;
$this->header['Total-Count'] = $this->allCount; //总条数
$this->header['Page-Count'] = $page_count; //总页数
$this->header['Current-Page'] = $this->p; //当前页数
$this->header['Per-Page'] = $this->row; //每页条数
return $this->header;
}
}
... ...
... ... @@ -73,7 +73,7 @@ class BaseController extends Controller
$response = [
'p' => (new EncryptUtils())->openssl_en($response, $k, $i)];
}
return response()->json($response)->header($this->header);
return response()->json($response,200,$this->header);
}
/**
... ...
... ... @@ -2,16 +2,114 @@
namespace App\Http\Controllers\Bside;
use App\Enums\Common\Code;
use App\Models\ProjectMenu as ProjectMenuModel;
use App\Models\ProjectRole as ProjectRoleModel;
use Illuminate\Support\Facades\Validator;
class ProjectMenuController extends BaseController
{
/**
* @name :菜单列表
* @name :用户组菜单列表(带分页)
* @return void
* @author :liyuhang
* @method
*/
public function lists(){
//根据角色获取菜单列表
$projectMenuModel = new ProjectMenuModel();
$lists = $projectMenuModel->lists($this->param,$this->p,$this->row,$this->order);
$this->result($lists);
}
/**
* @name :添加用户组菜单
* @return void
* @author :liyuhang
* @method
*/
public function add(){
//参数验证
$rules = [
'name'=>'required|max:11',
'rules'=>'required',
];
//验证的提示信息
$message = [
'name.required'=>'名称必须填写',
'name.max' => '名称不大于16字符.',
'rules.required'=>'路由必须填写',
];
$validate = Validator::make($this->param, $rules, $message);
if($validate->fails()){
return $this->response($validate->errors()->first(),Code::USER_PARAMS_ERROE,$this->param);
}
$projectMenuModel = new ProjectMenuModel();
$rs = $projectMenuModel->add($this->param);
if($rs === false){
$this->response('请求失败',Code::USER_ERROR,[]);
}
$this->response('success',Code::SUCCESS);
}
/**
* @name :编辑用户组菜单
* @return void
* @author :liyuhang
* @method
*/
public function edit(){
//参数验证
$rules = [
'id'=>'required',
'name'=>'required|max:11',
'rules'=>'required',
];
//验证的提示信息
$message = [
'id.required'=>'服务器id错误',
'name.required'=>'名称必须填写',
'name.max' => '名称不大于16字符.',
'rules.required'=>'路由必须填写',
];
$validate = Validator::make($this->param, $rules, $message);
if($validate->fails()){
return $this->response($validate->errors()->first(),Code::USER_PARAMS_ERROE,$this->param);
}
$projectMenuModel = new ProjectMenuModel();
$rs = $projectMenuModel->edit($this->param,['id'=>$this->param['id']]);
if($rs === false){
$this->response('请求失败',Code::USER_ERROR);
}
$this->response('success',Code::SUCCESS);
}
/**
* @name :编辑状态
* @return void
* @author :liyuhang
* @method
*/
public function status(){
//参数验证
$rules = [
'id'=>'required',
'status'=>'required',
];
//验证的提示信息
$message = [
'id.required'=>'主键必须填写',
'status.required'=>'状态必须填写',
];
$validate = Validator::make($this->param, $rules, $message);
if($validate->fails()){
return $this->response($validate->errors()->first(),Code::USER_PARAMS_ERROE,$this->param);
}
$projectMenuModel = new ProjectMenuModel();
$rs = $projectMenuModel->edit(['status'=>$this->param['status']],['id'=>$this->param['id']]);
if($rs === false){
$this->response('编辑失败',Code::USER_PARAMS_ERROE);
}
$this->response($this->param['status'] == 0 ? '启用成功' : '禁用成功',Code::SUCCESS);
}
}
... ...
... ... @@ -24,6 +24,7 @@ class UserController extends BaseController
if(empty($lists)){
$this->response('请求失败',Code::USER_ERROR,[]);
}
return response()->json($lists);
$this->result($lists);
}
... ...
... ... @@ -19,27 +19,6 @@ class BaseLogic
}
/**
* @notes: 请简要描述方法功能
* @param array $data
* @return array
*/
public function success(array $data): array
{
return $data;
}
/**
* @notes: 错误抛出
* @param string $code
* @param string $message
* @throws AsideGlobalException
*/
public function fail(string $code = Code::SYSTEM_ERROR, $message = "")
{
throw new AsideGlobalException($code, $message);
}
/**
* @notes: 统一格式化分页返回
* @return array
*/
... ...
... ... @@ -15,7 +15,7 @@ class DingService extends BaseService
{
use RedisTrait;
const LINK = 'https://oapi.dingtalk.com/robot/send?access_token=723c99369cc16806a26fee8b8ab2c5ae37a78ef842e6a3af89fed0b2a6211836';
const LINK = 'https://oapi.dingtalk.com/robot/send111?access_token=723c99369cc16806a26fee8b8ab2c5ae37a78ef842e6a3af89fed0b2a6211836';
const INFO = 'INFO';
const ERROR = 'ERROR';
const WARNNING = 'WARNNING';
... ...
... ... @@ -13,4 +13,5 @@ Route::middleware(['bloginauth'])->group(function () {
Route::group([], function () {
Route::any('/login', [\App\Http\Controllers\Bside\ComController::class, 'login'])->name('login');
Route::any('/user/lists', [\App\Http\Controllers\Bside\UserController::class, 'lists'])->name('user_lists');
Route::any('/project/page_lists', [\App\Http\Controllers\Bside\ProjectController::class, 'page_lists'])->name('page_lists');
});
... ...