LoginValidate.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\shopapi\validate;
  20. use app\common\cache\UserAccountSafeCache;
  21. use app\common\enum\LoginEnum;
  22. use app\common\enum\UserTerminalEnum;
  23. use app\common\enum\YesNoEnum;
  24. use app\common\model\User;
  25. use app\common\service\ConfigService;
  26. use app\common\service\sms\SmsDriver;
  27. use app\common\validate\BaseValidate;
  28. use think\facade\Config;
  29. class LoginValidate extends BaseValidate
  30. {
  31. protected $rule = [
  32. 'terminal' => 'require|in:' . UserTerminalEnum::WECHAT_MMP . ',' . UserTerminalEnum::WECHAT_OA . ','
  33. . UserTerminalEnum::H5 . ',' . UserTerminalEnum::PC . ','
  34. . UserTerminalEnum::IOS . ',' . UserTerminalEnum::ANDROID . ',' . UserTerminalEnum::TOUTIAO,
  35. 'scene' => 'require|in:1,2|checkConfig',
  36. 'account' => 'require',
  37. 'mobile' => 'require|mobile'
  38. ];
  39. protected $message = [
  40. 'terminal.require' => '终端参数缺失',
  41. 'terminal.in' => '终端参数状态值不正确',
  42. 'scene.require' => '场景不能为空',
  43. 'scene.in' => '场景值错误',
  44. 'account.require' => '请输入账号',
  45. 'password.require' => '请输入密码',
  46. 'mobile.require' => '请输入手机号',
  47. 'mobile.mobile' => '无效的手机号',
  48. ];
  49. function sceneCheckMobileUser()
  50. {
  51. return $this->only([ 'mobile' ]);
  52. }
  53. /**
  54. * @notes 账号密码/手机号密码/手机号验证码登录场景
  55. * @return LoginValidate
  56. * @author Tab
  57. * @date 2021/8/25 15:53
  58. */
  59. public function sceneAccount()
  60. {
  61. return $this->remove('mobile', 'require|mobile');
  62. }
  63. /**
  64. * @notes 发送登录验证码
  65. * @return LoginValidate
  66. * @author Tab
  67. * @date 2021/8/25 15:48
  68. */
  69. public function sceneCaptcha()
  70. {
  71. return $this->only(['mobile']);
  72. }
  73. /**
  74. * @notes 密码验证
  75. * @param $password
  76. * @param $other
  77. * @param $data
  78. * @return bool|string
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. * @author 令狐冲
  83. * @date 2021/7/2 14:00
  84. */
  85. public function checkPassword($password, $other, $data)
  86. {
  87. //后台账号安全机制,连续输错后锁定,防止账号密码暴力破解
  88. $userAccountSafeCache = new UserAccountSafeCache();
  89. if (!$userAccountSafeCache->isSafe()) {
  90. return '密码连续' . $userAccountSafeCache->count . '次输入错误,请' . $userAccountSafeCache->minute . '分钟后重试';
  91. }
  92. $where = [];
  93. if($data['scene'] == LoginEnum::MOBILE_PASSWORD || $data['scene'] == LoginEnum::MOBILE_CAPTCHA) {
  94. // 手机号密码登录
  95. $where = ['mobile' => $data['account']];
  96. }
  97. $userInfo = User::where($where)
  98. ->field(['password,disable'])
  99. ->find();
  100. if (empty($userInfo)) {
  101. return '用户不存在';
  102. }
  103. if ($userInfo['disable'] === YesNoEnum::YES) {
  104. return '账号被冻结,请联系客服。';
  105. }
  106. if (empty($userInfo['password'])) {
  107. $userAccountSafeCache->record();
  108. return '用户不存在';
  109. }
  110. $passwordSalt = Config::get('project.unique_identification');
  111. if ($userInfo['password'] !== create_password($password, $passwordSalt)) {
  112. $userAccountSafeCache->record();
  113. return '密码错误';
  114. }
  115. $userAccountSafeCache->relieve();
  116. return true;
  117. }
  118. /**
  119. * @notes 校验登录设置
  120. * @return bool|string
  121. * @author Tab
  122. * @date 2021/8/25 15:14
  123. */
  124. public function checkConfig($scene, $rule, $data)
  125. {
  126. $config = ConfigService::get('config', 'login_way', []);
  127. if(!in_array($scene, $config)) {
  128. return '不支持的登录方式';
  129. }
  130. if(($scene == LoginEnum::MOBILE_PASSWORD) && !isset($data['password'])) {
  131. return '请输入密码';
  132. }
  133. if(($scene == LoginEnum::MOBILE_PASSWORD)) {
  134. return $this->checkPassword($data['password'], [], $data);
  135. }
  136. if($scene == LoginEnum::MOBILE_CAPTCHA && !isset($data['code'])) {
  137. return '请输入手机验证码';
  138. }
  139. if($scene == LoginEnum::MOBILE_CAPTCHA) {
  140. return $this->checkCode($data['code'], [], $data);
  141. }
  142. return true;
  143. }
  144. /**
  145. * @notes 校验验证码
  146. * @param $code
  147. * @param $rule
  148. * @param $data
  149. * @return bool|string
  150. * @author Tab
  151. * @date 2021/8/25 15:43
  152. */
  153. public function checkCode($code, $rule, $data)
  154. {
  155. return true;
  156. $smsDriver = new SmsDriver();
  157. $result = $smsDriver->verify($data['account'], $code);
  158. if($result) {
  159. return true;
  160. }
  161. return '验证码错误';
  162. }
  163. }