StoreCategory.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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('only_store_user', '采购类', 0)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]),
  91. Form::radio('is_show', '状态', 1)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])
  92. ];
  93. $form = Form::make_post_form('添加分类', $field, Url::buildUrl('save'), 2);
  94. $this->assign(compact('form'));
  95. return $this->fetch('public/form-builder');
  96. }
  97. /**
  98. * 保存新建的资源
  99. *
  100. * @param \think\Request $request
  101. * @return \think\Response
  102. */
  103. public function save(Request $request)
  104. {
  105. $data = Util::postMore([
  106. 'pid',
  107. 'cate_name',
  108. ['pic', []],
  109. 'sort',
  110. ['is_show', 0],
  111. ['only_store_user', 0]
  112. ], $request);
  113. if ($data['pid'] == '') return Json::fail('请选择父类');
  114. if (!$data['cate_name']) return Json::fail('请输入分类名称');
  115. if (count($data['pic']) < 1) return Json::fail('请上传分类图标');
  116. if ($data['sort'] < 0) $data['sort'] = 0;
  117. $data['pic'] = $data['pic'][0];
  118. $data['add_time'] = time();
  119. CategoryModel::create($data);
  120. return Json::successful('添加分类成功!');
  121. }
  122. /**
  123. * 显示编辑资源表单页.
  124. *
  125. * @param int $id
  126. * @return \think\Response
  127. */
  128. public function edit($id)
  129. {
  130. $c = CategoryModel::get($id);
  131. if (!$c) return Json::fail('数据不存在!');
  132. $field = [
  133. Form::select('pid', '父级', (string)$c->getData('pid'))->setOptions(function () use ($id) {
  134. $list = CategoryModel::getTierList(CategoryModel::where('id', '<>', $id), 0);
  135. // $list = (sort_list_tier((CategoryModel::where('id','<>',$id)->select()->toArray(),'顶级','pid','cate_name'));
  136. $menus = [['value' => 0, 'label' => '顶级菜单']];
  137. foreach ($list as $menu) {
  138. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name']];
  139. }
  140. return $menus;
  141. })->filterable(1),
  142. Form::input('cate_name', '分类名称', $c->getData('cate_name')),
  143. Form::frameImageOne('pic', '分类图标', Url::buildUrl('admin/widget.images/index', array('fodder' => 'pic')), $c->getData('pic'))->icon('image')->width('100%')->height('500px'),
  144. Form::number('sort', '排序', $c->getData('sort')),
  145. Form::radio('only_store_user', '采购类', $c->getData('only_store_user'))->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]),
  146. Form::radio('is_show', '状态', $c->getData('is_show'))->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])
  147. ];
  148. $form = Form::make_post_form('编辑分类', $field, Url::buildUrl('update', array('id' => $id)), 2);
  149. $this->assign(compact('form'));
  150. return $this->fetch('public/form-builder');
  151. }
  152. /**
  153. * 保存更新的资源
  154. *
  155. * @param \think\Request $request
  156. * @param int $id
  157. * @return \think\Response
  158. */
  159. public function update(Request $request, $id)
  160. {
  161. $data = Util::postMore([
  162. 'pid',
  163. 'cate_name',
  164. ['pic', []],
  165. 'sort',
  166. ['only_store_user', 0],
  167. ['is_show', 0]
  168. ], $request);
  169. if ($data['pid'] == '') return Json::fail('请选择父类');
  170. if (!$data['cate_name']) return Json::fail('请输入分类名称');
  171. if (count($data['pic']) < 1) return Json::fail('请上传分类图标');
  172. if ($data['sort'] < 0) $data['sort'] = 0;
  173. $data['pic'] = $data['pic'][0];
  174. CategoryModel::edit($data, $id);
  175. return Json::successful('修改成功!');
  176. }
  177. /**
  178. * 删除指定资源
  179. *
  180. * @param int $id
  181. * @return \think\Response
  182. */
  183. public function delete($id)
  184. {
  185. if (!CategoryModel::delCategory($id))
  186. return Json::fail(CategoryModel::getErrorInfo('删除失败,请稍候再试!'));
  187. else
  188. return Json::successful('删除成功!');
  189. }
  190. }