StoreCategory.php 6.7 KB

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