MemberService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Helpers\Jwt;
  13. use App\Models\ActionLogModel;
  14. use App\Models\MemberModel;
  15. use App\Models\UserModel;
  16. use App\Services\BaseService;
  17. use App\Services\RedisService;
  18. /**
  19. * 会员-服务类
  20. * Class MemberService
  21. * @package App\Services\Api
  22. */
  23. class MemberService extends BaseService
  24. {
  25. // 静态对象
  26. protected static $instance = null;
  27. /**
  28. * 构造函数
  29. * @since 2020/11/10
  30. * LoginService constructor.
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new MemberModel();
  35. }
  36. /**
  37. * 静态入口
  38. * @return static|null
  39. */
  40. public static function make()
  41. {
  42. if(!self::$instance){
  43. self::$instance = (new static());
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * 用户注册
  49. * @param $params
  50. * @return bool
  51. */
  52. public function register($params)
  53. {
  54. // 检测账号是否存在
  55. if($this->checkExists('username', $params['username'])){
  56. $this->error = '2005';
  57. return false;
  58. }
  59. $username = isset($params['username'])? trim($params['username']) : '';
  60. $password = isset($params['password'])? trim($params['password']) : '123456';
  61. $avatar = isset($params['avatar'])? trim($params['avatar']) : '';
  62. $data = [
  63. 'username'=> $username,
  64. 'password'=> get_password($password . $username),
  65. 'mobile'=> isPhone($username)? $username : '',
  66. 'avatar'=> $avatar? $avatar : '',
  67. 'status'=> 1,
  68. 'mark'=> 1,
  69. 'create_time'=> time(),
  70. ];
  71. if(!$this->model->insert($data)){
  72. return true;
  73. }
  74. $this->error = 2007;
  75. return false;
  76. }
  77. /**
  78. * 用户登录
  79. * @param $params
  80. * @return array|false
  81. */
  82. public function login($params)
  83. {
  84. $username = isset($params['username'])? $params['username'] : '';
  85. $password = isset($params['password'])? $params['password'] : '';
  86. if(empty($username) || empty($password)){
  87. $this->error = 1013;
  88. return false;
  89. }
  90. // 用户验证
  91. $info = $this->model->getOne([['username', '=', $username]]);
  92. if (!$info) {
  93. $this->error = 2001;
  94. return false;
  95. }
  96. // 密码校验
  97. $password = get_password($password . $username);
  98. if ($password != $info['password']) {
  99. $this->error = 2002;
  100. return false;
  101. }
  102. // 使用状态校验
  103. if ($info['status'] != 1) {
  104. $this->error = 2009;
  105. return false;
  106. }
  107. // 设置日志标题
  108. ActionLogModel::setTitle("会员登录APP");
  109. ActionLogModel::record($info);
  110. // JWT生成token
  111. $jwt = new Jwt('jwt_app');
  112. $token = $jwt->getToken($info['id']);
  113. RedisService::set("stores:auths:info:{$info['id']}", $info, 5, 10);
  114. // 登录数据
  115. return [
  116. 'token' => $token,
  117. 'id' => $info['id'],
  118. 'user_type' => $info['user_type'],
  119. ];
  120. }
  121. }