MemberService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 $where
  50. * @param array $field
  51. */
  52. public function getInfo($where, array $field=[])
  53. {
  54. if(is_array($where)){
  55. $info = $this->model->where($where)->select($field)->first();
  56. }else{
  57. $info = $this->model->getInfo((int)$where);
  58. }
  59. return $info;
  60. }
  61. /**
  62. * 用户注册
  63. * @param $params
  64. * @return bool
  65. */
  66. public function register($params)
  67. {
  68. // 检测账号是否存在
  69. if($this->checkExists('username', $params['username'])){
  70. $this->error = '2005';
  71. return false;
  72. }
  73. $username = isset($params['username'])? trim($params['username']) : '';
  74. $password = isset($params['password'])? trim($params['password']) : '123456';
  75. $avatar = isset($params['avatar'])? trim($params['avatar']) : '';
  76. $data = [
  77. 'username'=> $username,
  78. 'password'=> get_password($password . $username),
  79. 'mobile'=> isPhone($username)? $username : '',
  80. 'avatar'=> $avatar? $avatar : '',
  81. 'status'=> 1,
  82. 'mark'=> 1,
  83. 'create_time'=> time(),
  84. ];
  85. if($this->model->insert($data)){
  86. return true;
  87. }
  88. $this->error = 2007;
  89. return false;
  90. }
  91. /**
  92. * 用户登录
  93. * @param $params
  94. * @return array|false
  95. */
  96. public function login($params)
  97. {
  98. $username = isset($params['username'])? $params['username'] : '';
  99. $password = isset($params['password'])? $params['password'] : '';
  100. if(empty($username) || empty($password)){
  101. $this->error = 1013;
  102. return false;
  103. }
  104. // 用户验证
  105. $info = $this->model->getOne([['username', '=', $username]]);
  106. if (!$info) {
  107. $this->error = 2001;
  108. return false;
  109. }
  110. // 密码校验
  111. $password = get_password($password . $username);
  112. if ($password != $info['password']) {
  113. $this->error = 2002;
  114. return false;
  115. }
  116. // 使用状态校验
  117. if ($info['status'] != 1) {
  118. $this->error = 2009;
  119. return false;
  120. }
  121. // 设置日志标题
  122. ActionLogModel::setTitle("会员登录APP");
  123. ActionLogModel::record($info);
  124. // JWT生成token
  125. $jwt = new Jwt('jwt_app');
  126. $token = $jwt->getToken($info['id']);
  127. RedisService::set("stores:auths:info:{$info['id']}", $info, 5, 10);
  128. // 登录数据
  129. return [
  130. 'token' => $token,
  131. 'user_id' => $info['id'],
  132. 'user_type' => $info['user_type'],
  133. ];
  134. }
  135. }