NavLogic.php
5.8 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
<?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 :保存数据
* @name :navSave
* @author :lyh
* @method :post
* @time :2023/8/23 11:14
*/
public function navSave()
{
if(!empty($this->param['location'])){
if($this->param['location'] == 'header'){
$this->param['group_id'] = BNavGroup::DEFAULT_HEADER_ID;
}
if($this->param['location'] == 'footer'){
$this->param['group_id'] = BNavGroup::DEFAULT_FOOTER_ID;
}
}
unset($this->param['able_import']);
$this->param['image'] = str_replace_url(isset($this->param['image']) ? $this->param['image'] : '');
$this->param['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($this->param,['id'=>$this->param['id']]);
}else{
$this->param['project_id'] = $this->user['project_id'];
$this->model->add($this->param);
}
//编辑菜单后,通知更新
$this->updateNotify(['project_id'=>$this->user['project_id'], 'type'=>RouteMap::SOURCE_NAV, 'route'=>'all']);
return $this->success();
}
/**
* @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('不允许成为自己的上级');
}
$pid_info = $this->model->read(['pid'=>$this->param['id']]);
if(($pid_info !== false) && $this->param['pid'] != $info['pid']){
$this->fail('当前菜单拥有子集不允许修改上级');
}
return $this->success();
}
/**
* @remark :删除菜单
* @name :navDelete
* @author :lyh
* @method :post
* @time :2023/8/23 11:12
*/
public function navDelete()
{
//判断当前菜单是否拥有下级
$info = $this->model->read(['pid'=>$this->param['id']]);
if($info !== false){
$this->fail('当前菜单存在下级,不允许删除');
}
$rs = $this->model->del($this->param);
if($rs === false){
$this->fail('error');
}
//编辑菜单后,通知更新
$this->updateNotify(['project_id'=>$this->user['project_id'], 'type'=>RouteMap::SOURCE_NAV, 'route'=>'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');
}
}
$list = $category->list(['pid' => $pid], 'id', $fields, 'asc');
$data = [];
$time = date('Y-m-d H:i:s');
foreach ($list as $item) {
$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']);
}
}
}