Level.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/GPL-2.0
  5. * @link https://www.gougucms.com
  6. */
  7. declare (strict_types = 1);
  8. namespace app\admin\controller;
  9. use app\admin\BaseController;
  10. use app\admin\validate\LevelCheck;
  11. use think\exception\ValidateException;
  12. use think\facade\Db;
  13. use think\facade\View;
  14. class Level extends BaseController
  15. {
  16. public function index()
  17. {
  18. if (request()->isAjax()) {
  19. $level = Db::name('UserLevel')->select();
  20. return to_assign(0, '', $level);
  21. } else {
  22. return view();
  23. }
  24. }
  25. //添加新增/编辑
  26. public function add()
  27. {
  28. $param = get_params();
  29. if (request()->isAjax()) {
  30. $param['title'] = preg_replace('# #','',$param['title']);
  31. if ($param['id'] > 0) {
  32. try {
  33. validate(LevelCheck::class)->scene('edit')->check($param);
  34. } catch (ValidateException $e) {
  35. // 验证失败 输出错误信息
  36. return to_assign(1, $e->getError());
  37. }
  38. $param['update_time'] = time();
  39. Db::name('UserLevel')->strict(false)->field(true)->update($param);
  40. add_log('edit', $param['id'], $param);
  41. } else {
  42. try {
  43. validate(LevelCheck::class)->scene('add')->check($param);
  44. } catch (ValidateException $e) {
  45. // 验证失败 输出错误信息
  46. return to_assign(1, $e->getError());
  47. }
  48. $param['create_time'] = time();
  49. $mid = Db::name('UserLevel')->strict(false)->field(true)->insertGetId($param);
  50. add_log('add', $mid, $param);
  51. }
  52. return to_assign();
  53. } else {
  54. $id = isset($param['id']) ? $param['id'] : 0;
  55. if($id>0){
  56. $detail = Db::name('UserLevel')->where('id',$id)->find();
  57. View::assign('detail', $detail);
  58. }
  59. View::assign('id', $id);
  60. return view();
  61. }
  62. }
  63. //禁用/启用
  64. public function disable()
  65. {
  66. $param = get_params();
  67. $param['update_time']= time();
  68. $res = Db::name('UserLevel')->strict(false)->field('status,update_time')->update($param);
  69. if($res!==false){
  70. if($param['status'] == 0){
  71. add_log('disable', $param['id'], $param);
  72. }
  73. else if($param['status'] == 1){
  74. add_log('recovery', $param['id'], $param);
  75. }
  76. return to_assign();
  77. }
  78. else{
  79. return to_assign(1,'操作失败');
  80. }
  81. }
  82. }