UserGroup.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app\adminapi\controller\v1\user;
  3. use app\adminapi\controller\AuthController;
  4. use app\models\user\UserGroup as GroupModel;
  5. use crmeb\services\{FormBuilder as Form, UtilService as Util};
  6. use think\facade\Route as Url;
  7. use crmeb\traits\CurdControllerTrait;
  8. /**
  9. * 会员设置
  10. * Class UserLevel
  11. * @package app\admin\controller\user
  12. */
  13. class UserGroup extends AuthController
  14. {
  15. use CurdControllerTrait;
  16. /**
  17. * 分组列表
  18. */
  19. public function index()
  20. {
  21. $where = Util::getMore([
  22. ['page', 1],
  23. ['limit', 20],
  24. ]);
  25. return $this->success(GroupModel::getList($where));
  26. }
  27. /**
  28. * 添加/修改分组页面
  29. * @param int $id
  30. * @return string
  31. */
  32. public function add()
  33. {
  34. $data = Util::getMore([
  35. ['id', 0],
  36. ]);
  37. $group = GroupModel::get($data['id']);
  38. $field = array();
  39. if (!$group) {
  40. $title = '添加分组';
  41. $field[] = Form::input('group_name', '分组名称', '');
  42. } else {
  43. $title = '修改分组';
  44. $field[] = Form::hidden('id', $group->getData('id'));
  45. $field[] = Form::input('group_name', '分组名称', $group->getData('group_name'));
  46. }
  47. return $this->makePostForm($title, $field, Url::buildUrl('/user/user_group/save'), 'POST');
  48. }
  49. /**
  50. *
  51. * @param int $id
  52. * @return mixed
  53. */
  54. public function save()
  55. {
  56. $data = Util::postMore([
  57. ['id', 0],
  58. ['group_name', ''],
  59. ]);
  60. if ($data['id'] != 0) {
  61. if (GroupModel::where('id', $data['id'])->update($data)) {
  62. return $this->success('修改成功');
  63. } else {
  64. return $this->fail('修改失败或者您没有修改什么!');
  65. }
  66. } else {
  67. unset($data['id']);
  68. if ($res = GroupModel::create($data)) {
  69. return $this->success('添加成功');
  70. } else {
  71. return $this->fail('添加失败!');
  72. }
  73. }
  74. }
  75. /**
  76. * 删除
  77. * @param $id
  78. * @throws \Exception
  79. */
  80. public function delete()
  81. {
  82. $data = Util::getMore([
  83. ['id', 0],
  84. ]);
  85. if (!$data['id']) return $this->fail('数据不存在');
  86. if (!GroupModel::be(['id' => $data['id']])) return $this->fail('分组数据不存在');
  87. if (!GroupModel::where('id', $data['id'])->delete()) {
  88. return $this->fail(GroupModel::getErrorInfo('删除失败,请稍候再试!'));
  89. } else {
  90. return $this->success('删除成功!');
  91. }
  92. }
  93. }