RegisterValidate.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. return true;
  97. $smsDriver = new SmsDriver();
  98. $result = $smsDriver->verify($data['mobile'], $value);
  99. if($result) {
  100. return true;
  101. }
  102. return '验证码错误';
  103. }
  104. /**
  105. * @notes 检测注册配置
  106. * @param $value
  107. * @param $rue
  108. * @param $data
  109. * @return bool|string
  110. * @author Tab
  111. * @date 2021/8/25 14:33
  112. */
  113. public function checkConfig($value, $rue, $data)
  114. {
  115. $config = [
  116. 'register_way' => ConfigService::get('config', 'register_way', []),
  117. 'is_mobile_register_code' => ConfigService::get('config', 'is_mobile_register_code', YesNoEnum::YES)
  118. ];
  119. // 1-手机号注册
  120. if(!in_array(1, $config['register_way'])) {
  121. return '未开启手机号注册';
  122. }
  123. if($config['is_mobile_register_code'] && !isset($data['code'])) {
  124. return '请输入验证码';
  125. }
  126. return true;
  127. }
  128. /**
  129. * @notes 校验密码复杂度
  130. * @param $value
  131. * @param $rue
  132. * @param $data
  133. * @author Tab
  134. * @date 2021/12/10 15:06
  135. */
  136. public function checkComplexity($value, $rue, $data)
  137. {
  138. $lowerCase = range('a', 'z');
  139. $upperCase = range('A', 'Z');
  140. $numbers = range(0, 9);
  141. $cases = array_merge($lowerCase, $upperCase);
  142. $caseCount = 0;
  143. $numberCount = 0;
  144. $passwordArr = str_split(trim(($data['password'] . '')));
  145. foreach ($passwordArr as $value) {
  146. if (in_array($value, $numbers)) {
  147. $numberCount++;
  148. }
  149. if (in_array($value, $cases)) {
  150. $caseCount++;
  151. }
  152. }
  153. if ($numberCount >= 1 && $caseCount >= 1) {
  154. return true;
  155. }
  156. return '密码需包含数字和字母';
  157. }
  158. }