RegisterValidate.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. // return $this->only(['register_source', 'mobile', 'password', 'password_confirm'])
  70. // ->append('mobile', 'checkMobile|checkConfig');
  71. }
  72. /**
  73. * @notes 校验手机号是否已注册
  74. * @param $value
  75. * @return bool|string
  76. * @author Tab
  77. * @date 2021/8/25 11:34
  78. */
  79. public function checkMobile($value)
  80. {
  81. $user = User::where('mobile', $value)->findOrEmpty();
  82. if(!$user->isEmpty()) {
  83. return '该手机号已被注册';
  84. }
  85. return true;
  86. }
  87. /**
  88. * @notes 校验验证码
  89. * @param $value
  90. * @param $rule
  91. * @param $data
  92. * @return bool|string
  93. * @author Tab
  94. * @date 2021/8/25 11:41
  95. */
  96. public function checkCode($value, $rule, $data)
  97. {
  98. return true;
  99. $smsDriver = new SmsDriver();
  100. $result = $smsDriver->verify($data['mobile'], $value);
  101. if($result) {
  102. return true;
  103. }
  104. return '验证码错误';
  105. }
  106. /**
  107. * @notes 检测注册配置
  108. * @param $value
  109. * @param $rue
  110. * @param $data
  111. * @return bool|string
  112. * @author Tab
  113. * @date 2021/8/25 14:33
  114. */
  115. public function checkConfig($value, $rue, $data)
  116. {
  117. $config = [
  118. 'register_way' => ConfigService::get('config', 'register_way', []),
  119. 'is_mobile_register_code' => ConfigService::get('config', 'is_mobile_register_code', YesNoEnum::YES)
  120. ];
  121. // 1-手机号注册
  122. if(!in_array(1, $config['register_way'])) {
  123. return '未开启手机号注册';
  124. }
  125. if($config['is_mobile_register_code'] && !isset($data['code'])) {
  126. return '请输入验证码';
  127. }
  128. return true;
  129. }
  130. /**
  131. * @notes 校验密码复杂度
  132. * @param $value
  133. * @param $rue
  134. * @param $data
  135. * @author Tab
  136. * @date 2021/12/10 15:06
  137. */
  138. public function checkComplexity($value, $rue, $data)
  139. {
  140. $lowerCase = range('a', 'z');
  141. $upperCase = range('A', 'Z');
  142. $numbers = range(0, 9);
  143. $cases = array_merge($lowerCase, $upperCase);
  144. $caseCount = 0;
  145. $numberCount = 0;
  146. $passwordArr = str_split(trim(($data['password'] . '')));
  147. foreach ($passwordArr as $value) {
  148. if (in_array($value, $numbers)) {
  149. $numberCount++;
  150. }
  151. if (in_array($value, $cases)) {
  152. $caseCount++;
  153. }
  154. }
  155. if ($numberCount >= 1 && $caseCount >= 1) {
  156. return true;
  157. }
  158. return '密码需包含数字和字母';
  159. }
  160. }