forked from sugar1569/CRMEB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStoreCategory.php
180 lines (168 loc) · 6.09 KB
/
StoreCategory.php
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
<?php
namespace app\admin\controller\store;
use app\admin\controller\AuthController;
use service\FormBuilder as Form;
use service\UtilService as Util;
use service\JsonService as Json;
use service\UploadService as Upload;
use think\Request;
use app\admin\model\store\StoreCategory as CategoryModel;
use think\Url;
use app\admin\model\system\SystemAttachment;
/**
* 产品分类控制器
* Class StoreCategory
* @package app\admin\controller\system
*/
class StoreCategory extends AuthController
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
$pid = $this->request->param('pid')?$this->request->param('pid'):0;
$where = Util::getMore([
['is_show',''],
['pid',$pid],
['cate_name',''],
],$this->request);
$this->assign('where',$where);
$this->assign('cate',CategoryModel::getTierList());
$this->assign(CategoryModel::systemPage($where));
return $this->fetch();
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
$field = [
Form::select('pid','父级')->setOptions(function(){
$list = CategoryModel::getTierList();
$menus = [['value'=>0,'label'=>'顶级菜单']];
foreach ($list as $menu){
$menus[] = ['value'=>$menu['id'],'label'=>$menu['html'].$menu['cate_name']];
}
return $menus;
})->filterable(1),
Form::input('cate_name','分类名称'),
Form::frameImageOne('pic','分类图标',Url::build('admin/widget.images/index',array('fodder'=>'pic')))->icon('image'),
Form::number('sort','排序'),
Form::radio('is_show','状态',1)->options([['label'=>'显示','value'=>1],['label'=>'隐藏','value'=>0]])
];
$form = Form::make_post_form('添加产品',$field,Url::build('save'));
$this->assign(compact('form'));
return $this->fetch('public/form-builder');
}
/**
* 上传图片
* @return \think\response\Json
*/
public function upload()
{
$res = Upload::image('file','store/category'.date('Ymd'));
$thumbPath = Upload::thumb($res->dir);
//产品图片上传记录
$fileInfo = $res->fileInfo->getinfo();
SystemAttachment::attachmentAdd($res->fileInfo->getSaveName(),$fileInfo['size'],$fileInfo['type'],$res->dir,$thumbPath,1);
if($res->status == 200)
return Json::successful('图片上传成功!',['name'=>$res->fileInfo->getSaveName(),'url'=>Upload::pathToUrl($thumbPath)]);
else
return Json::fail($res->error);
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
$data = Util::postMore([
'pid',
'cate_name',
['pic',[]],
'sort',
['is_show',0]
],$request);
if($data['pid'] == '') return Json::fail('请选择父类');
if(!$data['cate_name']) return Json::fail('请输入分类名称');
if(count($data['pic'])<1) return Json::fail('请上传分类图标');
if($data['sort'] <0 ) $data['sort'] = 0;
$data['pic'] = $data['pic'][0];
$data['add_time'] = time();
CategoryModel::set($data);
return Json::successful('添加分类成功!');
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
$c = CategoryModel::get($id);
if(!$c) return Json::fail('数据不存在!');
$form = Form::create(Url::build('update',array('id'=>$id)),[
Form::select('pid','父级',(string)$c->getData('pid'))->setOptions(function() use($id){
$list = CategoryModel::getTierList(CategoryModel::where('id','<>',$id));
// $list = (Util::sortListTier(CategoryModel::where('id','<>',$id)->select()->toArray(),'顶级','pid','cate_name'));
$menus = [['value'=>0,'label'=>'顶级菜单']];
foreach ($list as $menu){
$menus[] = ['value'=>$menu['id'],'label'=>$menu['html'].$menu['cate_name']];
}
return $menus;
})->filterable(1),
Form::input('cate_name','分类名称',$c->getData('cate_name')),
Form::frameImageOne('pic','分类图标',Url::build('admin/widget.images/index',array('fodder'=>'pic')),$c->getData('pic'))->icon('image'),
Form::number('sort','排序',$c->getData('sort')),
Form::radio('is_show','状态',$c->getData('is_show'))->options([['label'=>'显示','value'=>1],['label'=>'隐藏','value'=>0]])
]);
$form->setMethod('post')->setTitle('添加分类');
$this->assign(compact('form'));
return $this->fetch('public/form-builder');
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
$data = Util::postMore([
'pid',
'cate_name',
['pic',[]],
'sort',
['is_show',0]
],$request);
if($data['pid'] == '') return Json::fail('请选择父类');
if(!$data['cate_name']) return Json::fail('请输入分类名称');
if(count($data['pic'])<1) return Json::fail('请上传分类图标');
if($data['sort'] <0 ) $data['sort'] = 0;
$data['pic'] = $data['pic'][0];
CategoryModel::edit($data,$id);
return Json::successful('修改成功!');
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
if(!CategoryModel::delCategory($id))
return Json::fail(CategoryModel::getErrorInfo('删除失败,请稍候再试!'));
else
return Json::successful('删除成功!');
}
}