UserModel.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace addons\exam\model;
  3. use addons\exam\enum\UserStatus;
  4. use app\common\library\Auth;
  5. use fast\Random;
  6. class UserModel extends BaseModel
  7. {
  8. // 表名
  9. protected $name = 'user';
  10. // 开启自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'int';
  12. // 定义时间戳字段名
  13. protected $createTime = 'createtime';
  14. protected $updateTime = 'updatetime';
  15. // /**
  16. // * 登录并返回token
  17. // * @param $username
  18. // * @param $password
  19. // * @param $user_type
  20. // * @return string
  21. // */
  22. // public static function login($username, $password, $user_type)
  23. // {
  24. // $username = "{$user_type}-$username";
  25. // if (!$user = self::where('username', $username)->find()) {
  26. // api_fail('登录账号或密码错误');
  27. // }
  28. // if ($user['password'] != Auth::instance()->getEncryptPassword($password, $user['salt'])) {
  29. // api_fail('登录账号或密码错误');
  30. // }
  31. //
  32. // Auth::instance()->direct($user['id']);
  33. // return Auth::instance()->getToken();
  34. // }
  35. /**
  36. * 当前登录用户信息
  37. * @return Auth|null
  38. */
  39. public static function info()
  40. {
  41. if (Auth::instance()->isLogin()) {
  42. return Auth::instance();
  43. }
  44. return null;
  45. }
  46. /**
  47. * 快速注册用户
  48. * @param string $username
  49. * @param string $nickname
  50. * @param string $avatar
  51. * @param int $gender
  52. * @param string $password
  53. * @param string $mobile
  54. * @return UserModel
  55. */
  56. public static function fastRegister(string $username, string $nickname = '', string $avatar = '', int $gender = 0, string $password = '', string $mobile = '')
  57. {
  58. if (self::where('username', $username)->count()) {
  59. fail('该账号已被注册');
  60. }
  61. // 不严格要求可以去除
  62. // if ($mobile && self::where('mobile', $mobile)->count()) {
  63. // fail('该手机号码已被绑定');
  64. // }
  65. $salt = Random::alnum();
  66. return self::create([
  67. 'username' => $username,
  68. 'mobile' => $mobile,
  69. 'email' => $mobile . '@qq.com',
  70. 'salt' => $salt,
  71. 'password' => Auth::instance()->getEncryptPassword($password ?: $username, $salt),
  72. 'nickname' => $nickname,
  73. 'avatar' => $avatar,
  74. 'gender' => $gender,
  75. 'status' => UserStatus::NORMAL,
  76. 'logintime' => time(),
  77. ]);
  78. }
  79. /**
  80. * 是否必须绑定手机号
  81. * @param $user
  82. * @return void
  83. */
  84. public static function isMustBindMobile($user)
  85. {
  86. $system_config = getConfig('system_config');
  87. $bind_mobile = $system_config['bind_mobile'] ?? 0;
  88. if ($bind_mobile == 2 && (!isset($user['mobile']) || !$user['mobile'])) {
  89. fail('请先绑定手机号');
  90. }
  91. }
  92. /************************** 关联关系 **************************/
  93. public function agent()
  94. {
  95. return $this->hasOne(\app\admin\model\juhepay\AgentModel::class, 'user_id', 'id');
  96. }
  97. public function shop()
  98. {
  99. return $this->hasOne(\app\admin\model\juhepay\ShopModel::class, 'user_id', 'id');
  100. }
  101. }