NavLogic.php
7.9 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
namespace App\Http\Logic\Bside\Nav;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Nav\BNav;
use App\Models\Nav\BNavGroup;
use App\Models\RouteMap\RouteMap;
use Illuminate\Support\Facades\DB;
/**
* @remark :菜单管理
* @name :NavLogic
* @author :lyh
* @method :post
* @time :2023/8/23 11:14
*/
class NavLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->param = $this->requestAll;
$this->model = new BNav();
}
/**
* @remark :获取当前id下所有子集
* @name :getSubList
* @author :lyh
* @method :post
* @time :2023/12/4 15:11
*/
public function getSubList(){
//编辑时
if(isset($this->param['id']) && !empty($this->param['id'])) {
$str = [];
//排序掉当前id下所有子集
$str = $this->getAllSub($this->param['id'], $str);
$str[] = $this->param['id'];
$this->param['id'] = ['not in', $str];
}
$this->param['project_id'] = $this->user['project_id'];
$list = $this->model->list($this->param);
$data = array();
foreach ($list as $v){
$v = (array)$v;
if ($v['pid'] == 0) {
$v['sub'] = _get_child($v['id'], $list);
$data[] = $v;
}
}
return $this->success($data);
}
/**
* @remark :获取当前id下所有子集
* @name :getAllSub
* @author :lyh
* @method :post
* @time :2023/10/18 15:10
*/
public function getAllSub($id,&$str = []){
$list = $this->model->list(['pid'=>$id,'status'=>1],['id','pid']);
if(!empty($list)){
foreach ($list as $v){
$str[] = $v['id'];
$this->getAllSub($v['id'],$str);
}
}
return $str;
}
/**
* @remark :保存数据
* @name :navSave
* @author :lyh
* @method :post
* @time :2023/8/23 11:14
*/
public function navSave()
{
$data = $this->handleParam($this->param);//处理保存字符串
if(!empty($this->param['location'])){
if($this->param['location'] == 'header'){
$data['group_id'] = BNavGroup::DEFAULT_HEADER_ID;
}
if($this->param['location'] == 'footer'){
$data['group_id'] = BNavGroup::DEFAULT_FOOTER_ID;
}
}
$data['image'] = str_replace_url(isset($this->param['image']) ? $this->param['image'] : '');
$data['remark_image'] = str_replace_url(isset($this->param['remark_image']) ? $this->param['remark_image'] : '');
if(isset($this->param['id']) && !empty($this->param['id'])){
$this->handleEditParam();//验证是否可编辑分类
$this->model->edit($data,['id'=>$this->param['id']]);
}else{
$data['project_id'] = $this->user['project_id'];
$this->model->add($data);
}
//编辑菜单后,通知更新
$this->addUpdateNotify(RouteMap::SOURCE_NAV, 'all');
return $this->success();
}
/**
* @remark :保存时处理数据
* @name :saveHandleParam
* @author :lyh
* @method :post
* @time :2023/12/15 14:26
*/
public function handleParam($param){
$data = [
'pid'=>$param['pid'] ?? 0,
'name'=>$param['name'],
'location'=>$param['location'] ?? '',
'url'=>$param['url'],
'status'=>$param['status'] ?? 1,
'target'=>$param['target'] ?? 1,
'remark'=>$param['remark'] ?? '',
'group_id'=>$param['group_id'],
'show'=>$param['show'] ?? 0,
];
return $this->success($data);
}
/**
* @remark :验证是否可编辑
* @name :handleEditParam
* @author :lyh
* @method :post
* @time :2023/9/7 14:36
*/
public function handleEditParam(){
$info = $this->model->read(['id'=>$this->param['id']]);
if($this->param['pid'] == $info['id']){
$this->fail('不允许成为自己的上级');
}
return $this->success();
}
/**
* @remark :删除菜单
* @name :navDelete
* @author :lyh
* @method :post
* @time :2023/8/23 11:12
*/
public function navDelete()
{
$str = [];
//排序掉当前id下所有子集
$str = $this->getAllSub($this->param['id'], $str);
foreach ($str as $v){
$this->model->del(['id'=>$v]);
}
$this->model->del(['id'=>$this->param['id']]);
//编辑菜单后,通知更新
$this->addUpdateNotify(RouteMap::SOURCE_NAV, 'all');
return $this->success();
}
/**
* @remark :排序
* @name :navSort
* @author :lyh
* @method :post
* @time :2023/8/22 9:54
*/
public function navSort(){
$rs = $this->model->edit(['sort'=>$this->param['sort']],['id'=>$this->param['id']]);
if($rs === false){
$this->fail('error');
}
return $this->success();
}
/**
* 一键导入 对应分类
* @throws \App\Exceptions\AsideGlobalException
* @throws \App\Exceptions\BsideGlobalException
*/
public function importNav(){
$nav = $this->getInfo($this->param['id']);
if(!in_array($nav['url'], array_keys(BNav::ableImportMap()))){
$this->fail('该菜单不支持一键导入');
}
$class = BNav::ableImportMap($nav['url']);
$category = new $class();
$fields = ['id', 'name', 'pid', 'alias'];
if($nav['url'] == 'products'){
$fields = ['id', 'title as name', 'pid', 'route as alias'];
}
$this->addByPid($category, $nav, $fields);
return $this->success();
}
/**
* 一级一级的加
* @author zbj
* @date 2023/11/22
*/
protected function addByPid($category, $nav, $fields, $pid = 0)
{
$nav_pid = $nav['id'];
if($pid){
$p_cate = $category->where('id', $pid)->select($fields)->first();
if($p_cate){
$nav_pid = $this->model->where('import_id', $nav['id'])->where('url', $p_cate['alias'])->value('id') ?: $nav_pid;
}
}
$list = $category->list(['pid' => $pid], 'id', $fields, 'asc');
$data = [];
$time = date('Y-m-d H:i:s');
foreach ($list as $item) {
if(in_array($item['alias'], array_keys(BNav::ableImportMap()))){
continue;
}
$exists_info = $this->model->where('import_id', $nav['id'])->where('url', $item['alias'])->first();
if($exists_info){
continue;
}
$data[] = [
'pid' => $nav_pid,
'name' => $item['name'],
'url' => $item['alias'],
'project_id' => $this->project['id'],
'location' => $nav['location'],
'group_id' => $nav['group_id'],
'status' => BNav::STATUS_ACTIVE,
'import_id' => $nav['id'],
'created_at' => $time,
'updated_at' => $time,
];
}
//每500条更一次
$data_chunk = array_chunk($data,500);
foreach ($data_chunk as $chunk){
$this->model->insert($chunk);
}
foreach ($list as $item) {
$this->addByPid($category, $nav, $fields, $item['id']);
}
}
/**
* @remark :排序字段
* @name :setSortList
* @author :lyh
* @method :post
* @time :2023/12/18 13:47
*/
public function setSortList(){
$navGroupModel = new BNavGroup();
if(!empty($this->param['sort_list'])){
$this->param['sort_list'] = json_encode($this->param['sort_list']);
}
$navGroupModel->edit(['sort_list'=>$this->param['sort_list']],['id'=>$this->param['id']]);
return $this->success();
}
}