LoginService.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 App\Services\RedisService;
  17. use Carbon\Carbon;
  18. use Earnp\GoogleAuthenticator\GoogleAuthenticator;
  19. use Illuminate\Support\Facades\Validator;
  20. use Gregwar\Captcha\CaptchaBuilder;
  21. use Gregwar\Captcha\PhraseBuilder;
  22. use Ramsey\Uuid\Uuid;
  23. /**
  24. * 登录服务类
  25. * @author laravel开发员
  26. * @since 2020/11/10
  27. * Class LoginService
  28. * @package App\Services\Common
  29. */
  30. class LoginService extends BaseService
  31. {
  32. /**
  33. * 构造函数
  34. * @author laravel开发员
  35. * @since 2020/11/10
  36. * LoginService constructor.
  37. */
  38. public function __construct()
  39. {
  40. $this->model = new UserModel();
  41. }
  42. /**
  43. * 获取验证码
  44. * @author laravel开发员
  45. * @since 2020/11/10
  46. */
  47. public function captcha()
  48. {
  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. }
  78. /**
  79. * 系统登录
  80. * @return mixed
  81. * @since 2020/11/10
  82. * @author laravel开发员
  83. */
  84. public function login()
  85. {
  86. // 参数
  87. $param = request()->all();
  88. // 用户名
  89. $username = trim($param['username']);
  90. // 密码
  91. $password = trim($param['password']);
  92. // 验证规则
  93. $rules = [
  94. 'username' => 'required|min:2|max:20',
  95. 'password' => 'required|min:6|max:20',
  96. 'captcha' => ['required'],
  97. ];
  98. // 规则描述
  99. $messages = [
  100. 'required' => ':attribute为必填项',
  101. 'min' => ':attribute长度不符合要求',
  102. 'captcha.required' => '验证码不能为空',
  103. ];
  104. // 验证
  105. $validator = Validator::make($param, $rules, $messages, [
  106. 'username' => '用户名称',
  107. 'password' => '登录密码'
  108. ]);
  109. if ($validator->fails()) {
  110. $errors = $validator->errors()->getMessages();
  111. foreach ($errors as $key => $value) {
  112. return message($value[0], false);
  113. }
  114. }
  115. // 验证码校验
  116. $key = trim($param['key']);
  117. $captcha = $this->model->getCache($key);
  118. if (strtolower($captcha) != strtolower($param['captcha'])) {
  119. return message("请输入正确的验证码", false);
  120. }
  121. // 用户验证
  122. $info = $this->model->getOne([
  123. ['username', '=', $username],
  124. ]);
  125. if (!$info) {
  126. return message('您的登录用户名不存在', false);
  127. }
  128. // 密码校验
  129. $password = get_password($password . $username);
  130. if ($password != $info['password']) {
  131. return message("您的登录密码不正确", false);
  132. }
  133. // 使用状态校验
  134. if ($info['status'] != 1) {
  135. return message("您的帐号已被禁用", false);
  136. }
  137. // 谷歌验证码验证
  138. $googleBind = isset($info['google_bind'])? intval($info['google_bind']) : 0;
  139. $googleKey = isset($info['google_key'])? trim($info['google_key']) : '';
  140. $googleCode = isset($param['google_code'])? trim($param['google_code']) : '';
  141. if($googleBind == 1 && $googleKey){
  142. if(empty($googleCode)){
  143. return message("请输入谷歌验证码", false);
  144. }
  145. if (!GoogleAuthenticator::CheckCode($googleKey,$googleCode)){
  146. return message("谷歌验证码错误", false);
  147. }
  148. }
  149. // 设置日志标题
  150. ActionLogModel::setTitle("登录系统");
  151. ActionLogModel::record();
  152. // JWT生成token
  153. $jwt = new Jwt();
  154. $token = $jwt->getToken($info['id']);
  155. // 结果返回
  156. $result = [
  157. 'access_token' => $token,
  158. ];
  159. return message('登录成功', true, $result);
  160. }
  161. /**
  162. * 退出系统
  163. * @return array
  164. * @since 2020/11/12
  165. * @author laravel开发员
  166. */
  167. public function logout()
  168. {
  169. // 清空SESSION值
  170. session()->put("userId", null);
  171. // 记录退出日志
  172. ActionLogModel::setTitle("注销系统");
  173. // 创建退出日志
  174. ActionLogModel::record();
  175. return message();
  176. }
  177. /**
  178. * 验证账号
  179. * @return array
  180. */
  181. public function checkAccount()
  182. {
  183. // 获取参数
  184. $param = request()->all();
  185. $username = isset($param['username'])? trim($param['username']) : '';
  186. if(empty($username)){
  187. return message('请填写登录账号', false);
  188. }
  189. $cacheKey = "caches:user:checkAccount_".md5($username);
  190. if($data = RedisService::get($cacheKey)){
  191. return message(1010, true, $data);
  192. }
  193. $data = UserModel::where(['username'=>$username])->value('google_bind');
  194. if($data){
  195. RedisService::set($cacheKey, $data, rand(5, 10));
  196. }
  197. return message(1010, true, $data);
  198. }
  199. }