AdminUser.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php namespace App\Models\Manager;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Facades\Servers\Encrypts\AccessToken;
  5. /**
  6. *用户模型
  7. *
  8. */
  9. class AdminUser extends Model
  10. {
  11. use HasFactory;
  12. // 与模型关联的表名
  13. protected $table = 'admin';
  14. // 是否主动维护时间戳
  15. public $timestamps = false;
  16. protected $connection = 'mysql';
  17. // 定义时间戳字段名
  18. // const CREATED_AT = 'insert_time';
  19. // const UPDATED_AT = 'update_time';
  20. /**
  21. * 添加数据
  22. *
  23. */
  24. public function add($data)
  25. {
  26. // 时间
  27. $data['insert_time'] = time();
  28. $data['update_time'] = time();
  29. // 写入数据表
  30. $id = $this->query()->insertGetId($data);
  31. // 如果操作失败
  32. if( !$id ) return 0;
  33. // 更新缓存
  34. $this->getList(true);
  35. // 返回结果
  36. return $id;
  37. }
  38. /**
  39. * 添加数据
  40. *
  41. */
  42. public function edit($id,$data)
  43. {
  44. // 更新时间
  45. $data['update_time'] = time();
  46. // 写入数据表
  47. $result = $this->query()->where(['uid'=>$id])->update($data);
  48. // 如果操作失败
  49. if( !$result ) return 0;
  50. // 更新缓存
  51. $this->getList(true);
  52. // 返回结果
  53. return $id;
  54. }
  55. /**
  56. * 获取列表
  57. * @param Bool $force 是否强制更新
  58. *
  59. */
  60. public function getList($force = false)
  61. {
  62. // 结果数据
  63. $list = $force ? [] : cache('manager:admin:user:list');
  64. // 不存在数据
  65. if ( !$list ) {
  66. // 从数据库获取数据
  67. $data = $this->query()->get(['uid','username','phone','status','password','status','insert_time','update_time'])->toArray();
  68. // 循环处理数据
  69. $list = [];
  70. // 进行更新
  71. foreach ($data as $value) {
  72. // 重组数据
  73. $list[$value['uid']] = $value;
  74. }
  75. // 存起来
  76. cache(['manager:admin:user:list'=>$list]);
  77. }
  78. // 返回结果
  79. return $list;
  80. }
  81. /**
  82. * 获取配置平台对应的应用数据
  83. *
  84. * @param int 用户ID
  85. * @param string 指定字段
  86. *
  87. */
  88. public function getOne($id,$field='')
  89. {
  90. // 获取列表数据
  91. $list = $this->getList();
  92. // 获取数据
  93. $one = isset($list[$id]) ? $list[$id] : [];
  94. // 返回值
  95. return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
  96. }
  97. /**
  98. * 登录
  99. *
  100. * @param int 用户ID
  101. *
  102. */
  103. public function Login($uid,$module='manager'){
  104. // 组合数据
  105. $jwtData = ['uid'=>$uid,'type'=>$module,'expire'=>time()+3600*8];
  106. // 登录成功
  107. $token = AccessToken::encode($jwtData);
  108. // 如果异常的话
  109. if( isset($token['error']) ) return $token;
  110. // 登录成功
  111. cache([$module.':admin:access_token:'.$uid=>md5($token)],3600*8);
  112. // 返回结果
  113. return ['access_token_'.$module=>$token,'expire'=>3600*8];
  114. }
  115. /**
  116. * 退出登录
  117. *
  118. * @param int 用户ID
  119. *
  120. */
  121. public function LoginOut($uid,$module='manager'){
  122. // 删除缓存
  123. cache([$module.':admin:access_token:'.$uid=>null]);
  124. // 返回结果
  125. return true;
  126. }
  127. /**
  128. * 获取登录信息
  129. */
  130. public function getLogin($uid,$module='manager'){
  131. // 删除缓存
  132. $result = cache($module.':admin:access_token:'.$uid);
  133. // 返回结果
  134. return $result;
  135. }
  136. }