SiteUserService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\site;
  12. use app\dict\sys\UserDict;
  13. use app\model\sys\SysUser;
  14. use app\model\sys\SysUserRole;
  15. use app\service\admin\user\UserService;
  16. use core\base\BaseAdminService;
  17. /**
  18. * 站点用户服务层
  19. * Class BaseService
  20. * @package app\service
  21. */
  22. class SiteUserService extends BaseAdminService
  23. {
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. $this->model = new SysUser();
  28. }
  29. /**
  30. * 管理端获取用户列表(对应站点用户列表)
  31. * @param array $where
  32. * @return array
  33. */
  34. public function getPage(array $where)
  35. {
  36. $site_id = $this->site_id;
  37. $field = 'id,SysUserRole.uid,site_id,role_ids,SysUserRole.create_time,is_admin,SysUserRole.status,count(site_id) as site_num';
  38. $order = 'SysUserRole.create_time desc';
  39. $search_model = (new SysUserRole())
  40. ->field($field)
  41. ->order($order)
  42. ->with('userinfo')
  43. ->hasWhere('userinfo', function ($query) use ($where, $site_id) {
  44. $condition = [
  45. ['SysUserRole.site_id', '>', 0 ]
  46. ];
  47. if (!empty($where['username'])) $condition[] = ['username', 'like', "%{$where['username']}%"];
  48. if (!empty($where['realname'])) $condition[] = ['realname', 'like', "%{$where['realname']}%"];
  49. //最后登录时间
  50. if (!empty($where['last_time'])) {
  51. $start_time = empty($where['last_time'][0]) ? 0 : strtotime($where['last_time'][0]);
  52. $end_time = empty($where['last_time'][1]) ? 0 : strtotime($where['last_time'][1]);
  53. if ($start_time > 0 && $end_time > 0) {
  54. $condition[] = ['last_time', 'between', [$start_time, $end_time]];
  55. } else if ($start_time > 0 && $end_time == 0) {
  56. $condition[] = ['last_time', '>=', $start_time];
  57. } else if ($start_time == 0 && $end_time > 0) {
  58. $condition[] = ['last_time', '<=', $end_time];
  59. }
  60. }
  61. $query->where($condition);
  62. })
  63. ->group('SysUserRole.uid')
  64. ->append(['status_name']);
  65. return $this->pageQuery($search_model);
  66. }
  67. /**
  68. * 用户详情(站点用户详情)
  69. * @param int $uid
  70. * @return array
  71. */
  72. public function getInfo(int $uid)
  73. {
  74. $field = 'uid, username, head_img, real_name, last_ip, last_time, create_time, login_count, delete_time, update_time';
  75. $info = $this->model->where([ ['uid', '=', $uid] ])->field($field)->with(['roles' => function($query) {
  76. $query->field('uid, site_id, is_admin')->with('siteInfo');
  77. }])->findOrEmpty()->toArray();
  78. if (!empty($info)) {
  79. $info['roles'] = array_values(array_filter(array_map(function ($item) {
  80. if ($item['site_id']) return $item;
  81. }, $info['roles'])));
  82. }
  83. return $info;
  84. }
  85. /**
  86. * 添加当前站点用户
  87. * @param array $data
  88. * @return bool
  89. */
  90. public function add(array $data)
  91. {
  92. return (new UserService())->addSiteUser($data, $this->site_id);
  93. }
  94. /**
  95. * 编辑站点用户
  96. * @param int $uid
  97. * @param array $data
  98. * @return true
  99. */
  100. public function edit(int $uid, array $data)
  101. {
  102. return (new UserService())->editSiteUser($uid, $data, $this->site_id);
  103. }
  104. /**
  105. * 修改字段
  106. * @param int $uid
  107. * @param string $field
  108. * @param $data
  109. * @return bool|true
  110. */
  111. public function modify(int $uid, string $field, $data)
  112. {
  113. $field_name = match ($field) {
  114. 'password' => 'password',
  115. 'real_name' => 'real_name',
  116. 'head_img' => 'head_img',
  117. };
  118. return (new UserService())->edit($uid, [$field_name => $data]);
  119. }
  120. /**
  121. * 删除
  122. * @param int $uid
  123. * @return true
  124. */
  125. public function del(int $uid)
  126. {
  127. $where = [
  128. ['uid', '=', $uid],
  129. ['site_id', '=', $this->site_id]
  130. ];
  131. SysUserRole::where($where)->delete();
  132. return true;
  133. }
  134. /**
  135. * 锁定
  136. * @param int $uid
  137. * @return bool|true
  138. */
  139. public function lock(int $uid){
  140. return (new UserService())->statusChange($uid, UserDict::OFF);
  141. }
  142. /**
  143. * 解锁
  144. * @param int $uid
  145. * @return bool|true
  146. */
  147. public function unlock(int $uid){
  148. return (new UserService())->statusChange($uid, UserDict::ON);
  149. }
  150. }