Admin.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model\admin;
  4. use library\basic\BaseModel;
  5. use library\traits\JwtAuthModelTrait;
  6. use library\traits\ModelTrait;
  7. use think\facade\Cache;
  8. use think\Model;
  9. /**
  10. * @mixin \think\Model
  11. */
  12. class Admin extends BaseModel
  13. {
  14. use ModelTrait;
  15. use JwtAuthModelTrait;
  16. private $sassId = 0;
  17. public function setSassId($sassid) {
  18. $this->sassId = $sassid;
  19. }
  20. /**
  21. * 管理注册写入
  22. * @param $post
  23. */
  24. public static function saveAdmin($post) {
  25. $save = ['name' => $post['name'],
  26. 'role_id' => $post['role_id'],
  27. 'avatar' => $post['avatar'],
  28. 'status' => $post['status'],
  29. 'sassid' => $post['sassid']
  30. ];
  31. if(empty($post['sassid'])){
  32. return self::setErrorInfo('站点不存在');
  33. }
  34. //是否密码写入
  35. if(!empty($post['password'])) {
  36. $save['password'] = password_hash($post['password'],PASSWORD_BCRYPT);
  37. }
  38. if($post['id']) {
  39. $info = self::where("username",$post['username'])->where('sassid',$save['sassid'])->find();
  40. if(!empty($info) && $info['id'] != $post['id'] ) {
  41. return self::setErrorInfo('登录的账号已存在');
  42. }
  43. //判断密码
  44. if(empty($save['password'])) {
  45. unset($post['password']);
  46. }
  47. self::where('id',$post['id'])->save($save);
  48. } else {
  49. $save['time'] = time();
  50. $save['last_time'] = time();
  51. $save['username'] = $post['username'];
  52. $count = self::where("username",$post['username'])->where('sassid',$save['sassid'])->count();
  53. if($count > 0) {
  54. return self::setErrorInfo('登录的账号已存在');
  55. }
  56. if(empty($save['password'])) {
  57. return self::setErrorInfo('请输入登录密码');
  58. }
  59. self::insert($save);
  60. }
  61. return true;
  62. }
  63. /**
  64. * 后台管理人员登录
  65. * @param $username
  66. * @param $pwd
  67. */
  68. public static function login($username, $pwd,$sassid){
  69. $adminInfo = self::get(compact('username','sassid'));
  70. if(empty($adminInfo)) return self::setErrorInfo('登录的账号不存在');
  71. if (!$adminInfo['status']) return self::setErrorInfo('账户以后被停用!');
  72. if (!password_verify($pwd, $adminInfo['password'])) return self::setErrorInfo('账号或密码错误,请重新输入');
  73. //发生事件
  74. event('AdminLoginAfter', [$adminInfo]);
  75. return $adminInfo;
  76. }
  77. /**
  78. * 验证密码
  79. * @param $id 管理ID
  80. * @param $pwd 旧密码
  81. * @return bool
  82. */
  83. public static function checkPassowrd($id,$pwd) {
  84. $adminInfo = self::get(compact('id'));
  85. if(empty($adminInfo)) return self::setErrorInfo('登录的账号不存在');
  86. if (!password_verify($pwd, $adminInfo['password'])){
  87. return false;
  88. }
  89. return true;
  90. }
  91. /**
  92. * 生成token存放令牌 | 存放redis
  93. * @param Admin $adminInfo
  94. * @param $type
  95. * @return array|bool
  96. */
  97. public static function createToken(Admin $adminInfo,$type)
  98. {
  99. $tokenInfo = $adminInfo->getToken($type, []);
  100. $exp = (int)bcadd((string)($tokenInfo['params']['exp'] - $tokenInfo['params']['iat']), "60", 0);
  101. try {
  102. $res = Cache::store('redis')->set($tokenInfo['token'], ['token' => $tokenInfo['token'], 'exp' => $exp], (int)$exp);
  103. } catch (\Throwable $e) {
  104. return self::setErrorInfo('保存token失败');
  105. }
  106. return $tokenInfo;
  107. }
  108. }