UserService.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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\user;
  12. use app\dict\sys\UserDict;
  13. use app\model\sys\SysUser;
  14. use app\model\sys\SysUserRole;
  15. use app\service\admin\auth\LoginService;
  16. use core\base\BaseAdminService;
  17. use core\exception\AdminException;
  18. use Exception;
  19. use think\db\exception\DbException;
  20. use think\facade\Db;
  21. use think\Model;
  22. /**
  23. * 用户服务层
  24. * Class BaseService
  25. * @package app\service
  26. */
  27. class UserService extends BaseAdminService
  28. {
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. $this->model = new SysUser();
  33. }
  34. /**
  35. * 用户列表
  36. * @param array $where
  37. * @return array
  38. */
  39. public function getPage(array $where)
  40. {
  41. return $this->getPageList($this->model, $where, 'uid,username,head_img,real_name,last_ip,last_time,login_count,status', 'uid desc',['status_name']);
  42. }
  43. /**
  44. * 用户详情
  45. * @param int $uid
  46. * @return array
  47. */
  48. public function getInfo(int $uid){
  49. $where = [
  50. ['uid', '=', $uid],
  51. ];
  52. $field = 'uid, username, head_img, real_name, last_ip, last_time, create_time, login_count, status, delete_time, update_time';
  53. $user = $this->model->where($where)->field($field)->append(['status_name'])->findOrEmpty();
  54. return $user->toArray();
  55. }
  56. /**
  57. * 获取用户列表
  58. * @param array $where
  59. * @return array
  60. */
  61. public function getUserAdminPage(array $where)
  62. {
  63. $site_id = $this->site_id;
  64. $field = 'id,SysUserRole.uid,site_id,role_ids,SysUserRole.create_time,is_admin,SysUserRole.status';
  65. $order = 'SysUserRole.create_time desc';
  66. $search_model = (new SysUserRole())
  67. ->field($field)
  68. ->order($order)
  69. ->with('userinfo')
  70. ->hasWhere('userinfo', function ($query) use ($where, $site_id) {
  71. $condition = [
  72. ['SysUserRole.site_id', '=', $site_id ]
  73. ];
  74. if (!empty($where['username'])) $condition[] = ['username', 'like', "%{$where['username']}%"];
  75. if (!empty($where['realname'])) $condition[] = ['realname', 'like', "%{$where['realname']}%"];
  76. $query->where($condition);
  77. })
  78. ->append(['status_name']);
  79. return $this->pageQuery($search_model, function ($item, $key) {
  80. if (!empty($item->role_ids)) {
  81. $item->role_array = (new UserRoleService())->getRoleByUserRoleIds($item->role_ids, $this->site_id);
  82. } else {
  83. $item->role_array = [];
  84. }
  85. });
  86. }
  87. /**
  88. * 获取用户信息
  89. * @param int $uid
  90. * @return array
  91. */
  92. public function getUserAdminInfo(int $uid)
  93. {
  94. $field = 'id,uid,site_id,role_ids,create_time,is_admin,status';
  95. $info = (new SysUserRole())->where([ ['uid', '=', $uid], ['site_id', '=', $this->site_id ] ])
  96. ->field($field)
  97. ->with('userinfo')
  98. ->findOrEmpty()
  99. ->toArray();
  100. if (!empty($info)) {
  101. if (!empty($info['role_ids'])) {
  102. $info['role_array'] = (new UserRoleService())->getRoleByUserRoleIds($info['role_ids'], $this->site_id);
  103. } else {
  104. $info['role_array'] = [];
  105. }
  106. }
  107. return $info;
  108. }
  109. /**
  110. * 添加用户(添加用户,不添加站点)
  111. * @param array $data
  112. * @return bool
  113. * @throws Exception
  114. */
  115. public function add(array $data){
  116. $user_data = [
  117. 'username' => $data['username'],
  118. 'head_img' => $data['head_img'],
  119. 'status' => $data['status'],
  120. 'real_name' => $data['real_name'],
  121. 'password' => create_password($data['password'])
  122. ];
  123. $user = $this->model->create($user_data);
  124. return $user?->uid;
  125. }
  126. /**
  127. * 添加对应站点用户(添加站点,同时添加站点用户,用于添加站点以及站点添加站点用户)
  128. * @param $data
  129. * @param $site_id
  130. * @return bool
  131. */
  132. public function addSiteUser($data, $site_id)
  133. {
  134. Db::startTrans();
  135. try {
  136. if (isset($data['uid']) && !empty($data['uid'])) {
  137. $uid = $data['uid'];
  138. $user = $this->model->where([ ['uid', '=', $uid] ])->field('uid')->findOrEmpty();
  139. if ($user->isEmpty()) {
  140. Db::commit();
  141. throw new AdminException('USER_NOT_EXIST');
  142. }
  143. } else {
  144. //添加用户
  145. $uid = $this->add($data);
  146. }
  147. $role_ids = $data['role_ids'] ?? [];
  148. $is_admin = $data['is_admin'] ?? 0;
  149. //创建用户站点管理权限
  150. (new UserRoleService())->add($uid, ['role_ids' => $role_ids, 'is_admin' => $is_admin, 'status' => $data['status'] ?? UserDict::ON], $site_id);
  151. Db::commit();
  152. return $uid;
  153. } catch ( Exception $e) {
  154. Db::rollback();
  155. throw new AdminException($e->getMessage());
  156. }
  157. }
  158. /**
  159. * 更新对应站点用户
  160. * @param $uid
  161. * @param $data
  162. * @param $site_id
  163. * @return true
  164. */
  165. public function editSiteUser($uid, $data, $site_id)
  166. {
  167. Db::startTrans();
  168. try {
  169. //添加用户
  170. $this->edit($uid, $data);
  171. $role_ids = $data['role_ids'] ?? [];
  172. $is_admin = $data['is_admin'] ?? 0;
  173. //创建用户站点管理权限
  174. (new UserRoleService())->edit($site_id, $uid, $role_ids);
  175. Db::commit();
  176. return true;
  177. } catch ( Exception $e) {
  178. Db::rollback();
  179. throw new AdminException($e->getMessage());
  180. }
  181. }
  182. /**
  183. * 检测用户名是否重复
  184. * @param $username
  185. * @return bool
  186. * @throws DbException
  187. */
  188. public function checkUsername($username)
  189. {
  190. $count = $this->model->where([['username', '=', $username]])->count();
  191. if($count > 0)
  192. {
  193. return true;
  194. }
  195. else return false;
  196. }
  197. /**
  198. * 用户模型对象
  199. * @param int $uid
  200. * @return SysUser|array|mixed|Model
  201. */
  202. public function find(int $uid){
  203. $user = $this->model->findOrEmpty($uid);
  204. if ($user->isEmpty())
  205. throw new AdminException('USER_NOT_EXIST');
  206. return $user;
  207. }
  208. /**
  209. * 编辑用户
  210. * @param int $uid
  211. * @param array $data
  212. * @return true
  213. */
  214. public function edit(int $uid, array $data){
  215. $user = $this->find($uid);
  216. $user_data = [
  217. ];
  218. $is_off_status = false;
  219. if(isset($data['status'])){
  220. $this->statusChange($uid, $data['status']);
  221. if($data['status'] == UserDict::OFF)
  222. $is_off_status = true;
  223. }
  224. if(isset($data['head_img'])){
  225. $user_data['head_img'] = $data['head_img'];
  226. }
  227. if(isset($data['real_name'])){
  228. $user_data['real_name'] = $data['real_name'];
  229. }
  230. $password = $data['password'] ?? '';
  231. $is_change_password = false;
  232. if(!empty($password) && !check_password($password, $user->password)){
  233. $user_data['password'] = create_password($password);
  234. $is_change_password = true;
  235. }
  236. if(empty($user_data))
  237. return true;
  238. //更新用户信息
  239. $user->save($user_data);
  240. //更新权限 禁用用户 修改密码 都会清理token
  241. if($is_off_status || $is_change_password){
  242. LoginService::clearToken($uid);
  243. }
  244. return true;
  245. }
  246. /**
  247. * 改变用户状态
  248. * @param $uid
  249. * @param $status
  250. * @return true
  251. */
  252. public function statusChange($uid, $status) {
  253. (new SysUserRole())->where([ ['uid', '=', $uid], ['site_id', '=', $this->site_id] ])->update(['status' => $status]);
  254. LoginService::clearToken($uid);
  255. return true;
  256. }
  257. /**
  258. * 删除
  259. * @param int $uid
  260. * @return true
  261. */
  262. public function del(int $uid){
  263. $where = [
  264. ['uid', '=', $uid],
  265. ['site_id', '=', $this->site_id]
  266. ];
  267. (new SysUserRole())->where($where)->delete();
  268. return true;
  269. }
  270. /**
  271. * 通过账号获取管理员信息
  272. * @param string $username
  273. * @return SysUser|array|mixed|Model
  274. */
  275. public function getUserInfoByUsername(string $username){
  276. return $this->model->where([['username', '=',$username]])->findOrEmpty();
  277. }
  278. /**
  279. * 获取全部用户列表(用于平台整体用户管理)
  280. * @param array $where
  281. * @return array
  282. */
  283. public function getUserAllPage(array $where)
  284. {
  285. $field = 'uid, username, head_img';
  286. return $this->model->withSearch(['username', 'realname', 'create_time'], $where)
  287. ->field($field)
  288. ->order('uid desc')
  289. ->select()
  290. ->toArray();
  291. }
  292. }