作者 liyuhang

gx

@@ -16,7 +16,6 @@ class BaseController extends Controller @@ -16,7 +16,6 @@ class BaseController extends Controller
16 protected $param = [];//所有请求参数 16 protected $param = [];//所有请求参数
17 protected $token = ''; //token 17 protected $token = ''; //token
18 protected $request = [];//助手函数 18 protected $request = [];//助手函数
19 - protected $allCount = 0;//总条数  
20 protected $p = 1;//当前页 19 protected $p = 1;//当前页
21 protected $row = 20;//每页条数 20 protected $row = 20;//每页条数
22 protected $header = [];//设置请求头参数 21 protected $header = [];//设置请求头参数
@@ -137,29 +136,6 @@ class BaseController extends Controller @@ -137,29 +136,6 @@ class BaseController extends Controller
137 $response = response($result,$result_code,$this->header);; 136 $response = response($result,$result_code,$this->header);;
138 throw new HttpResponseException($response); 137 throw new HttpResponseException($response);
139 } 138 }
140 - /**  
141 - * 方法请求输出数据(带分页参数)  
142 - * @param $data  
143 - * @return  
144 - */  
145 - protected function result($lists) {  
146 - $data['data'] = $lists;  
147 - $data['page'] = $this->setPages();  
148 - $this->response('success', 200, $data);  
149 -  
150 - }  
151 -  
152 - /**  
153 - * 设置分页返回参数()  
154 - */  
155 - protected function setPages() {  
156 - $page_count = $this->allCount > $this->row ? ceil($this->allCount / $this->row) : 1;  
157 - $this->header['Total-Count'] = $this->allCount; //总条数  
158 - $this->header['Page-Count'] = $page_count; //总页数  
159 - $this->header['Current-Page'] = $this->p; //当前页数  
160 - $this->header['Per-Page'] = $this->row; //每页条数  
161 - return $this->header;  
162 - }  
163 139
164 /** 140 /**
165 * @name :上传图片 141 * @name :上传图片
  1 +<?php
  2 +
  3 +namespace App\Http\Controllers\Bside;
  4 +
  5 +/**
  6 + * @name:用户组相关
  7 + */
  8 +class ProjectGroupController extends BaseController
  9 +{
  10 + /**
  11 + * @name :用户组列表
  12 + * @return json
  13 + * @author :liyuhang
  14 + * @method
  15 + */
  16 + public function lists()
  17 + {
  18 +
  19 + }
  20 +}
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 2
3 namespace App\Http; 3 namespace App\Http;
4 4
  5 +use App\Http\Middleware\AccessToken;
5 use App\Http\Middleware\Aside\BlackListMiddleware as AsideBlackListMiddleware; 6 use App\Http\Middleware\Aside\BlackListMiddleware as AsideBlackListMiddleware;
6 use App\Http\Middleware\Aside\ParamMiddleware as AsideParamMiddleware; 7 use App\Http\Middleware\Aside\ParamMiddleware as AsideParamMiddleware;
7 use App\Http\Middleware\Bside\BlackListMiddleware as BsideBlackListMiddleware; 8 use App\Http\Middleware\Bside\BlackListMiddleware as BsideBlackListMiddleware;
@@ -91,6 +92,7 @@ class Kernel extends HttpKernel @@ -91,6 +92,7 @@ class Kernel extends HttpKernel
91 //A端登录验证中间件 92 //A端登录验证中间件
92 'aloginauth'=>AsideLoginAuthMiddleware::class, 93 'aloginauth'=>AsideLoginAuthMiddleware::class,
93 //B端登录验证中间件 94 //B端登录验证中间件
94 - 'bloginauth'=>BsideLoginAuthMiddleware::class 95 + 'bloginauth'=>BsideLoginAuthMiddleware::class,
  96 + 'accesstoken'=>AccessToken::class,
