SiteUserService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. use think\facade\Db;
  18. /**
  19. * 站点用户服务层
  20. * Class BaseService
  21. * @package app\service
  22. */
  23. class SiteUserService extends BaseAdminService
  24. {
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. $this->model = new SysUser();
  29. }
  30. /**
  31. * 管理端获取用户列表(对应站点用户列表)
  32. * @param array $where
  33. * @return array
  34. */
  35. public function getPage(array $where)
  36. {
  37. $site_id = $this->site_id;
  38. Db::execute("SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''))"); // 动态移除限制[2](@ref)
  39. $field = 'id,SysUserRole.uid,site_id,role_ids,SysUserRole.create_time,is_admin,SysUserRole.status,count(site_id) as site_num';
  40. $order = 'SysUserRole.create_time desc';
  41. $search_model = (new SysUserRole())
  42. ->field($field)
  43. ->order($order)
  44. ->with('userinfo')
  45. ->hasWhere('userinfo', function ($query) use ($where, $site_id) {
  46. $condition = [
  47. ['SysUserRole.site_id', '>', 0 ]
  48. ];
  49. if (!empty($where['username'])) $condition[] = ['username', 'like', "%{$where['username']}%"];
  50. if (!empty($where['realname'])) $condition[] = ['realname', 'like', "%{$where['realname']}%"];
  51. //最后登录时间
  52. if (!empty($where['last_time'])) {
  53. $start_time = empty($where['last_time'][0]) ? 0 : strtotime($where['last_time'][0]);
  54. $end_time = empty($where['last_time'][1]) ? 0 : strtotime($where['last_time'][1]);
  55. if ($start_time > 0 && $end_time > 0) {
  56. $condition[] = ['last_time', 'between', [$start_time, $end_time]];
  57. } else if ($start_time > 0 && $end_time == 0) {
  58. $condition[] = ['last_time', '>=', $start_time];
  59. } else if ($start_time == 0 && $end_time > 0) {
  60. $condition[] = ['last_time', '<=', $end_time];
  61. }
  62. }
  63. $query->where($condition);
  64. })
  65. ->group('SysUserRole.uid')
  66. ->append(['status_name']);
  67. return $this->pageQuery($search_model);
  68. }
  69. /**
  70. * 用户详情(站点用户详情)
  71. * @param int $uid
  72. * @return array
  73. */
  74. public function getInfo(int $uid)
  75. {
  76. $field = 'uid, username, head_img, real_name, last_ip, last_time, create_time, login_count, delete_time, update_time';
  77. $info = $this->model->where([ ['uid', '=', $uid] ])->field($field)->with(['roles' => function($query) {
  78. $query->field('uid, site_id, is_admin')->with('siteInfo');
  79. }])->findOrEmpty()->toArray();
  80. if (!empty($info)) {
  81. $info['roles'] = array_values(array_filter(array_map(function ($item) {
  82. if ($item['site_id']) return $item;
  83. }, $info['roles'])));
  84. }
  85. return $info;
  86. }
  87. /**
  88. * 添加当前站点用户
  89. * @param array $data
  90. * @return bool
  91. */
  92. public function add(array $data)
  93. {
  94. return (new UserService())->addSiteUser($data, $this->site_id);
  95. }
  96. /**
  97. * 编辑站点用户
  98. * @param int $uid
  99. * @param array $data
  100. * @return true
  101. */
  102. public function edit(int $uid, array $data)
  103. {
  104. return (new UserService())->editSiteUser($uid, $data, $this->site_id);
  105. }
  106. /**
  107. * 修改字段
  108. * @param int $uid
  109. * @param string $field
  110. * @param $data
  111. * @return bool|true
  112. */
  113. public function modify(int $uid, string $field, $data)
  114. {
  115. $field_name = match ($field) {
  116. 'password' => 'password',
  117. 'real_name' => 'real_name',
  118. 'head_img' => 'head_img',
  119. };
  120. return (new UserService())->edit($uid, [$field_name => $data]);
  121. }
  122. /**
  123. * 删除
  124. * @param int $uid
  125. * @return true
  126. */
  127. public function del(int $uid)
  128. {
  129. $where = [
  130. ['uid', '=', $uid],
  131. ['site_id', '=', $this->site_id]
  132. ];
  133. SysUserRole::where($where)->delete();
  134. return true;
  135. }
  136. /**
  137. * 锁定
  138. * @param int $uid
  139. * @return bool|true
  140. */
  141. public function lock(int $uid){
  142. return (new UserService())->statusChange($uid, UserDict::OFF);
  143. }
  144. /**
  145. * 解锁
  146. * @param int $uid
  147. * @return bool|true
  148. */
  149. public function unlock(int $uid){
  150. return (new UserService())->statusChange($uid, UserDict::ON);
  151. }
  152. }