作者 赵彬吉

update

@@ -4,25 +4,25 @@ namespace App\Http\Controllers\Aside; @@ -4,25 +4,25 @@ namespace App\Http\Controllers\Aside;
4 4
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
6 use App\Http\Controllers\Controller; 6 use App\Http\Controllers\Controller;
7 -use App\Utils\EncryptUtils;  
8 -use Illuminate\Http\Exceptions\HttpResponseException;  
9 use Illuminate\Http\JsonResponse; 7 use Illuminate\Http\JsonResponse;
10 use Illuminate\Http\Request; 8 use Illuminate\Http\Request;
11 -use Illuminate\Http\Response;  
12 -use Illuminate\Support\Facades\Cache; 9 +use Illuminate\Http\Exceptions\HttpResponseException;
  10 +use Illuminate\Support\Facades\Session;
13 11
14 class BaseController extends Controller 12 class BaseController extends Controller
15 { 13 {
16 protected $param = [];//所有请求参数 14 protected $param = [];//所有请求参数
17 protected $token = ''; //token 15 protected $token = ''; //token
18 protected $request = [];//助手函数 16 protected $request = [];//助手函数
19 - protected $p = 1;//当前页 17 + protected $allCount = 0;//总条数
  18 + protected $page = 1;//当前页
20 protected $row = 20;//每页条数 19 protected $row = 20;//每页条数
21 protected $header = [];//设置请求头参数 20 protected $header = [];//设置请求头参数
22 protected $order = 'id'; 21 protected $order = 'id';
23 protected $map = [];//处理后的参数 22 protected $map = [];//处理后的参数
24 protected $uid = 0; 23 protected $uid = 0;
25 protected $user = [];//当前登录用户详情 24 protected $user = [];//当前登录用户详情
  25 +
26 /** 26 /**
27 * 获取所有参数 27 * 获取所有参数
28 */ 28 */
@@ -30,32 +30,24 @@ class BaseController extends Controller @@ -30,32 +30,24 @@ class BaseController extends Controller
30 { 30 {
31 $this->request = $request; 31 $this->request = $request;
32 $this->param = $this->request->all(); 32 $this->param = $this->request->all();
33 - $this->token = $this->request->header('token');  
34 $this->get_param(); 33 $this->get_param();
35 - $this->auth_token();  
36 } 34 }
37 35
38 /** 36 /**
39 - * @name  
40 - * @return void  
41 - * @author :liyuhang  
42 - * @method 37 + * @return mixed
  38 + * @author zbj
  39 + * @date 2023/4/19
43 */ 40 */
44 - public function auth_token(){  
45 - $info = Cache::get($this->token);  
46 - if(isset($info) && !empty($info)){  
47 - $this->user = $info;  
48 - $this->uid = $info['id'];  
49 - } 41 + public function manage(){
  42 + return Session::get('manage');
50 } 43 }
  44 +
51 /** 45 /**
52 * 成功返回 46 * 成功返回
53 * @param array $data 47 * @param array $data
54 * @param string $code 48 * @param string $code
55 * @param bool $objectData 49 * @param bool $objectData
56 * @return JsonResponse 50 * @return JsonResponse
57 - * @throws \Psr\Container\ContainerExceptionInterface  
58 - * @throws \Psr\Container\NotFoundExceptionInterface  
59 */ 51 */
60 function success(array $data = [], string $code = Code::SUCCESS, bool $objectData = false): JsonResponse 52 function success(array $data = [], string $code = Code::SUCCESS, bool $objectData = false): JsonResponse
61 { 53 {
@@ -68,16 +60,8 @@ class BaseController extends Controller @@ -68,16 +60,8 @@ class BaseController extends Controller
68 'data' => $data, 60 'data' => $data,
69 'msg' => $code->description, 61 'msg' => $code->description,
70 ]; 62 ];
71 - //加密-返回数据  
72 - if (config('app.params_encrypt')) {  
73 - $k = config('app.params_encrypt_key');  
74 - $i = config('app.params_encrypt_iv');  
75 - $response = [  
76 - 'p' => (new EncryptUtils())->openssl_en($response, $k, $i)];  
77 - }  
78 return response()->json($response,200,$this->header); 63 return response()->json($response,200,$this->header);
79 } 64 }
80 -  
81 /** 65 /**
82 * @name 参数过滤 66 * @name 参数过滤
83 * @return void 67 * @return void
@@ -94,20 +78,23 @@ class BaseController extends Controller @@ -94,20 +78,23 @@ class BaseController extends Controller
94 case "order": 78 case "order":
95 $this->order = $v; 79 $this->order = $v;
96 break; 80 break;
97 - case 'p':  
98 - $this->p = $v; 81 + case 'page':
  82 + $this->page = $v;
99 break; 83 break;
100 case 'row': 84 case 'row':
101 $this->row = $v; 85 $this->row = $v;
102 break; 86 break;
103 - case "created_at": 87 + case "name":
  88 + $this->map['name'] = ['like','%'.$v.'%'];
  89 + break;
  90 + case "start_at":
104 $this->_btw[0] = $v; 91 $this->_btw[0] = $v;
105 $this->_btw[1] = date('Y-m-d H:i:s',time()); 92 $this->_btw[1] = date('Y-m-d H:i:s',time());
106 - $this->map['create_at'] = ['between', $this->_btw]; 93 + $this->map['created_at'] = ['between', $this->_btw];
107 break; 94 break;
108 - case "updated_at": 95 + case "end_at":
109 $this->_btw[1] = $v; 96 $this->_btw[1] = $v;
110 - $this->map['update_at'] = ['between', $this->_btw]; 97 + $this->map['updated_at'] = ['between', $this->_btw];
111 break; 98 break;
112 default: 99 default:
113 if (!empty($v)) { 100 if (!empty($v)) {
@@ -116,46 +103,24 @@ class BaseController extends Controller @@ -116,46 +103,24 @@ class BaseController extends Controller
116 break; 103 break;
117 } 104 }
118 } 105 }
119 -  
120 } 106 }
121 /** 107 /**
122 * @name 统一返回参数 108 * @name 统一返回参数
123 - * @return void 109 + * @return JsonResponse
124 * @author :liyuhang 110 * @author :liyuhang
125 * @method 111 * @method
126 */ 112 */
127 - public function response($msg,$code = 200,$data = [],$result_code = null,$type = 'application/json'){  
128 - $result_code === null && $result_code = $code; 113 + public function response($msg = null,string $code = Code::SUCCESS,$data = [],$result_code = 200,$type = 'application/json'): JsonResponse
  114 + {
  115 + $code = Code::fromValue($code);
129 $result = [ 116 $result = [
130 - 'msg' =>$msg,  
131 - 'code'=>$result_code,  
132 - 'data'=>$data 117 + 'msg' => $msg == ' ' ? $code->description : $msg,
  118 + 'code' => $code->value,
  119 + 'data' => $data,
133 ]; 120 ];
134 $this->header['Content-Type'] = $type; 121 $this->header['Content-Type'] = $type;
135 $this->header['token'] = $this->token; 122 $this->header['token'] = $this->token;
136 $response = response($result,$result_code,$this->header);; 123 $response = response($result,$result_code,$this->header);;
137 throw new HttpResponseException($response); 124 throw new HttpResponseException($response);
138 } 125 }
139 -  
140 - /**  
141 - * @name :上传图片  
142 - * @return void  
143 - * @author :liyuhang  
144 - * @method  
145 - */  
146 - public function uploads(){  
147 - $files = $this->request->file('file');  
148 - if(empty($files)){  
149 - return $this->response('没有上传文件',Code::USER_ERROR);  
150 - }  
151 - $url = './uploads/images/';  
152 - $param = $this->request->post();  
153 - if($this->request->hasFile('image') && $files->isValid()){  
154 - $filename = date('ymdHis').rand(10000,99999).$this->request->file('image');  
155 - $this->request->file('image')->move('./uploads/images/',$filename);  
156 - }else{  
157 - return false;  
158 - }  
159 - return $url.$filename;  
160 - }  
161 } 126 }
1 -<?php  
2 -  
3 -namespace App\Http\Controllers\Aside;  
4 -  
5 -use App\Http\Logic\Aside\DemoLogic;  
6 -use App\Http\Requests\Aside\DemoRequest;  
7 -  
8 -class DemoController extends BaseController  
9 -{  
10 - /**  
11 - * Deom控制器  
12 - * @param DemoRequest $request  
13 - * @param DemoLogic $logic  
14 - * @return \Illuminate\Http\JsonResponse  
15 - * @throws \Psr\Container\ContainerExceptionInterface  
16 - * @throws \Psr\Container\NotFoundExceptionInterface  
17 - */  
18 - public function test(DemoRequest $request,DemoLogic $logic)  
19 - {  
20 - $request->validated();  
21 - $data=$logic->testLogic();  
22 - return $this->success($data);  
23 - }  
24 -}  
  1 +<?php
  2 +
  3 +namespace App\Http\Controllers\Aside;
  4 +
  5 +use App\Http\Controllers\Controller;
  6 +use Illuminate\Http\Request;
  7 +
  8 +/**
  9 + * Class IndexController
  10 + * @package App\Http\Controllers\Aside
  11 + * @author zbj
  12 + * @date 2023/4/19
  13 + */
  14 +class IndexController extends Controller
  15 +{
  16 + /**
  17 + * 首页
  18 + * @param Request $request
  19 + * @return \Illuminate\Http\JsonResponse
  20 + */
  21 + public function index(Request $request)
  22 + {
  23 +
  24 + }
  25 +
  26 +}
  1 +<?php
  2 +
  3 +
  4 +namespace App\Http\Controllers\Aside;
  5 +
  6 +use App\Http\Logic\Aside\LoginLogic;
  7 +use App\Rules\Mobile;
  8 +use Illuminate\Http\Request;
  9 +
  10 +/**
  11 + * Class LoginController
  12 + * @package App\Http\Controllers\Aside
  13 + * @author zbj
  14 + * @date 2023/4/19
  15 + */
  16 +class LoginController extends BaseController
  17 +{
  18 +
  19 + function login(Request $request, LoginLogic $logic)
  20 + {
  21 + if ($request->isMethod('POST')) {
  22 + $request->validate([
  23 + 'mobile' => ['required', new Mobile()],
  24 + 'password' => 'required',
  25 + ], [
  26 + 'mobile.required' => '请输入手机号',
  27 + 'password.required' => '请输入密码',
  28 + ]);
  29 +
  30 + $logic->login();
  31 +
  32 + return $this->success();
  33 + }
  34 + return view('admin.login');
  35 + }
  36 +
  37 + public function logout(LoginLogic $logic)
  38 + {
  39 + return $logic->logout();
  40 + }
  41 +}
  1 +<?php
  2 +
  3 +namespace App\Http\Controllers\Aside;
  4 +
  5 +use App\Http\Controllers\Controller;
  6 +use Illuminate\Http\Request;
  7 +
  8 +/**
  9 + * Class Menu
  10 + * @package App\Http\Controllers\Aside
  11 + * @author zbj
  12 + * @date 2023/4/19
  13 + */
  14 +class MenuController extends Controller
  15 +{
  16 + /**
  17 + * 菜单列表
  18 + * @param Request $request
  19 + * @return \Illuminate\Http\JsonResponse
  20 + */
  21 + public function index(Request $request)
  22 + {
  23 + echo 111;
  24 + }
  25 +
  26 +}