95 ]; 97 ];
96 } 98 }
  1 +<?php
  2 +
  3 +namespace App\Http\Middleware;
  4 +
  5 +use App\Enums\Common\Code;
  6 +use App\Models\ProjectMenu;
  7 +use App\Models\ProjectRole as ProjectRoleModel;
  8 +
  9 +class AccessToken
  10 +{
  11 + /**
  12 + * Handle an incoming request.
  13 + *
  14 + * @param \Illuminate\Http\Request $request
  15 + * @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
  16 + * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
  17 + */
  18 + public function handle(Request $request, Closure $next)
  19 + {
  20 + session_start();
  21 + // 指定允许其他域名访问
  22 + $http_origin = "*";
  23 + if(isset($_SERVER['HTTP_ORIGIN'])){
  24 + $http_origin = $_SERVER['HTTP_ORIGIN'];
  25 + }
  26 + header("Access-Control-Allow-Origin:".$http_origin);
  27 + header('Access-Control-Allow-Methods:POST,GET'); //支持的http 动作
  28 + header('Access-Control-Allow-Credentials: true');
  29 + header('Access-Control-Max-Age: 1000');
  30 + header('Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept, Authorization, token'); //响应头 请按照自己需求添加。
  31 + if (strtolower($_SERVER['REQUEST_METHOD']) == 'options') {
  32 + exit;
  33 + }
  34 + return $next($request);
  35 + }
  36 +
  37 +}
