BaseController.php
5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace App\Http\Controllers\Bside;
use App\Enums\Common\Code;
use App\Http\Controllers\Controller;
use App\Models\ProjectMenu;
use App\Models\ProjectRole as ProjectRoleModel;
use \Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Http\Exceptions\HttpResponseException;
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;
protected $user = [];//当前登录用户详情
/**
* 获取所有参数
*/
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();
$this->auth_role();
}
/**
* @name
* @return void
* @author :liyuhang
* @method
*/
public function auth_token(){
$info = Cache::get($this->token);
$this->user = $info;
$this->uid = $info['id'];
//操作权限设置
$projectRoleModel = new ProjectRoleModel();
$role_info = $projectRoleModel->read(['id'=>$this->user['role_id']]);
//获取当前操作的控制器与方法
$action = $this->request->route()->getAction();
//查询当前用户是否拥有权限操作
$projectMenuModel = new ProjectMenu();
$menu_id = $projectMenuModel->read(['action'=>$action['as']],['id']);
if($menu_id !== false && strpos($role_info['role_menu'], $menu_id['id']) === false){
$this->response('拦截',Code::USER_PERMISSION_ERROE);
}
}
/**
* @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 "created_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 "updated_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;
}
/**
* @name :上传图片
* @return void
* @author :liyuhang
* @method
*/
public function uploads(){
$files = $this->request->file('file');
if(empty($files)){
return $this->response('没有上传文件',Code::USER_ERROR);
}
$url = './uploads/images/';
$param = $this->request->post();
if($this->request->hasFile('image') && $files->isValid()){
$filename = date('ymdHis').rand(10000,99999).$this->request->file('image');
$this->request->file('image')->move('./uploads/images/',$filename);
}else{
return false;
}
return $url.$filename;
}
}