Section.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\adminapi\controller\v1\section;
  3. use app\adminapi\controller\AuthController;
  4. use app\models\section\SectionModel;
  5. use think\Request;
  6. use think\facade\Route as Url;
  7. use crmeb\services\{
  8. FormBuilder as Form, UtilService as Util
  9. };
  10. /**
  11. * 部门管理
  12. */
  13. class Section extends AuthController
  14. {
  15. /**
  16. * 显示资源列表
  17. */
  18. public function index()
  19. {
  20. $where = Util::getMore([
  21. ['page', 1],
  22. ['limit', 15],
  23. ['status', ''],
  24. ['title', ''],
  25. ], $this->request);
  26. $list = SectionModel::systemPage($where);
  27. return $this->success($list);
  28. }
  29. /**
  30. * 显示创建资源表单页.
  31. *
  32. * @return \think\Response
  33. */
  34. public function create()
  35. {
  36. $f = array();
  37. $f[] = Form::input('title', '部门名称');
  38. $f[] = Form::input('intr', '部门简介')->type('textarea');
  39. $f[] = Form::frameImage('slider_image', '部门图片', Url::buildUrl('admin/widget.images/index', array('fodder' => 'slider_image')))->maxLength(5)->icon('ios-add')->width('60%')->height('435px');
  40. $f[] = Form::number('sort', '排序', 0);
  41. $f[] = Form::radio('status', '状态', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  42. return $this->makePostForm('添加部门', $f, Url::buildUrl('/section/section'), 'POST');
  43. }
  44. /**
  45. * 保存新建的资源
  46. *
  47. * @param \think\Request $request
  48. * @return \think\Response
  49. */
  50. public function save(Request $request)
  51. {
  52. $data = Util::postMore([
  53. 'title',
  54. 'intr',
  55. ['image', []],
  56. ['sort', 0],
  57. 'status',]);
  58. if (!$data['title']) return $this->fail('请输入部门名称');
  59. if (count($data['image']) != 1) return $this->fail('请选择部门图片,并且只能上传一张');
  60. if ($data['sort'] < 0) return $this->fail('排序不能是负数');
  61. $data['add_time'] = time();
  62. $data['image'] = $data['image'][0];
  63. $res = SectionModel::create($data);
  64. return $this->success('添加部门成功!');
  65. }
  66. /**
  67. * 显示编辑资源表单页.
  68. *
  69. * @param int $id
  70. * @return \think\Response
  71. */
  72. public function edit($id)
  73. {
  74. if (!$id) return $this->fail('参数错误');
  75. $section = SectionModel::get($id)->getData();
  76. if (!$section) return $this->fail('数据不存在!');
  77. $f = array();
  78. $f[] = Form::input('title', '部门名称', $section['title']);
  79. $f[] = Form::input('intr', '部门简介', $section['intr'])->type('textarea');
  80. $f[] = Form::frameImage('slider_image', '部门图片', Url::buildUrl('admin/widget.images/index', array('fodder' => 'slider_image')), $section['image'])->maxLength(5)->icon('ios-add')->width('60%')->height('435px');
  81. $f[] = Form::number('sort', '排序', $section['sort']);
  82. $f[] = Form::radio('status', '状态', $section['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  83. return $this->makePostForm('编辑部门', $f, Url::buildUrl('/section/section/' . $id), 'PUT');
  84. }
  85. /**
  86. * 保存更新的资源
  87. *
  88. * @param \think\Request $request
  89. * @param int $id
  90. * @return \think\Response
  91. */
  92. public function update(Request $request, $id)
  93. {
  94. $data = Util::postMore([
  95. 'title',
  96. 'intr',
  97. ['image', []],
  98. ['sort', 0],
  99. 'status',]);
  100. if (!$data['title']) return $this->fail('请输入分类名称');
  101. if (count($data['image']) != 1) return $this->fail('请选择分类图片,并且只能上传一张');
  102. if ($data['sort'] < 0) return $this->fail('排序不能是负数');
  103. $data['image'] = $data['image'][0];
  104. if (!SectionModel::get($id)) return $this->fail('编辑的记录不存在!');
  105. SectionModel::edit($data, $id);
  106. return $this->success('修改成功!');
  107. }
  108. /**
  109. * 删除指定资源
  110. *
  111. * @param int $id
  112. * @return \think\Response
  113. */
  114. public function delete($id)
  115. {
  116. $res = SectionModel::edit(['is_del' => 1], $id, 'id');
  117. if (!$res)
  118. return $this->fail(SectionModel::getErrorInfo('删除失败,请稍候再试!'));
  119. else
  120. return $this->success('删除成功!');
  121. }
  122. /**
  123. * 修改状态
  124. * @param $id
  125. * @param $status
  126. * @return mixed
  127. */
  128. public function set_status($id, $status)
  129. {
  130. if ($status == '' || $id == 0) return $this->fail('参数错误');
  131. SectionModel::where(['id' => $id])->update(['status' => $status]);
  132. return $this->success($status == 0 ? '隐藏成功' : '显示成功');
  133. }
  134. }