Admin.php 3.7 KB

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