StoreCategory.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace app\admin\controller\store;
  3. use app\admin\controller\AuthController;
  4. use think\Request;
  5. use think\facade\Route as Url;
  6. use app\admin\model\system\SystemAttachment;
  7. use app\admin\model\store\StoreCategory as CategoryModel;
  8. use crmeb\services\{FormBuilder as Form, JsonService as Json, UtilService as Util};
  9. /**
  10. * 产品分类控制器
  11. * Class StoreCategory
  12. * @package app\admin\controller\system
  13. */
  14. class StoreCategory extends AuthController
  15. {
  16. /**
  17. * 显示资源列表
  18. *
  19. * @return \think\Response
  20. */
  21. public function index()
  22. {
  23. $this->assign('pid', $this->request->get('pid', 0));
  24. $this->assign('cate', CategoryModel::getTierList(null, 0));
  25. $this->assign('type',0);
  26. return $this->fetch();
  27. }
  28. public function index1()
  29. {
  30. $this->assign('pid', $this->request->get('pid', 0));
  31. $this->assign('cate', CategoryModel::getTierList(null, 0,1));
  32. $this->assign('type',1);
  33. return $this->fetch('index');
  34. }
  35. /*
  36. * 异步获取分类列表
  37. * @return json
  38. */
  39. public function category_list()
  40. {
  41. $where = Util::getMore([
  42. ['is_show', ''],
  43. ['pid', $this->request->param('pid', '')],
  44. ['cate_name', ''],
  45. ['page', 1],
  46. ['limit', 20],
  47. ['order', ''],
  48. ['type',0]
  49. ]);
  50. return Json::successlayui(CategoryModel::CategoryList($where));
  51. }
  52. /**
  53. * 设置产品分类上架|下架
  54. * @param string $is_show
  55. * @param string $id
  56. */
  57. public function set_show($is_show = '', $id = '')
  58. {
  59. ($is_show == '' || $id == '') && Json::fail('缺少参数');
  60. if (CategoryModel::setCategoryShow($id, (int)$is_show)) {
  61. return Json::successful($is_show == 1 ? '显示成功' : '隐藏成功');
  62. } else {
  63. return Json::fail(CategoryModel::getErrorInfo($is_show == 1 ? '显示失败' : '隐藏失败'));
  64. }
  65. }
  66. /**
  67. * 快速编辑
  68. * @param string $field
  69. * @param string $id
  70. * @param string $value
  71. */
  72. public function set_category($field = '', $id = '', $value = '')
  73. {
  74. $field == '' || $id == '' || $value == '' && Json::fail('缺少参数');
  75. if (CategoryModel::where('id', $id)->update([$field => $value]))
  76. return Json::successful('保存成功');
  77. else
  78. return Json::fail('保存失败');
  79. }
  80. /**
  81. * 显示创建资源表单页.
  82. *
  83. * @return \think\Response
  84. */
  85. public function create($type=0)
  86. {
  87. $field = [
  88. Form::select('pid', '父级')->setOptions(function () use($type){
  89. $list = CategoryModel::getTierList(null, 0,$type);
  90. $menus = [['value' => 0, 'label' => '顶级菜单']];
  91. foreach ($list as $menu) {
  92. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name']];
  93. }
  94. return $menus;
  95. })->filterable(1),
  96. Form::input('cate_name', '分类名称'),
  97. Form::frameImageOne('pic', '分类图标(180*180)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'pic')))->icon('image')->width('100%')->height('500px'),
  98. Form::number('sort', '排序'),
  99. Form::radio('is_show', '状态', 1)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]]),
  100. Form::hidden('type',$type),
  101. ];
  102. $form = Form::make_post_form('添加分类', $field, Url::buildUrl('save'), 2);
  103. $this->assign(compact('form'));
  104. return $this->fetch('public/form-builder');
  105. }
  106. /**
  107. * 保存新建的资源
  108. *
  109. * @param \think\Request $request
  110. * @return \think\Response
  111. */
  112. public function save(Request $request)
  113. {
  114. $data = Util::postMore([
  115. 'pid',
  116. 'cate_name',
  117. ['pic', []],
  118. 'sort',
  119. ['is_show', 0],
  120. ['type',0]
  121. ], $request);
  122. if ($data['pid'] == '') return Json::fail('请选择父类');
  123. if (!$data['cate_name']) return Json::fail('请输入分类名称');
  124. if (count($data['pic']) < 1) return Json::fail('请上传分类图标');
  125. if ($data['sort'] < 0) $data['sort'] = 0;
  126. $data['pic'] = $data['pic'][0];
  127. $data['add_time'] = time();
  128. CategoryModel::create($data);
  129. return Json::successful('添加分类成功!');
  130. }
  131. /**
  132. * 显示编辑资源表单页.
  133. *
  134. * @param int $id
  135. * @return \think\Response
  136. */
  137. public function edit($id)
  138. {
  139. $c = CategoryModel::get($id);
  140. if (!$c) return Json::fail('数据不存在!');
  141. $field = [
  142. Form::select('pid', '父级', (string)$c->getData('pid'))->setOptions(function () use ($id) {
  143. $list = CategoryModel::getTierList(CategoryModel::where('id', '<>', $id), 0);
  144. // $list = (sort_list_tier((CategoryModel::where('id','<>',$id)->select()->toArray(),'顶级','pid','cate_name'));
  145. $menus = [['value' => 0, 'label' => '顶级菜单']];
  146. foreach ($list as $menu) {
  147. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name']];
  148. }
  149. return $menus;
  150. })->filterable(1),
  151. Form::input('cate_name', '分类名称', $c->getData('cate_name')),
  152. Form::frameImageOne('pic', '分类图标', Url::buildUrl('admin/widget.images/index', array('fodder' => 'pic')), $c->getData('pic'))->icon('image')->width('100%')->height('500px'),
  153. Form::number('sort', '排序', $c->getData('sort')),
  154. Form::radio('is_show', '状态', $c->getData('is_show'))->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])
  155. ];
  156. $form = Form::make_post_form('编辑分类', $field, Url::buildUrl('update', array('id' => $id)), 2);
  157. $this->assign(compact('form'));
  158. return $this->fetch('public/form-builder');
  159. }
  160. /**
  161. * 保存更新的资源
  162. *
  163. * @param \think\Request $request
  164. * @param int $id
  165. * @return \think\Response
  166. */
  167. public function update(Request $request, $id)
  168. {
  169. $data = Util::postMore([
  170. 'pid',
  171. 'cate_name',
  172. ['pic', []],
  173. 'sort',
  174. ['is_show', 0]
  175. ], $request);
  176. if ($data['pid'] == '') return Json::fail('请选择父类');
  177. if (!$data['cate_name']) return Json::fail('请输入分类名称');
  178. if (count($data['pic']) < 1) return Json::fail('请上传分类图标');
  179. if ($data['sort'] < 0) $data['sort'] = 0;
  180. $data['pic'] = $data['pic'][0];
  181. CategoryModel::edit($data, $id);
  182. return Json::successful('修改成功!');
  183. }
  184. /**
  185. * 删除指定资源
  186. *
  187. * @param int $id
  188. * @return \think\Response
  189. */
  190. public function delete($id)
  191. {
  192. if (!CategoryModel::delCategory($id))
  193. return Json::fail(CategoryModel::getErrorInfo('删除失败,请稍候再试!'));
  194. else
  195. return Json::successful('删除成功!');
  196. }
  197. }