@@ -8,28 +8,15 @@ use Illuminate\Support\Facades\DB; @@ -8,28 +8,15 @@ use Illuminate\Support\Facades\DB;
8 class Base extends Model 8 class Base extends Model
9 { 9 {
10 protected $table = ''; 10 protected $table = '';
11 - public $allCount = 0;  
12 //自动维护create_at创建时间 updated_at修改时间 11 //自动维护create_at创建时间 updated_at修改时间
13 public $timestamps = true; 12 public $timestamps = true;
14 - //统一设置 13 + //统一设置返回时间格式
15 protected $casts = [ 14 protected $casts = [
16 'created_at' => 'datetime:Y-m-d H:i:s', 15 'created_at' => 'datetime:Y-m-d H:i:s',
17 'updated_at' => 'datetime:Y-m-d H:i:s', 16 'updated_at' => 'datetime:Y-m-d H:i:s',
18 ]; 17 ];
19 18
20 /** 19 /**
21 - * 日期序列化  
22 - * @param \DateTimeInterface $date  
23 - * @return string  
24 - * @author zbj  
25 - * @date 2023/4/13  
26 - */  
27 - protected function serializeDate(\DateTimeInterface $date): string  
28 - {  
29 - return $date->format('Y-m-d H:i:s');  
30 - }  
31 -  
32 - /**  
33 * @name 列表数据 20 * @name 列表数据
34 * @return void 21 * @return void
35 * @author :liyuhang 22 * @author :liyuhang
@@ -37,12 +24,11 @@ class Base extends Model @@ -37,12 +24,11 @@ class Base extends Model
37 */ 24 */
38 public function lists($map, $p, $row, $order = 'id', $fields = ['*']){ 25 public function lists($map, $p, $row, $order = 'id', $fields = ['*']){
39 //TODO::where(['id'=>'','name'=>'']) 26 //TODO::where(['id'=>'','name'=>''])
40 - $lists = DB::table($this->table)->select($fields)->where($map)->orderBy($order)->paginate($row, ['*'], 'page', $p); 27 + $lists = $this->select($fields)->wheres($map)->orderBy($order)->paginate($row, ['*'], 'page', $p);
41 if (empty($lists)) { 28 if (empty($lists)) {
42 return false; 29 return false;
43 } 30 }
44 $lists = $lists->toArray(); 31 $lists = $lists->toArray();
45 - $this->allCount = $this->where($map)->count();  
46 return $lists; 32 return $lists;
47 } 33 }
48 34
@@ -57,7 +43,7 @@ class Base extends Model @@ -57,7 +43,7 @@ class Base extends Model
57 * @method 43 * @method
58 */ 44 */
59 public function list($map,$order = 'id',$fields = ['*']){ 45 public function list($map,$order = 'id',$fields = ['*']){
60 - $lists = $this->select($fields)->where($map)->orderBy($order)->get(); 46 + $lists = $this->select($fields)->wheres($map)->orderBy($order)->get();
61 if (empty($lists)) { 47 if (empty($lists)) {
62 return false; 48 return false;
63 } 49 }
@@ -73,7 +59,7 @@ class Base extends Model @@ -73,7 +59,7 @@ class Base extends Model
73 */ 59 */
74 public function read($condition,$files = ['*']) 60 public function read($condition,$files = ['*'])
75 { 61 {
76 - $info = $this->select($files)->where($condition)->first(); 62 + $info = $this->select($files)->wheres($condition)->first();
77 if (empty($info)) { 63 if (empty($info)) {
78 return false; 64 return false;
79 } 65 }
@@ -102,7 +88,7 @@ class Base extends Model @@ -102,7 +88,7 @@ class Base extends Model
102 */ 88 */
103 public function edit($data,$condition){ 89 public function edit($data,$condition){
104 $data['updated_at'] = date('Y-m-d H:i:s'); 90 $data['updated_at'] = date('Y-m-d H:i:s');
105 - return $this->where($condition)->update($data); 91 + return $this->wheres($condition)->update($data);
106 } 92 }
107 93
108 /** 94 /**
@@ -112,6 +98,102 @@ class Base extends Model @@ -112,6 +98,102 @@ class Base extends Model
112 * @method 98 * @method
113 */ 99 */
114 public function del($condition){ 100 public function del($condition){
115 - return $this->where($condition)->delete(); 101 + return $this->wheres($condition)->delete();
  102 + }
  103 +
  104 +
  105 + public function wheres($map,$val = ''){
  106 + $query = $this;
  107 + $query->where(function ($query) use ($map,$val){
  108 + //拼接数据
  109 + if(is_array($map)){
  110 + foreach ($map as $v){
  111 + switch ($v){
  112 + case 'like':
  113 + // like查询 ['name|title', 'like', '%a%']
  114 + if (strpos($v[0], '|') !== false) {
  115 + $query->where(function ($query) use ($v) {
  116 + $item = explode('|', $v[0]);
  117 + foreach ($item as $vo) {
  118 + $query->orWhere($vo, $v[1], $v[2]);
  119 + }
  120 + });
  121 + } else {
  122 + $query->where($v[0], $v[1], $v[2]);
  123 + }
  124 + break;
  125 + case 'in':
  126 + // in查询 ['id', 'in', [1,2,3]]
  127 + if (!is_array($v[2])) {
  128 + $v[2] = explode(',', $v[2]);
  129 + }
  130 + $query->whereIn($v[0], $v[2]);
  131 + break;
  132 + case 'not in':
  133 + // not in查询 ['id', 'not in', [1,2,3]]
  134 + if (!is_array($v[2])) {
  135 + $v[2] = explode(',', $v[2]);
  136 + }
  137 + $query->whereNotIn($v[0], $v[2]);
  138 + break;
  139 + case 'between':
  140 + // between查询 ['created_at', 'between', ['xxx', 'xxx]]
  141 + if (!is_array($v[2])) {
  142 + $v[2] = explode(',', $v[2]);
  143 + }
  144 + $query->whereBetween($v[0], $v[2]);
  145 + break;
  146 + case 'not between':
  147 + // not between查询 ['created_at', 'not between', ['xxx', 'xxx]]
  148 + if (!is_array($v[2])) {
  149 + $v[2] = explode(',', $v[2]);
  150 + }
  151 + $query->whereNotBetween($v[0], $v[2]);
  152 + break;
  153 + case 'null':
  154 + // null查询 ['deleted_at', 'null']
  155 + $query->whereNull($v[0]);
  156 + break;
  157 + case "not null":
  158 + // not null查询 ['deleted_at', 'not null']
  159 + $query->whereNotNull($v[0]);
  160 + break;
  161 + case "or":
  162 + // or查询 [[['status'=>1],['status'=>2]], 'or'];
  163 + //格式:or (status=1 and status=2)
  164 + $where = $v[0];
  165 + $query->orWhere(function ($query) use ($where) {
  166 + // 递归解析查询条件
  167 + $this->formatQuery($where, $query);
  168 + });
  169 + break;
  170 + case 'xor':
  171 + // xor查询 [[['status'=>1],['status'=>2]], 'xor'];
  172 + // 格式:and (status=1 or status=2)
  173 + $where = $v[0];
  174 + $query->where(function ($query) use ($where) {
  175 + foreach ($where as $w) {
  176 + $query->orWhere(function ($query) use ($w) {
  177 + // 递归解析查询条件
  178 + $this->formatQuery([$w], $query);
  179 + });
  180 + }
  181 + });
  182 + break;
  183 + default:
  184 + // 常规查询
  185 + if (count($v) == 2) {
  186 + $query->where($v[0], '=', $v[1]);
  187 + } else {
  188 + $query->where($v[0], $v[1], $v[2]);
  189 + }
  190 + break;
  191 + }
  192 + }
  193 + }else{
  194 + $query->where($map,$val);
  195 + }
  196 + });
  197 + return $query;
116 } 198 }
117 } 199 }
  1 +<?php
  2 +
  3 +namespace App\Models;
  4 +
  5 +class ProjectGroup extends Base
  6 +{
  7 + //设置关联表名
  8 + protected $table = 'gl_project_group';
  9 + //自动维护create_at创建时间 updated_at修改时间
  10 + public $timestamps = true;
  11 +}