StoreCategory.php 7.1 KB

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