AdminService.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace App\Services;
  3. use App\Model\Admin;
  4. use App\Model\AdminGroup;
  5. use App\Model\AdminLog;
  6. use App\Model\AdminMenu;
  7. use Illuminate\Support\Facades\Cache;
  8. class AdminService
  9. {
  10. public static function login($data)
  11. {
  12. $token = md5($data["id"].$data["name"].$data['group_id']);
  13. $arr = ["id" => $data["id"], "name" => $data["name"], 'group_id' => $data['group_id']];
  14. Admin::updateLogin($data["id"], $token, date("Y-m-d H:i:s"), request()->getClientIp());
  15. $arr['token'] = $token;
  16. return $arr;
  17. }
  18. /** token 获取 用户信息
  19. * @param $token
  20. * @return array|false|mixed
  21. */
  22. public static function tokenGetUser($token)
  23. {
  24. $userData = Cache::get($token, false);
  25. if ($userData === false) {
  26. $userData = Admin::findWhereToken($token);
  27. }
  28. if (!$userData) {
  29. return false;
  30. }
  31. $userData = ["id" => $userData["id"], "name" => $userData["name"], "group_id" => $userData['group_id']];
  32. Cache::put($token, $userData);
  33. return $userData;
  34. }
  35. /** 是否有权限
  36. * @param $adminGroupId
  37. * @param $path
  38. * @return bool
  39. */
  40. public static function therePermission($adminGroupId, $path)
  41. {
  42. // 查询用户组
  43. $adminGroupData = AdminGroup::findRole($adminGroupId);
  44. if (!$adminGroupData) {
  45. return false;
  46. }
  47. if ($adminGroupData['role'] == "all") {
  48. return true;
  49. }
  50. // 需要登陆,但不需要认证的
  51. $pathArr = [
  52. 'admin/admin/adminInfo',
  53. 'admin/admin/adminGroup',
  54. 'admin/admin/menuList',
  55. 'admin/user/rbacList',
  56. 'admin/user/userGroup',
  57. 'admin/user/getDeportmentList',
  58. 'admin/user/userSearchLog',
  59. ];
  60. if (in_array($path, $pathArr)) {
  61. return true;
  62. }
  63. // 判断用户组是否有权限
  64. $pathData = AdminMenu::findWhereUrl($path);
  65. if (!$pathData) {
  66. return false;
  67. }
  68. $roleArr = json_decode($adminGroupData['role'], true);
  69. if (!in_array($pathData['id'], $roleArr)) {
  70. return false;
  71. }
  72. return true;
  73. }
  74. /** 获取管理员列表
  75. * @param $group_id
  76. * @param $name
  77. * @param $page
  78. * @param $length
  79. * @return array
  80. */
  81. public static function adminList($group_id, $account, $page, $length, $name, $realname)
  82. {
  83. $where = [];
  84. if (!empty($account)) {
  85. $where[] = ['account','=',$account];
  86. }
  87. if (!empty($group_id)) {
  88. $where[] = ["group_id",'=',$group_id];
  89. }
  90. if (!empty($name)) {
  91. $where[] = ["name",'like',"%".$name."%"];
  92. }
  93. if (!empty($realname)) {
  94. $where[] = ["realname",'like',"%".$realname."%"];
  95. }
  96. $count = Admin::getPageCount($where);
  97. if ($count > 0) {
  98. $data = Admin::getPageAll($where, $page, $length);
  99. if (!$data) {
  100. return ["list" => [], "count" => 0];
  101. } else {
  102. $ids = array_column($data, 'group_id');
  103. $adminGroup = AdminGroup::getInIdAll($ids);
  104. $adminGroup = array_column($adminGroup, null, 'id');
  105. foreach ($data as $k => &$v) {
  106. $v["group_name"] = $adminGroup[$v["group_id"]]["name"];
  107. $v["status"] = $v['id'] == 1 ? 0 : 1;
  108. unset($v["password"]);
  109. unset($v["salt"]);
  110. unset($v["token"]);
  111. }
  112. return ["list" => $data, "count" => $count];
  113. }
  114. } else {
  115. return ["list" => [], "count" => 0];
  116. }
  117. }
  118. // 添加管理员
  119. public static function addAdmin($name, $account, $pwd, $groupId, $desc, $phone, $realname)
  120. {
  121. $passwordArr = $pwd;
  122. return Admin::add($name, $account, $passwordArr, $passwordArr, $groupId, $desc, $phone, $realname);
  123. }
  124. // 修改管理员
  125. public static function editAdmin($id, $name, $account, $pwd, $groupId, $desc, $phone, $realname)
  126. {
  127. $data = [];
  128. if (!empty($name)) {
  129. $data['name'] = $name;
  130. }
  131. if (!empty($account)) {
  132. $data['account'] = $account;
  133. }
  134. if (!empty($phone)) {
  135. $data['phone'] = $phone;
  136. }
  137. if (!empty($realname)) {
  138. $data['realname'] = $realname;
  139. }
  140. if (!empty($pwd)) {
  141. // $passwordArr = $pwd;
  142. // $data['password'] = $passwordArr["password"];
  143. // $data['salt'] = $passwordArr['salt'];
  144. $data['password'] = $pwd;
  145. $data['salt'] = $pwd;
  146. }
  147. if (!empty($groupId)) {
  148. $data['group_id'] = $groupId;
  149. }
  150. if (!empty($desc)) {
  151. $data['desc'] = $desc;
  152. }
  153. $adminData = Admin::findOne($id);
  154. Cache::forget($adminData["token"]);
  155. $data["token"] = "";
  156. return Admin::edit(["id" => $id], $data);
  157. }
  158. // 查看用户组
  159. public static function adminGroup()
  160. {
  161. return AdminGroup::getAll(['id','name']);
  162. }
  163. // 写入后台用户操作记录
  164. public static function writeAdminLog(array $data)
  165. {
  166. $mustDataKey = ['userId', 'name', 'path', 'method', 'queryParams', 'ip', 'title', 'userAgent'];
  167. $notCheck = ['queryParams'];
  168. $filter = array_filter(array_keys($data), function ($var) use ($mustDataKey, $notCheck, $data){
  169. return ( in_array($var, $mustDataKey) && isset($data[$var]) && !empty($data[$var]) ) || in_array($var, $notCheck);
  170. });
  171. if (count($filter) == count($mustDataKey)) {
  172. $content = '';
  173. if(!empty($data['queryParams'])){
  174. $params = $data['queryParams'];
  175. $replaceKeys = ['token', 'pwd', 'password'];
  176. foreach ($params as $key => &$value) {
  177. if(in_array($key, $replaceKeys)){
  178. $value = str_repeat('*', strlen($value) );
  179. }
  180. }
  181. $content = json_encode($params, 256);
  182. }
  183. $adminLog = new AdminLog;
  184. $adminLog->path = $data['path'];
  185. $adminLog->method = $data['method'];
  186. $adminLog->title = $data['title'];
  187. $adminLog->content = $content;
  188. $adminLog->admin_name = $data['name'];
  189. $adminLog->admin_id = $data['userId'];
  190. $adminLog->user_agent = $data['userAgent'];
  191. $adminLog->ip = $data['ip'];
  192. $adminLog->created_at = date("Y-m-d H:i:s", time());
  193. return $adminLog->save();
  194. }
  195. return false;
  196. }
  197. }