RegisterValidate.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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\enum\YesNoEnum;
  21. use app\common\model\User;
  22. use app\common\service\ConfigService;
  23. use app\common\service\sms\SmsDriver;
  24. use app\common\validate\BaseValidate;
  25. /**
  26. * 注册验证器
  27. * Class RegisterValidate
  28. * @package app\shopapi\validate
  29. */
  30. class RegisterValidate extends BaseValidate
  31. {
  32. protected $rule = [
  33. 'register_source' => 'require',
  34. 'mobile' => 'require|mobile',
  35. 'code' => 'checkCode',
  36. 'password' => 'require|length:6,25|alphaDash|checkComplexity',
  37. 'password_confirm' => 'require|confirm'
  38. ];
  39. protected $message = [
  40. 'register_source.require' => '注册来源参数缺失',
  41. 'mobile.require' => '请输入手机号',
  42. 'mobile.mobile' => '无效的手机号',
  43. 'password.require' => '请输入密码',
  44. 'password.length' => '密码须在6-25位之间',
  45. 'password.alphaDash' => '密码须为字母数字下划线或破折号',
  46. 'password_confirm.require' => '请确认密码',
  47. 'password_confirm.confirm' => '两次输入的密码不一致'
  48. ];
  49. /**
  50. * @notes 注册发送验证码场景
  51. * @return RegisterValidate
  52. * @author Tab
  53. * @date 2021/8/25 11:18
  54. */
  55. public function sceneCaptcha()
  56. {
  57. return $this->only(['mobile']);
  58. }
  59. /**
  60. * @notes 用户注册
  61. * @return RegisterValidate
  62. * @author Tab
  63. * @date 2021/8/25 11:32
  64. */
  65. public function sceneRegister()
  66. {
  67. return $this->only(['register_source', 'mobile', 'code', 'password', 'password_confirm'])
  68. ->append('mobile', 'checkMobile|checkConfig');
  69. }
  70. /**
  71. * @notes 校验手机号是否已注册
  72. * @param $value
  73. * @return bool|string
  74. * @author Tab
  75. * @date 2021/8/25 11:34
  76. */
  77. public function checkMobile($value)
  78. {
  79. $user = User::where('mobile', $value)->findOrEmpty();
  80. if(!$user->isEmpty()) {
  81. return '该手机号已被注册';
  82. }
  83. return true;
  84. }
  85. /**
  86. * @notes 校验验证码
  87. * @param $value
  88. * @param $rule
  89. * @param $data
  90. * @return bool|string
  91. * @author Tab
  92. * @date 2021/8/25 11:41
  93. */
  94. public function checkCode($value, $rule, $data)
  95. {
  96. $smsDriver = new SmsDriver();
  97. $result = $smsDriver->verify($data['mobile'], $value);
  98. if($result) {
  99. return true;
  100. }
  101. return '验证码错误';
  102. }
  103. /**
  104. * @notes 检测注册配置
  105. * @param $value
  106. * @param $rue
  107. * @param $data
  108. * @return bool|string
  109. * @author Tab
  110. * @date 2021/8/25 14:33
  111. */
  112. public function checkConfig($value, $rue, $data)
  113. {
  114. $config = [
  115. 'register_way' => ConfigService::get('config', 'register_way', []),
  116. 'is_mobile_register_code' => ConfigService::get('config', 'is_mobile_register_code', YesNoEnum::YES)
  117. ];
  118. // 1-手机号注册
  119. if(!in_array(1, $config['register_way'])) {
  120. return '未开启手机号注册';
  121. }
  122. if($config['is_mobile_register_code'] && !isset($data['code'])) {
  123. return '请输入验证码';
  124. }
  125. return true;
  126. }
  127. /**
  128. * @notes 校验密码复杂度
  129. * @param $value
  130. * @param $rue
  131. * @param $data
  132. * @author Tab
  133. * @date 2021/12/10 15:06
  134. */
  135. public function checkComplexity($value, $rue, $data)
  136. {
  137. $lowerCase = range('a', 'z');
  138. $upperCase = range('A', 'Z');
  139. $numbers = range(0, 9);
  140. $cases = array_merge($lowerCase, $upperCase);
  141. $caseCount = 0;
  142. $numberCount = 0;
  143. $passwordArr = str_split(trim(($data['password'] . '')));
  144. foreach ($passwordArr as $value) {
  145. if (in_array($value, $numbers)) {
  146. $numberCount++;
  147. }
  148. if (in_array($value, $cases)) {
  149. $caseCount++;
  150. }
  151. }
  152. if ($numberCount >= 1 && $caseCount >= 1) {
  153. return true;
  154. }
  155. return '密码需包含数字和字母';
  156. }
  157. }