@@ -2,35 +2,21 @@ @@ -2,35 +2,21 @@
2 2
3 namespace App\Http\Logic\Aside; 3 namespace App\Http\Logic\Aside;
4 4
5 -use App\Enums\Common\Code;  
6 -use App\Exceptions\AsideGlobalException; 5 +
  6 +use App\Http\Logic\Logic;
7 7
8 /** 8 /**
9 * @notes: 逻辑层基类 控制器调用 统一返回 统一抛出异常 9 * @notes: 逻辑层基类 控制器调用 统一返回 统一抛出异常
10 * Class BaseLogic 10 * Class BaseLogic
11 * @package App\Http\Logic\Aside 11 * @package App\Http\Logic\Aside
12 */ 12 */
13 -class BaseLogic 13 +class BaseLogic extends Logic
14 { 14 {
15 protected $requestAll; 15 protected $requestAll;
16 - public function __construct()  
17 - {  
18 - $this->requestAll=request()->all();  
19 - }  
20 16
21 - /**  
22 - * @notes: 统一格式化分页返回  
23 - * @return array  
24 - */  
25 - function getPageData($pagninate): array 17 + public function __construct()
26 { 18 {
27 - $p = $pagninate->toArray();  
28 - $result['list'] = $p ['data'];  
29 - $result['pager']['total'] = $p ['total'];  
30 - $result['pager']['page'] = $p ['current_page'];  
31 - $result['pager']['pagesize'] = $p ['per_page'];  
32 -  
33 - return $result; 19 + $this->requestAll = request()->all();
34 } 20 }
35 21
36 } 22 }
1 -<?php  
2 -  
3 -namespace App\Http\Logic\Aside;  
4 -  
5 -class DemoLogic extends BaseLogic  
6 -{  
7 - protected $requestAll;  
8 - public function __construct()  
9 - {  
10 - $this->requestAll=request()->all();  
11 - }  
12 - public function testLogic():array  
13 - {  
14 - return $this->success($this->requestAll);  
15 - }  
16 -}  
  1 +<?php
  2 +
  3 +namespace App\Http\Logic\Aside;
  4 +
  5 +use App\Models\Manage;
  6 +use Illuminate\Support\Facades\Hash;
  7 +use Illuminate\Support\Facades\Session;
  8 +
  9 +
  10 +/**
  11 + * Class LoginLogic
  12 + * @package App\Http\Logic\Aside
  13 + * @author zbj
  14 + * @date 2023/4/19
  15 + */
  16 +class LoginLogic extends BaseLogic
  17 +{
  18 + public function __construct()
  19 + {
  20 + parent::__construct();
  21 +
  22 + $this->model = new Manage();
  23 + }
  24 +
  25 +
  26 + public function login()
  27 + {
  28 + $info = $this->model->where('mobile', $this->requestAll['mobile'])->first();
  29 +
  30 + if (!$info){
  31 + $this->fail('登录用户名不存在');
  32 + }
  33 + if (Manage::STATUS_DISABLE == $info->status) {
  34 + $this->fail('帐号已被禁用');
  35 + }
  36 + if (!Hash::check($this->requestAll['password'], $info->password)) {
  37 + $this->fail('登录密码不正确');
  38 + }
  39 + Session::put('manage', $info->toArray());
  40 + return $this->success();
  41 + }
  42 +
  43 + public function logout(){
  44 + Session::forget('manage');
  45 + return redirect(route('admin.login'));
  46 + }
  47 +}
