Subject.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\special;
  12. use app\admin\model\special\SpecialSubject;
  13. use app\admin\model\special\Grade;
  14. use app\admin\model\special\Special;
  15. use think\Url;
  16. use service\FormBuilder as Form;
  17. use service\JsonService as Json;
  18. use app\admin\controller\AuthController;
  19. /**
  20. * 科目控制器
  21. * Class Grade
  22. * @package app\admin\controller\special
  23. */
  24. class Subject extends AuthController
  25. {
  26. public function index($pid=0)
  27. {
  28. $this->assign('grade', Grade::getAll());
  29. $this->assign('pid',$pid);
  30. return $this->fetch();
  31. }
  32. public function get_subject_list()
  33. {
  34. $where = parent::getMore([
  35. ['page', 1],
  36. ['limit', 20],
  37. ['pid',$this->request->param('pid','')],
  38. ['name', ''],
  39. ]);
  40. return Json::successlayui(SpecialSubject::get_subject_list($where));
  41. }
  42. /**
  43. * 显示创建资源表单页.
  44. *
  45. * @return \think\Response
  46. */
  47. public function create($id = 0)
  48. {
  49. if ($id) $subject = SpecialSubject::get($id);
  50. $form = Form::create(Url::build('save', ['id' => $id]), [
  51. Form::select('grade_id', '一级分类', isset($subject) ? (string)$subject->grade_id : 0)->setOptions(function () {
  52. $list = Grade::getAll();
  53. $menus = [['value' => 0, 'label' => '顶级菜单']];
  54. foreach ($list as $menu) {
  55. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  56. }
  57. return $menus;
  58. })->filterable(1),
  59. Form::input('name', '分类名称', isset($subject) ? $subject->name : ''),
  60. Form::frameImageOne('pic', '图标', Url::build('admin/widget.images/index', array('fodder' => 'pic')), isset($subject) ? $subject->pic : '')->icon('image')->width('70%')->height('500px'),
  61. Form::number('sort', '排序', isset($subject) ? $subject->sort : 0),
  62. Form::radio('is_show', '状态', isset($subject) ? $subject->is_show : 1)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])
  63. ]);
  64. $form->setMethod('post')->setTitle($id ? '修改二级分类':'添加二级分类')->setSuccessScript('parent.$(".J_iframe:visible")[0].contentWindow.location.reload();');
  65. $this->assign(compact('form'));
  66. return $this->fetch('public/form-builder');
  67. }
  68. /**
  69. * 新增或者修改
  70. *
  71. * @return json
  72. */
  73. public function save($id = 0)
  74. {
  75. $post = parent::postMore([
  76. ['name', ''],
  77. ['pic', ''],
  78. ['is_show', 1],
  79. ['grade_id', 0],
  80. ['sort', 0],
  81. ]);
  82. if (!$post['name']) return Json::fail('请输入分类名称');
  83. if (!$post['pic']) return Json::fail('请选择分类图标');
  84. if (!$post['grade_id']) return Json::fail('请选择一级分类');
  85. if ($id) {
  86. SpecialSubject::update($post, ['id' => $id]);
  87. return Json::successful('修改成功');
  88. } else {
  89. $post['add_time'] = time();
  90. if (SpecialSubject::set($post))
  91. return Json::successful('添加成功');
  92. else
  93. return Json::fail('添加失败');
  94. }
  95. }
  96. /**
  97. * 快速编辑
  98. *
  99. * @return json
  100. */
  101. public function set_value($field = '', $id = '', $value = '')
  102. {
  103. $field == '' || $id == '' || $value == '' && Json::fail('缺少参数');
  104. if (SpecialSubject::where(['id' => $id])->update([$field => $value]))
  105. return Json::successful('保存成功');
  106. else
  107. return Json::fail('保存失败');
  108. }
  109. /**二级分是否显示快捷操作
  110. * @param string $is_show
  111. * @param string $id
  112. * @return mixed
  113. */
  114. public function set_show($is_show = '', $id = '')
  115. {
  116. ($is_show == '' || $id == '') && Json::fail('缺少参数');
  117. $res = SpecialSubject::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  118. if ($res) {
  119. return Json::successful($is_show == 1 ? '显示成功' : '隐藏成功');
  120. } else {
  121. return Json::fail($is_show == 1 ? '显示失败' : '隐藏失败');
  122. }
  123. }
  124. /**
  125. * 删除
  126. *
  127. * @return json
  128. */
  129. public function delete($id = 0)
  130. {
  131. if (!$id) return Json::fail('缺少参数');
  132. if (Special::where('subject_id', $id)->where('is_del', 0)->count()) return Json::fail('暂无法删除,请先去除专题关联');
  133. $data['is_del']=1;
  134. if (SpecialSubject::update($data,['id'=>$id]))
  135. return Json::successful('删除成功');
  136. else
  137. return Json::fail('删除成功');
  138. }
  139. }