LoginService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Helpers\Jwt;
  13. use App\Models\ActionLogModel;
  14. use App\Models\UserModel;
  15. use Carbon\Carbon;
  16. use Illuminate\Support\Facades\Session;
  17. use Illuminate\Support\Facades\Validator;
  18. use Gregwar\Captcha\CaptchaBuilder;
  19. use Gregwar\Captcha\PhraseBuilder;
  20. use Ramsey\Uuid\Uuid;
  21. /**
  22. * 登录服务类
  23. * @author wesmiler
  24. * @since 2020/11/10
  25. * Class LoginService
  26. * @package App\Services
  27. */
  28. class LoginService extends BaseService
  29. {
  30. /**
  31. * 构造函数
  32. * @author wesmiler
  33. * @since 2020/11/10
  34. * LoginService constructor.
  35. */
  36. public function __construct()
  37. {
  38. $this->model = new UserModel();
  39. }
  40. /**
  41. * 获取验证码
  42. * @author wesmiler
  43. * @since 2020/11/10
  44. */
  45. public function captcha()
  46. {
  47. $phrase = new PhraseBuilder;
  48. // 设置验证码位数
  49. $code = $phrase->build(4);
  50. // 生成验证码图片的Builder对象,配置相应属性
  51. $builder = new CaptchaBuilder($code, $phrase);
  52. // 设置背景颜色25,25,112
  53. $builder->setBackgroundColor(255, 255, 255);
  54. // 设置倾斜角度
  55. $builder->setMaxAngle(25);
  56. // 设置验证码后面最大行数
  57. $builder->setMaxBehindLines(10);
  58. // 设置验证码前面最大行数
  59. $builder->setMaxFrontLines(10);
  60. // 设置验证码颜色
  61. $builder->setTextColor(230, 81, 175);
  62. // 可以设置图片宽高及字体
  63. $builder->build($width = 165, $height = 45, $font = null);
  64. // 获取验证码的内容
  65. $phrase = $builder->getPhrase();
  66. // 把内容存入 cache,10分钟后过期
  67. $key = Uuid::uuid1()->toString();
  68. $this->model->setCache($key, $phrase, Carbon::now()->addMinutes(10));
  69. // 组装接口数据
  70. $data = [
  71. 'key' => $key,
  72. 'captcha' => $builder->inline(),
  73. ];
  74. return message("操作成功", true, $data);
  75. }
  76. /**
  77. * 系统登录
  78. * @return mixed
  79. * @since 2020/11/10
  80. * @author wesmiler
  81. */
  82. public function login()
  83. {
  84. // 参数
  85. $param = request()->all();
  86. // 用户名
  87. $username = trim($param['username']);
  88. // 密码
  89. $password = trim($param['password']);
  90. // 验证规则
  91. $rules = [
  92. 'username' => 'required|min:2|max:20',
  93. 'password' => 'required|min:6|max:20',
  94. 'captcha' => ['required'],
  95. ];
  96. // 规则描述
  97. $messages = [
  98. 'required' => ':attribute为必填项',
  99. 'min' => ':attribute长度不符合要求',
  100. 'captcha.required' => '验证码不能为空',
  101. ];
  102. // 验证
  103. $validator = Validator::make($param, $rules, $messages, [
  104. 'username' => '用户名称',
  105. 'password' => '登录密码'
  106. ]);
  107. if ($validator->fails()) {
  108. $errors = $validator->errors()->getMessages();
  109. foreach ($errors as $key => $value) {
  110. return message($value[0], false);
  111. }
  112. }
  113. // 验证码校验
  114. $key = trim($param['key']);
  115. $captcha = $this->model->getCache($key);
  116. if (strtolower($captcha) != strtolower($param['captcha'])) {
  117. return message("请输入正确的验证码", false);
  118. }
  119. // 用户验证
  120. $info = $this->model->getOne([
  121. ['username', '=', $username],
  122. ]);
  123. if (!$info) {
  124. return message('您的登录用户名不存在', false);
  125. }
  126. // 密码校验
  127. $password = get_password($password . $username);
  128. if ($password != $info['password']) {
  129. return message("您的登录密码不正确", false);
  130. }
  131. // 使用状态校验
  132. if ($info['status'] != 1) {
  133. return message("您的帐号已被禁用", false);
  134. }
  135. // 设置日志标题
  136. ActionLogModel::setTitle("登录系统");
  137. ActionLogModel::record();
  138. // JWT生成token
  139. $jwt = new Jwt('jwt_yh');
  140. $token = $jwt->getToken($info['id']);
  141. // 结果返回
  142. $result = [
  143. 'access_token' => $token,
  144. ];
  145. return message('登录成功', true, $result);
  146. }
  147. /**
  148. * 退出系统
  149. * @return array
  150. * @since 2020/11/12
  151. * @author wesmiler
  152. */
  153. public function logout()
  154. {
  155. // 清空SESSION值
  156. session()->put("userId", null);
  157. // 记录退出日志
  158. ActionLogModel::setTitle("注销系统");
  159. // 创建退出日志
  160. ActionLogModel::record();
  161. return message();
  162. }
  163. }