LoginService.php 5.5 KB

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