Group.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace app\admin\controller\users;
  3. use app\common\controller\AdminController;
  4. use app\http\IResponse;
  5. class Group extends AdminController
  6. {
  7. /**
  8. * 获取系统会员列表
  9. *
  10. * @return mixed
  11. * @throws \think\exception\DbException
  12. */
  13. public function index()
  14. {
  15. $where = [];
  16. //组合搜索
  17. !empty(input('name')) && $where[]
  18. = ['name', 'like', '%' . input('name') . '%'];
  19. return IResponse::paginate(model('common/UsersGroup')->withTrashed()->where($where)
  20. ->paginate(input('limit'),false));
  21. }
  22. /**
  23. *
  24. * @return mixed
  25. */
  26. public function save()
  27. {
  28. // 接收数据
  29. $params = $this->request->param();
  30. // 数据校验
  31. $valid = $this->validate($params,[
  32. 'name|用户组名称' => 'require|chs|unique:UsersGroup',
  33. 'introduce|用户组描述' => 'require'
  34. ],[
  35. 'name.chs' => '用户组名称仅支持中文!',
  36. 'name.unique' => '用户组名称已存在!'
  37. ]);
  38. (true !== $valid) && IResponse::failure($valid);
  39. // 保存数据
  40. $res = model('common/UsersGroup')->storeBy($params);
  41. return $res ? IResponse::success([],'新增用户组成功'):
  42. IResponse::failure('新增用户组异常');
  43. }
  44. /**
  45. * 更新数据
  46. *
  47. * @author 许祖兴 < zuxing.xu@lettered.cn>
  48. * @date 2020/6/5 14:24
  49. *
  50. * @param $id
  51. * @return \think\response\Json
  52. */
  53. public function update($id)
  54. {
  55. // 接收数据
  56. $params = $this->request->param();
  57. // 数据校验
  58. $valid = $this->validate($params,[
  59. 'name|用户组名称' => 'require|chs',
  60. 'introduce|用户组描述' => 'require'
  61. ],[
  62. 'name.chs' => '用户组名称仅支持中文!',
  63. ]);
  64. // 校验失败
  65. (true !== $valid) && IResponse::failure($valid);
  66. // 查改
  67. $role = model('common/UsersGroup')->findBy($id);
  68. $role->updateBy($id, $params);
  69. return IResponse::success('更新用户组信息成功');
  70. }
  71. /**
  72. * 删除用户组
  73. *
  74. * @author 许祖兴 < zuxing.xu@lettered.cn>
  75. * @date 2020/6/5 14:22
  76. *
  77. * @param $id
  78. * @return \think\response\Json
  79. */
  80. public function delete($id)
  81. {
  82. model('common/UsersGroup')->deleteBy($id);
  83. return IResponse::success([],'删除用户组成功');
  84. }
  85. /**
  86. * 恢复删除用户组
  87. *
  88. * @author 许祖兴 < zuxing.xu@lettered.cn>
  89. * @date 2020/6/6 13:05
  90. *
  91. * @param $id
  92. * @return mixed
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. * @throws \think\exception\DbException
  96. */
  97. public function restore($id)
  98. {
  99. // 查询数据
  100. $userGroup = model('common/UsersGroup')->onlyTrashed()->find($id);
  101. if (!$userGroup){
  102. return IResponse::failure('用户组不存在!');
  103. }
  104. // 恢复
  105. return $userGroup->restore() ? IResponse::success('恢复用户组成功!')
  106. : IResponse::failure('恢复用户组失败!');
  107. }
  108. }