User.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\admin\model\admin;
  13. use app\common\model\admin\User as UserModel;
  14. use app\admin\service\admin\User as AdminUserService;
  15. /**
  16. * 超管后台用户模型
  17. * Class User
  18. * @package app\admin\model\admin
  19. */
  20. class User extends UserModel
  21. {
  22. /**
  23. * 隐藏的字段
  24. * @var array
  25. */
  26. protected $hidden = [
  27. 'password',
  28. ];
  29. // 用户登录token
  30. private $token;
  31. /**
  32. * 超管后台用户登录
  33. * @param array $data
  34. * @return array|bool|null|\think\Model
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\DbException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. */
  39. public function login(array $data)
  40. {
  41. // 验证用户名密码是否正确
  42. if (!$user = $this->getUserInfoByLogin($data)) {
  43. $this->error = '登录失败, 用户名或密码错误';
  44. return false;
  45. }
  46. // 记录登录状态, 并记录token
  47. $this->token = AdminUserService::login($user->toArray());
  48. return $user;
  49. }
  50. /**
  51. * 返回生成的token
  52. * @return mixed
  53. */
  54. public function getToken()
  55. {
  56. return $this->token;
  57. }
  58. /**
  59. * 获取登录的用户信息
  60. * @param $data
  61. * @return array|false|\think\Model
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. private function getUserInfoByLogin(array $data)
  67. {
  68. // 用户信息
  69. $useInfo = static::withoutGlobalScope()
  70. ->where(['user_name' => $data['username']])
  71. ->find();
  72. if (empty($useInfo)) return false;
  73. // 验证密码是否正确
  74. if (!password_verify($data['password'], $useInfo['password'])) {
  75. return false;
  76. }
  77. return $useInfo;
  78. }
  79. /**
  80. * 更新当前管理员信息
  81. * @param $data
  82. * @return bool
  83. */
  84. public function renew(array $data)
  85. {
  86. if ($data['password'] !== $data['password_confirm']) {
  87. $this->error = '确认密码不正确';
  88. return false;
  89. }
  90. // 更新管理员信息
  91. if ($this->save([
  92. 'user_name' => $data['user_name'],
  93. 'password' => encryption_hash($data['password']),
  94. ]) === false) {
  95. return false;
  96. }
  97. // 更新登录信息
  98. AdminUserService::update($this->toArray());
  99. return true;
  100. }
  101. }