User.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace app\admin\model;
  3. use\think\Model;
  4. use think\Session;
  5. use think\cache\driver\Redis;
  6. use think\Loader;
  7. use think\Cache;
  8. class User extends Model
  9. {
  10. /**
  11. * 用户列表
  12. */
  13. public function userList()
  14. {
  15. $code = -2;
  16. $limit = (input('pageSize') ?? 10);
  17. $currentPage = (input('currentPage') ?? 1);
  18. $userName = input('userName');
  19. $getData = [
  20. 'pageSize' => $limit,
  21. 'currentPage' => $currentPage,
  22. 'userName' => $userName,
  23. ];
  24. // 验证传参.
  25. $validate = Loader::validate('Common');
  26. if (!$validate->scene('userList')->check($getData)) {
  27. return [
  28. 'code' => $code,
  29. 'msg' => $validate->getError(),
  30. 'data' => [
  31. 'userList' => [],
  32. 'userCount' => 0,
  33. 'currentPage' => 1,
  34. 'page' => [],
  35. ],
  36. ];
  37. }
  38. $offset = (($currentPage - 1) * $limit);
  39. $where = [];
  40. if (strlen($userName)) {
  41. $where['user_name'] = [
  42. 'like',
  43. "%$userName%"
  44. ];
  45. }
  46. // 获取用户列表.
  47. $userList = $this;
  48. if ($where) {
  49. $userList = $userList->where($where);
  50. }
  51. $userList = $userList
  52. ->limit($offset, $limit)
  53. ->select();
  54. // 查询总数.
  55. $userCount = $this;
  56. if ($where) {
  57. $userCount = $userCount->where($where);
  58. }
  59. $userCount = $userCount->count();
  60. // 分页.
  61. $page = getPage($userCount, $limit, $currentPage);
  62. return [
  63. 'code' => 1,
  64. 'msg' => lang('MC01005'),
  65. 'data' => [
  66. 'userList' => $userList,
  67. 'userCount' => $userCount,
  68. 'currentPage' => $currentPage,
  69. 'page' => $page,
  70. ],
  71. ];
  72. }//end userList()
  73. /**
  74. * 修改用户
  75. */
  76. public function updateUser()
  77. {
  78. $code = -2;
  79. $userId = input('id');
  80. $userStatus = input('status');
  81. $getData = [
  82. 'user_id' => $userId,
  83. 'user_status' => $userStatus,
  84. ];
  85. // 验证传参.
  86. $validate = Loader::validate('Common');
  87. if (!$validate->scene('userList')->check($getData)) {
  88. return [
  89. 'code' => $code,
  90. 'msg' => $validate->getError(),
  91. 'data' => [],
  92. ];
  93. }
  94. // 修改用户.
  95. $where['user_id'] = $userId;
  96. $data['user_status'] = $userStatus;
  97. $updateUser = $this
  98. ->where($where)
  99. ->update($data);
  100. return [
  101. 'code' => 1,
  102. 'msg' => lang('MC01004'),
  103. 'data' => [],
  104. ];
  105. }//end updateUser()
  106. /**
  107. * 删除所选用户
  108. */
  109. public function delUser($ids)
  110. {
  111. $this->wherein('user_id',$ids)->delete();
  112. return [
  113. 'code' => 1,
  114. 'msg' => lang('MC01003'),
  115. 'data' => [],
  116. ];
  117. }//end delUser()
  118. }