Users.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace catchAdmin\permissions\model;
  3. use catchAdmin\permissions\model\search\UserSearch;
  4. use catcher\base\CatchModel;
  5. use catcher\exceptions\FailedException;
  6. use catcher\Utils;
  7. use catchAdmin\permissions\model\DataRangScopeTrait;
  8. use think\facade\Db;
  9. class Users extends CatchModel
  10. {
  11. use HasRolesTrait;
  12. use HasJobsTrait;
  13. use UserSearch;
  14. //权限过滤
  15. use DataRangScopeTrait;
  16. protected $name = 'users';
  17. protected $field = [
  18. 'id', //
  19. 'username', // 用户名
  20. 'password', // 用户密码
  21. 'email', // 邮箱 登录
  22. 'avatar', // 头像
  23. 'remember_token',
  24. 'creator_id', // 创建者ID
  25. 'department_id', // 部门ID
  26. 'status', // 用户状态 1 正常 2 禁用
  27. 'last_login_ip', // 最后登录IP
  28. 'last_login_time', // 最后登录时间
  29. 'created_at', // 创建时间
  30. 'updated_at', // 更新时间
  31. 'deleted_at', // 删除状态,0未删除 >0 已删除
  32. 'area_id', // 区域ID
  33. 'phone', // 手机号
  34. 'wxmp_open_id',
  35. 'wx_open_id',
  36. 'wx_union_id',
  37. 'school_id',
  38. 'grade_id',
  39. 'class_id',
  40. 'passive_rfid',
  41. 'realname',
  42. 'idcard',
  43. 'active_rfid',
  44. 'active_rfid_code',
  45. 'rfid_expire_date',
  46. 'student_no',
  47. 'card_status',
  48. 'rules_id',
  49. 'parents_id',
  50. 'sex',
  51. 'age',
  52. 'online_time',
  53. 'alarm_status',
  54. 'last_station_mac',
  55. 'birthday',
  56. 'addr',
  57. 'classes',
  58. 'manage_classes',
  59. 'subjects',
  60. 'card_type',
  61. 'student_type',
  62. 'student_status',
  63. 'voice',
  64. 'voice_size',
  65. 'voice_time',
  66. 'imei',
  67. 'battery_level',
  68. 'accesskey',
  69. 'secretkey',
  70. 'remark',
  71. 'dept_name',
  72. 'asset_admin',
  73. 'wifi_macs',
  74. 'user_no'
  75. ];
  76. /**
  77. * set password
  78. *
  79. * @time 2019年12月07日
  80. * @param $value
  81. * @return false|string
  82. */
  83. public function setPasswordAttr($value)
  84. {
  85. return password_hash($value, PASSWORD_DEFAULT);
  86. }
  87. /**
  88. * 用户列表
  89. *
  90. * @time 2019年12月08日
  91. * @throws \think\db\exception\DbException
  92. * @return \think\Paginator
  93. */
  94. public function getList(): \think\Paginator
  95. {
  96. $no_display_roles = Db::table('roles')->whereIn('identify','personal,group_card_user,group_badge_user')->column('id');
  97. $user = request()->user();
  98. $res = $this->dataRange()
  99. ->withoutField(['updated_at'], true)
  100. ->catchSearch()
  101. ->alias('u')
  102. ->join('user_has_roles r','u.id=r.uid')
  103. // ->distinct(true)
  104. ->group('u.id')
  105. ->where('u.id','<>',1) //超级管理员账号不显示
  106. ->where('u.id','<>',$user->id) //不显示自己
  107. ->whereNotIn('r.role_id',$no_display_roles)
  108. ->catchLeftJoin(Department::class, 'id', 'department_id', ['department_name'])
  109. ->order($this->aliasField('id'), 'desc')
  110. ->paginate();
  111. // var_dump($this->getLastSql());
  112. return $res;
  113. }
  114. /**
  115. * 获取权限
  116. *
  117. * @time 2019年12月12日
  118. * @param $uid
  119. * @throws \think\db\exception\DataNotFoundException
  120. * @throws \think\db\exception\DbException
  121. * @throws \think\db\exception\ModelNotFoundException
  122. * @return array
  123. */
  124. public function getPermissionsBy($uid = 0): array
  125. {
  126. // 获取超级管理配置 超级管理员全部权限
  127. if ($uid == config('catch.permissions.super_admin_id')) {
  128. return Permissions::select()->column('id');
  129. }
  130. $roles = $uid ? $this->findBy($uid)->getRoles() : $this->getRoles();
  131. $permissionIds = [];
  132. foreach ($roles as $role) {
  133. $permissionIds = array_merge($permissionIds, $role->getPermissions()->column('id'));
  134. }
  135. return array_unique($permissionIds);
  136. }
  137. /**
  138. * 后台根据权限标识判断用户是否拥有某个权限
  139. * @param string $permission_mark
  140. * @return bool
  141. * @throws \think\db\exception\DataNotFoundException
  142. * @throws \think\db\exception\DbException
  143. * @throws \think\db\exception\ModelNotFoundException
  144. *
  145. * 用法 request()->user()->can('permission@create');
  146. */
  147. public function can($permission_mark)
  148. {
  149. // 超级管理员直接返回true
  150. if (Utils::isSuperAdmin()){
  151. return true;
  152. }
  153. // 查询当前用户的权限
  154. return in_array(
  155. Permissions::where('permission_mark',$permission_mark)->value('id') ? : 0,
  156. $this->getPermissionsBy()
  157. );
  158. }
  159. /**
  160. * 获取用户管理区域
  161. */
  162. public function getAreaIdBy()
  163. {
  164. return $this->where('id', request()->user()->id)->value('area_id');
  165. }
  166. /**
  167. * 根据部门获取用户
  168. */
  169. public function getUserByDepart($value)
  170. {
  171. return $this
  172. ->where('department_id', $value)
  173. ->field('id as value,username as text')
  174. ->select();
  175. }
  176. /**
  177. * 根据多个部门ID获取用户Ids
  178. */
  179. public function getUserByDepartIds($value)
  180. {
  181. return $this
  182. ->whereIn('department_id', $value)
  183. ->column('id');
  184. }
  185. /**
  186. * 获取推送用户(除家长、学生)
  187. */
  188. public function getPushUserList()
  189. {
  190. // 查出非家长、学生角色id
  191. $allowed_roles = Db::table('roles')->whereNotIn('identify',['personal','group_card_user','group_badge_user'])->column('id');
  192. $allowed_roles_text = join(',', $allowed_roles);
  193. // 查出有这些角色的用户信息
  194. $res = $this->dataRange()
  195. ->catchSearch()
  196. ->alias('u')
  197. ->field('u.*')
  198. ->where('u.id','<>',1) //超级管理员账号不显示
  199. ->distinct(true)
  200. ->join('user_has_roles uhr', "uhr.role_id in ({$allowed_roles_text}) and uhr.uid = u.id")
  201. ->select()
  202. ->toArray();
  203. // 非管理员,可能存在是管理员添加的账号,查不到自己,追加自己
  204. // var_dump($this->getLastSql());
  205. if (!Utils::isSuperAdmin()){
  206. $has_self = false;
  207. foreach ($res as $user) {
  208. if ($user['id'] == request()->user()->id) {
  209. $has_self = true;
  210. }
  211. }
  212. if (!$has_self) {
  213. array_push($res, request()->user());
  214. }
  215. }
  216. return $res;
  217. }
  218. }