@@ -2,24 +2,20 @@ @@ -2,24 +2,20 @@
2 2
3 namespace App\Http\Logic\Bside; 3 namespace App\Http\Logic\Bside;
4 4
5 -use App\Enums\Common\Code;  
6 use App\Exceptions\BsideGlobalException; 5 use App\Exceptions\BsideGlobalException;
7 -use App\Helper\Arr; 6 +use App\Http\Logic\Logic;
8 use Illuminate\Support\Facades\Cache; 7 use Illuminate\Support\Facades\Cache;
9 8
10 /** 9 /**
11 * @notes: 逻辑层基类 控制器调用 统一返回 统一抛出异常 10 * @notes: 逻辑层基类 控制器调用 统一返回 统一抛出异常
12 */ 11 */
13 -class BaseLogic 12 +class BaseLogic extends Logic
14 { 13 {
15 - protected $model;  
16 14
17 protected $requestAll; 15 protected $requestAll;
18 16
19 protected $user; 17 protected $user;
20 18
21 - protected $is_cache = true; //是否缓存数据  
22 -  
23 public function __construct() 19 public function __construct()
24 { 20 {
25 $this->requestAll = request()->all(); 21 $this->requestAll = request()->all();
@@ -27,27 +23,6 @@ class BaseLogic @@ -27,27 +23,6 @@ class BaseLogic
27 $this->user = Cache::get(request()->header('token')); 23 $this->user = Cache::get(request()->header('token'));
28 } 24 }
29 25
30 - /**  
31 - * @notes: 请简要描述方法功能  
32 - * @param array $data  
33 - * @return array  
34 - */  
35 - public function success(array $data = [])  
36 - {  
37 - return $data;  
38 - }  
39 -  
40 - /**  
41 - * @notes: 错误抛出  
42 - * @param string $code  
43 - * @param string $message  
44 - * @throws BsideGlobalException  
45 - */  
46 - public function fail(string $message = "", string $code = Code::SYSTEM_ERROR)  
47 - {  
48 - throw new BsideGlobalException($code, $message);  
49 - }  
50 -  
51 26
52 /** 27 /**
53 * 列表 28 * 列表
@@ -62,41 +37,7 @@ class BaseLogic @@ -62,41 +37,7 @@ class BaseLogic
62 public function getList(array $map = [], array $sort = ['id' => 'desc'], array $columns = ['*'], int $limit = 20) 37 public function getList(array $map = [], array $sort = ['id' => 'desc'], array $columns = ['*'], int $limit = 20)
63 { 38 {
64 $map[] = ['project_id' => $this->user['project_id']]; 39 $map[] = ['project_id' => $this->user['project_id']];
65 - // 闭包查询条件格式化  
66 - $query = $this->formatQuery($map);  
67 -  
68 - // 排序(支持多重排序)  
69 - $query = $query->when($sort, function ($query, $sort) {  
70 - foreach ($sort as $k=>$v) {  
71 - $query->orderBy($k, $v);  
72 - }  
73 - });  
74 -  
75 - // 数据分页设置  
76 - if ($limit) {  
77 - $result = $query->select($columns)->paginate($limit);  
78 - }else{  
79 - $result = $query->select($columns)->get();  
80 - }  
81 -  
82 - return $this->success($result ? $result->toArray() : []);  
83 - }  
84 -  
85 -  
86 - /**  
87 - * 详情  
88 - * @param $id  
89 - * @return array  
90 - * @author zbj  
91 - * @date 2023/4/13  
92 - */  
93 - public function getInfo($id)  
94 - {  
95 - $info = $this->getCacheInfo($id);  
96 - if(!$info){  
97 - $this->fail('数据不存在或者已经删除');  
98 - }  
99 - return $this->success($info->toArray()); 40 + return parent::getList($map, $sort, $columns, $limit);
100 } 41 }
101 42
102 /** 43 /**
@@ -105,19 +46,10 @@ class BaseLogic @@ -105,19 +46,10 @@ class BaseLogic
105 * @author zbj 46 * @author zbj
106 * @date 2023/4/15 47 * @date 2023/4/15
107 */ 48 */
108 - public function getCacheInfo($id){  
109 - if($this->is_cache){  
110 - $info = Cache::get($this->getInfoCacheKey($id));  
111 - if (!$info) {  
112 - $info = $this->model->find($id);  
113 - if($info){  
114 - Cache::put($this->getInfoCacheKey($id), $info);  
115 - }  
116 - }  
117 - }else{  
118 - $info = $this->model->find($id);  
119 - }  
120 - if($info && $info['project_id'] != $this->user['project_id']) { 49 + public function getCacheInfo($id)
  50 + {
  51 + $info = parent::getCacheInfo($id);
  52 + if ($info && $info['project_id'] != $this->user['project_id']) {
121 $info = null; 53 $info = null;
122 } 54 }
123 return $info; 55 return $info;
@@ -131,177 +63,23 @@ class BaseLogic @@ -131,177 +63,23 @@ class BaseLogic
131 * @author zbj 63 * @author zbj
132 * @date 2023/4/13 64 * @date 2023/4/13
133 */ 65 */
134 - public function save($param){  
135 - if(!empty($param['id'])){  
136 - $this->model = $this->getCacheInfo($param['id']);  
137 - if(!$this->model){  
138 - $this->fail('数据不存在或者已经删除');  
139 - }  
140 - } 66 + public function save($param)
  67 + {
141 $param['project_id'] = $this->user['project_id']; 68 $param['project_id'] = $this->user['project_id'];
142 - foreach ($param as $name => $value){  
143 - $this->model[$name] = $value;  
144 - }  
145 -  
146 - $res = $this->model->save();  
147 -  
148 - if($res){  
149 - //清缓存  
150 - if($this->is_cache && !empty($param['id'])){  
151 - Cache::forget($this->getInfoCacheKey($param['id']));  
152 - }  
153 - return $this->success(['id' => $this->model->id]); //返回保存的数据id  
154 - }else{  
155 - $this->fail('保存失败');  
156 - } 69 + return parent::save($param);
157 } 70 }
158 71
159 /** 72 /**
160 * 批量删除 73 * 批量删除
161 * @param $ids 74 * @param $ids
  75 + * @param array $map
162 * @return array 76 * @return array
163 - * @throws BsideGlobalException  
164 - * @author zbj  
165 - * @date 2023/4/13  
166 - */  
167 - public function delete($ids){  
168 - $ids = array_filter(Arr::splitFilterToArray($ids), 'intval');  
169 - if(!$ids){  
170 - $this->fail('ID不能为空');  
171 - }  
172 - $map[] = ['id', 'in', $ids];  
173 - $map[] = ['project_id' => $this->user['project_id']];  
174 -  
175 - $res = $this->formatQuery($map)->delete();  
176 - if($res){  
177 -  
178 - if($this->is_cache){  
179 - foreach ($ids as $id){  
180 - Cache::forget($this->getInfoCacheKey($id));  
181 - }  
182 - }  
183 - return $this->success();  
184 - }else{  
185 - $this->fail('删除失败');  
186 - }  
187 - }  
188 -  
189 - /**  
190 - * @param $id  
191 - * @return string  
192 - * @author zbj  
193 - * @date 2023/4/13  
194 - */  
195 - public function getInfoCacheKey($id){  
196 - return $this->model->getTable() . '_info_' . $id;  
197 - }  
198 -  
199 - /**  
200 - * 格式化查询条件  
201 - * @param $map  
202 - * @param $query  
203 - * @return mixed  
204 * @author zbj 77 * @author zbj
205 * @date 2023/4/13 78 * @date 2023/4/13
206 */ 79 */
207 - public function formatQuery($map, $query = '') 80 + public function delete($ids, $map = [])
208 { 81 {
209 - $model = $query ?: $this->model;  
210 - $query = $model->where(function ($query) use ($map) {  
211 - foreach ($map as $v) {  
212 - if ($v instanceof \Closure) {  
213 - $query = $query->where($v);  
214 - continue;  
215 - }  
216 - // 判断是否是键值对类型  
217 - if (key($v) !== 0) {  
218 - $key = key($v);  
219 - $val = $v[$key];  
220 - $v = [$key, is_array($val) ? 'in' : '=', $val];  
221 - }  
222 - switch ($v[1]) {  
223 - case 'like':  
224 - // like查询 ['name|title', 'like', '%a%']  
225 - if (strpos($v[0], '|') !== false) {  
226 - $query->where(function ($query) use ($v) {  
227 - $item = explode('|', $v[0]);  
228 - foreach ($item as $vo) {  
229 - $query->orWhere($vo, $v[1], $v[2]);  
230 - }  
231 - });  
232 - } else {  
233 - $query->where($v[0], $v[1], $v[2]);  
234 - }  
235 - break;  
236 - case 'in':  
237 - // in查询 ['id', 'in', [1,2,3]]  
238 - if (!is_array($v[2])) {  
239 - $v[2] = explode(',', $v[2]);  
240 - }  
241 - $query->whereIn($v[0], $v[2]);  
242 - break;  
243 - case 'not in':  
244 - // not in查询 ['id', 'not in', [1,2,3]]  
245 - if (!is_array($v[2])) {  
246 - $v[2] = explode(',', $v[2]);  
247 - }  
248 - $query->whereNotIn($v[0], $v[2]);  
249 - break;  
250 - case 'between':  
251 - // between查询 ['created_at', 'between', ['xxx', 'xxx]]  
252 - if (!is_array($v[2])) {  
253 - $v[2] = explode(',', $v[2]);  
254 - }  
255 - $query->whereBetween($v[0], $v[2]);  
256 - break;  
257 - case 'not between':  
258 - // not between查询 ['created_at', 'not between', ['xxx', 'xxx]]  
259 - if (!is_array($v[2])) {  
260 - $v[2] = explode(',', $v[2]);  
261 - }  
262 - $query->whereNotBetween($v[0], $v[2]);  
263 - break;  
264 - case 'null':  
265 - // null查询 ['deleted_at', 'null']  
266 - $query->whereNull($v[0]);  
267 - break;  
268 - case "not null":  
269 - // not null查询 ['deleted_at', 'not null']  
270 - $query->whereNotNull($v[0]);  
271 - break;  
272 - case "or":  
273 - // or查询 [[['status'=>1],['status'=>2]], 'or'];  
274 - //格式:or (status=1 and status=2)  
275 - $where = $v[0];  
276 - $query->orWhere(function ($query) use ($where) {  
277 - // 递归解析查询条件  
278 - $this->formatQuery($where, $query);  
279 - });  
280 - break;  
281 - case 'xor':  
282 - // xor查询 [[['status'=>1],['status'=>2]], 'xor'];  
283 - // 格式:and (status=1 or status=2)  
284 - $where = $v[0];  
285 - $query->where(function ($query) use ($where) {  
286 - foreach ($where as $w) {  
287 - $query->orWhere(function ($query) use ($w) {  
288 - // 递归解析查询条件  
289 - $this->formatQuery([$w], $query);  
290 - });  
291 - }  
292 - });  
293 - break;  
294 - default:  
295 - // 常规查询  
296 - if (count($v) == 2) {  
297 - $query->where($v[0], '=', $v[1]);  
298 - } else {  
299 - $query->where($v[0], $v[1], $v[2]);  
300 - }  
301 - break;  
302 - }  
303 - }  
304 - });  
305 - return $query; 82 + $map[] = ['project_id' => $this->user['project_id']];
  83 + return parent::delete($ids, $map);
306 } 84 }
307 } 85 }
@@ -2,7 +2,6 @@ @@ -2,7 +2,6 @@
2 2
3 namespace App\Http\Logic\Bside\Product; 3 namespace App\Http\Logic\Bside\Product;
4 4
5 -use App\Helper\Arr;  
6 use App\Http\Logic\Bside\BaseLogic; 5 use App\Http\Logic\Bside\BaseLogic;
7 use App\Models\Product\Attr; 6 use App\Models\Product\Attr;
8 use App\Models\Product\AttrValue; 7 use App\Models\Product\AttrValue;
  1 +<?php
  2 +
  3 +namespace App\Http\Logic;
  4 +
  5 +use App\Enums\Common\Code;
  6 +use App\Enums\Common\Common;
  7 +use App\Exceptions\AsideGlobalException;
  8 +use App\Exceptions\BsideGlobalException;
  9 +use App\Helper\Arr;
  10 +use Illuminate\Support\Facades\Cache;
  11 +
  12 +/**
  13 + * @notes: 逻辑层基类 控制器调用 统一返回 统一抛出异常
  14 + */
  15 +class Logic
  16 +{
  17 + protected $model;
  18 +
  19 + protected $is_cache = true; //是否缓存数据
  20 +
  21 + /**
  22 + * @notes: 请简要描述方法功能
  23 + * @param array $data
  24 + * @return array
  25 + */
  26 + public function success(array $data = [])
  27 + {
  28 + return $data;
  29 + }
  30 +
  31 + /**
  32 + * @notes: 错误抛出
  33 + * @param string $code
  34 + * @param string $message
  35 + * @throws AsideGlobalException|BsideGlobalException
  36 + */
  37 + public function fail(string $message = "", string $code = Code::SYSTEM_ERROR)
  38 + {
  39 + if((request()->path()[0]) == Common::B){
  40 + throw new BsideGlobalException($code, $message);
  41 + }
  42 + throw new AsideGlobalException($code, $message);
  43 + }
  44 +
  45 + /**
  46 + * 列表
  47 + * @param array $map
  48 + * @param array $sort
  49 + * @param array $columns
  50 + * @param int $limit
  51 + * @return array
  52 + * @author zbj
  53 + * @date 2023/4/13
  54 + */
  55 + public function getList(array $map = [], array $sort = ['id' => 'desc'], array $columns = ['*'], int $limit = 20)
  56 + {
  57 + // 闭包查询条件格式化
  58 + $query = $this->formatQuery($map);
  59 +
  60 + // 排序(支持多重排序)
  61 + $query = $query->when($sort, function ($query, $sort) {
  62 + foreach ($sort as $k=>$v) {
  63 + $query->orderBy($k, $v);
  64 + }
  65 + });
  66 +
  67 + // 数据分页设置
  68 + if ($limit) {
  69 + $result = $query->select($columns)->paginate($limit);
  70 + }else{
  71 + $result = $query->select($columns)->get();
  72 + }
  73 +
  74 + return $this->success($result ? $result->toArray() : []);
  75 + }
  76 +
  77 +
  78 + /**
  79 + * 详情
  80 + * @param $id
  81 + * @return array
  82 + * @throws AsideGlobalException|BsideGlobalException
  83 + * @author zbj
  84 + * @date 2023/4/13
  85 + */
  86 + public function getInfo($id)
  87 + {
  88 + $info = $this->getCacheInfo($id);
  89 + if(!$info){
  90 + $this->fail('数据不存在或者已经删除');
  91 + }
  92 + return $this->success($info->toArray());
  93 + }
  94 +
  95 + /**
  96 + * @param $id
  97 + * @return mixed
  98 + * @author zbj
  99 + * @date 2023/4/15
  100 + */
  101 + public function getCacheInfo($id){
  102 + if($this->is_cache){
  103 + $info = Cache::get($this->getInfoCacheKey($id));
  104 + if (!$info) {
  105 + $info = $this->model->find($id);
  106 + if($info){
  107 + Cache::put($this->getInfoCacheKey($id), $info);
  108 + }
  109 + }
  110 + }else{
  111 + $info = $this->model->find($id);
  112 + }
  113 + return $info;
  114 + }
  115 +
  116 + /**
  117 + * 保存
  118 + * @param $param
  119 + * @return array
  120 + * @throws BsideGlobalException|AsideGlobalException
  121 + * @author zbj
  122 + * @date 2023/4/13
  123 + */
  124 + public function save($param){
  125 + if(!empty($param['id'])){
  126 + $this->model = $this->getCacheInfo($param['id']);
  127 + if(!$this->model){
  128 + $this->fail('数据不存在或者已经删除');
  129 + }
  130 + }
  131 + foreach ($param as $name => $value){
  132 + $this->model[$name] = $value;
  133 + }
  134 +
  135 + $res = $this->model->save();
  136 +
  137 + if($res){
  138 + //清缓存
  139 + if($this->is_cache && !empty($param['id'])){
  140 + Cache::forget($this->getInfoCacheKey($param['id']));
  141 + }
  142 + return $this->success(['id' => $this->model->id]); //返回保存的数据id
  143 + }else{
  144 + $this->fail('保存失败');
  145 + }
  146 + }
  147 +
  148 + /**
  149 + * 批量删除
  150 + * @param $ids
  151 + * @param array $map
  152 + * @return array
  153 + * @throws AsideGlobalException|BsideGlobalException
  154 + * @author zbj
  155 + * @date 2023/4/13
  156 + */
  157 + public function delete($ids, $map = []){
  158 + $ids = array_filter(Arr::splitFilterToArray($ids), 'intval');
  159 + if(!$ids){
  160 + $this->fail('ID不能为空');
  161 + }
  162 + $map[] = ['id', 'in', $ids];
  163 +
  164 + $res = $this->formatQuery($map)->delete();
  165 + if($res){
  166 +
  167 + if($this->is_cache){
  168 + foreach ($ids as $id){
  169 + Cache::forget($this->getInfoCacheKey($id));
  170 + }
  171 + }
  172 + return $this->success();
  173 + }else{
  174 + $this->fail('删除失败');
  175 + }
  176 + }
  177 +
  178 + /**
  179 + * @param $id
  180 + * @return string
  181 + * @author zbj
  182 + * @date 2023/4/13
  183 + */
  184 + public function getInfoCacheKey($id){
  185 + return $this->model->getTable() . '_info_' . $id;
  186 + }
  187 +
  188 + /**
  189 + * 格式化查询条件
  190 + * @param $map
  191 + * @param $query
  192 + * @return mixed
  193 + * @author zbj
  194 + * @date 2023/4/13
  195 + */
  196 + public function formatQuery($map, $query = '')
  197 + {
  198 + $model = $query ?: $this->model;
  199 + $query = $model->where(function ($query) use ($map) {
  200 + foreach ($map as $v) {
  201 + if ($v instanceof \Closure) {
  202 + $query = $query->where($v);
  203 + continue;
  204 + }
  205 + // 判断是否是键值对类型
  206 + if (key($v) !== 0) {
  207 + $key = key($v);
  208 + $val = $v[$key];
  209 + $v = [$key, is_array($val) ? 'in' : '=', $val];
  210 + }
  211 + switch ($v[1]) {
  212 + case 'like':
  213 + // like查询 ['name|title', 'like', '%a%']
  214 + if (strpos($v[0], '|') !== false) {
  215 + $query->where(function ($query) use ($v) {
  216 + $item = explode('|', $v[0]);
  217 + foreach ($item as $vo) {
  218 + $query->orWhere($vo, $v[1], $v[2]);
  219 + }
  220 + });
  221 + } else {
  222 + $query->where($v[0], $v[1], $v[2]);
  223 + }
  224 + break;
  225 + case 'in':
  226 + // in查询 ['id', 'in', [1,2,3]]
  227 + if (!is_array($v[2])) {
  228 + $v[2] = explode(',', $v[2]);
  229 + }
  230 + $query->whereIn($v[0], $v[2]);
  231 + break;
  232 + case 'not in':
  233 + // not in查询 ['id', 'not in', [1,2,3]]
  234 + if (!is_array($v[2])) {
  235 + $v[2] = explode(',', $v[2]);
  236 + }
  237 + $query->whereNotIn($v[0], $v[2]);
  238 + break;
  239 + case 'between':
  240 + // between查询 ['created_at', 'between', ['xxx', 'xxx]]
  241 + if (!is_array($v[2])) {
  242 + $v[2] = explode(',', $v[2]);
  243 + }
  244 + $query->whereBetween($v[0], $v[2]);
  245 + break;
  246 + case 'not between':
  247 + // not between查询 ['created_at', 'not between', ['xxx', 'xxx]]
  248 + if (!is_array($v[2])) {
  249 + $v[2] = explode(',', $v[2]);
  250 + }
  251 + $query->whereNotBetween($v[0], $v[2]);
  252 + break;
  253 + case 'null':
  254 + // null查询 ['deleted_at', 'null']
  255 + $query->whereNull($v[0]);
  256 + break;
  257 + case "not null":
  258 + // not null查询 ['deleted_at', 'not null']
  259 + $query->whereNotNull($v[0]);
  260 + break;
  261 + case "or":
  262 + // or查询 [[['status'=>1],['status'=>2]], 'or'];
  263 + //格式:or (status=1 and status=2)
  264 + $where = $v[0];
  265 + $query->orWhere(function ($query) use ($where) {
  266 + // 递归解析查询条件
  267 + $this->formatQuery($where, $query);
  268 + });
  269 + break;
  270 + case 'xor':
  271 + // xor查询 [[['status'=>1],['status'=>2]], 'xor'];
  272 + // 格式:and (status=1 or status=2)
  273 + $where = $v[0];
  274 + $query->where(function ($query) use ($where) {
  275 + foreach ($where as $w) {
  276 + $query->orWhere(function ($query) use ($w) {
  277 + // 递归解析查询条件
  278 + $this->formatQuery([$w], $query);
  279 + });
  280 + }
  281 + });
  282 + break;
  283 + default:
  284 + // 常规查询
  285 + if (count($v) == 2) {
  286 + $query->where($v[0], '=', $v[1]);
  287 + } else {
  288 + $query->where($v[0], $v[1], $v[2]);
  289 + }
  290 + break;
  291 + }
  292 + }
  293 + });
  294 + return $query;
  295 + }
  296 +}
@@ -2,8 +2,10 @@ @@ -2,8 +2,10 @@
2 2
3 namespace App\Http\Middleware\Aside; 3 namespace App\Http\Middleware\Aside;
4 4
  5 +use App\Enums\Common\Code;
5 use Closure; 6 use Closure;
6 use Illuminate\Http\Request; 7 use Illuminate\Http\Request;
  8 +use Illuminate\Support\Facades\Session;
7 9
8 class LoginAuthMiddleware 10 class LoginAuthMiddleware
9 { 11 {
@@ -16,6 +18,16 @@ class LoginAuthMiddleware @@ -16,6 +18,16 @@ class LoginAuthMiddleware
16 */ 18 */
17 public function handle(Request $request, Closure $next) 19 public function handle(Request $request, Closure $next)
18 { 20 {
  21 + $manage = Session::get('manage');
  22 +
  23 + if (!$manage) {
  24 + if($request->ajax()){
  25 + return response(['status'=> Code::USER_ERROR,'msg'=>'当前用户未登录']);
  26 + }else{
  27 + return redirect(route('admin.login'));
  28 + }
  29 + }
  30 +
19 return $next($request); 31 return $next($request);
20 } 32 }
21 } 33 }
1 -<?php  
2 -  
3 -namespace App\Http\Requests\Aside;  
4 -  
5 -use App\Enums\Common\Demo;  
6 -use BenSampo\Enum\Rules\EnumValue;  
7 -use Illuminate\Foundation\Http\FormRequest;  
8 -  
9 -class DemoRequest extends FormRequest  
10 -{  
11 - /**  
12 - * Determine if the user is authorized to make this request.  
13 - *  
14 - * @return bool  
15 - */  
16 - public function authorize()  
17 - {  
18 - return true;  
19 - }  
20 -  
21 - /**  
22 - * Get the validation rules that apply to the request.  
23 - *  
24 - * @return array  
25 - */  
26 - public function rules()  
27 - {  
28 - return [  
29 - 'name'=>['required'],  
30 - 'status'=>['required','integer',new EnumValue(Demo::class)]  
31 - ];  
32 - }  
33 -}  
@@ -2,11 +2,13 @@ @@ -2,11 +2,13 @@
2 2
3 namespace App\Models; 3 namespace App\Models;
4 4
5 -class Manager extends Base 5 +class Manage extends Base
6 { 6 {
7 //设置关联表名 7 //设置关联表名
8 - protected $table = 'gl_manager';  
9 - //自动维护create_at创建时间 updated_at修改时间  
10 - public $timestamps = true; 8 + protected $table = 'gl_manage';
11 9
  10 + protected $hidden = ['password'];
  11 +
  12 + const STATUS_ACTIVE = 0;
  13 + const STATUS_DISABLE = 1;
12 } 14 }
  1 +<?php
  2 +
  3 +namespace App\Rules;
  4 +
  5 +use Illuminate\Contracts\Validation\Rule;
  6 +
  7 +/**
  8 + * 验证手机号
  9 + * Class Mobile
  10 + * @package App\Rules
  11 + * @author zbj
  12 + * @date 2023/4/19
  13 + */
  14 +class Mobile implements Rule
  15 +{
  16 +
  17 + /**
  18 + * Determine if the validation rule passes.
  19 + *
  20 + * @param string $attribute
  21 + * @param mixed $value
  22 + * @return bool
  23 + */
  24 + public function passes($attribute, $value)
  25 + {
  26 + $cardReg = '/^1(3|4|5|7|8)\d{9}$/';
  27 + return preg_match($cardReg, $value);
  28 + }
  29 +
  30 + /**
  31 + * Get the validation error message.
  32 + *
  33 + * @return string
  34 + */
  35 + public function message()
  36 + {
  37 + return '手机号码格式不正确';
  38 + }
  39 +}
  1 +<form method="post" action="">
  2 + @csrf
  3 + <input type="text" name="mobile" value="15680871314">
  4 + <input type="text" name="password" value="123456">
  5 + <input type="submit">
  6 +</form>
@@ -5,12 +5,23 @@ @@ -5,12 +5,23 @@
5 use \Illuminate\Support\Facades\Route; 5 use \Illuminate\Support\Facades\Route;
6 use \App\Http\Controllers\Aside; 6 use \App\Http\Controllers\Aside;
7 //必须登录验证的路由组 7 //必须登录验证的路由组
8 -Route::middleware(['aloginauth'])->group(function ($route) { 8 +Route::middleware(['web'])->group(function (){ //admin用渲染默认要加上web的中间件
  9 + Route::middleware(['aloginauth'])->group(function () {
  10 + Route::get('/', [Aside\IndexController::class, 'index'])->name('admin.home');
  11 + Route::get('/logout', [Aside\LoginController::class, 'logout'])->name('admin.logout');
9 12
10 -}); 13 + //菜单
  14 + Route::prefix('menu')->group(function () {
  15 + Route::get('/', [Aside\MenuController::class, 'index'])->name('admin.menu');
  16 + Route::get('/info', [Aside\MenuController::class, 'info'])->name('admin.menu_info');
  17 + Route::post('/save', [Aside\MenuController::class, 'save'])->name('admin.menu_save');
  18 + Route::any('/delete', [Aside\MenuController::class, 'delete'])->name('admin.menu_delete');
  19 + });
  20 + });
11 21
12 //无需登录验证的路由组 22 //无需登录验证的路由组
13 -Route::group([], function ($route) {  
14 - //demo  
15 - $route->post('/demo', [Aside\DemoController::class, 'test']); 23 + Route::group([], function () {
  24 + Route::any('/login', [Aside\LoginController::class, 'login'])->name('admin.login');
  25 + });
16 }); 26 });
  27 +