Register.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\user\controller;
  3. use think\Controller;
  4. use think\Lang;
  5. use think\Validate;
  6. class Register extends Controller
  7. {
  8. public function _initialize()
  9. {
  10. parent::_initialize();
  11. Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/login.lang.php');
  12. }
  13. /**
  14. * 验证密码
  15. * @return mixed
  16. */
  17. function checkPassword($password)
  18. {
  19. if (preg_match('/(?=^.{6,20}$)(?=.*\d)(?=.*[a-z])(?=.*[!@#$%^&*]).*$/', $password)) {
  20. return 1;
  21. } else {
  22. return 0;
  23. }
  24. }
  25. /**
  26. * 注册
  27. * @return mixed
  28. */
  29. public function index()
  30. {
  31. if (request()->isPost()) {
  32. $user_email = input('post.user_email');
  33. $user_password = input('post.user_password');
  34. $confirm_password = input('post.confirm_password');
  35. $user_phone = input('post.user_phone');
  36. $captcha = input('post.captcha');
  37. $ch_box = input('post.ch_box');
  38. $user_type = input('post.user_type');
  39. $http = input('post.http');
  40. // 用户类型 校验
  41. if (empty($user_type)) {
  42. //验证失败
  43. $this->error('未选择用户类型');
  44. }
  45. // // 协议 校验
  46. // if ($ch_box == false) {
  47. // //验证失败
  48. // $this->error('未勾选协议');
  49. // }
  50. //验证密码
  51. if ($user_password != $confirm_password) {
  52. //验证失败
  53. $this->error('密码不一致');
  54. }
  55. $data = array(
  56. 'user_email' => $user_email,
  57. 'user_password' => $user_password,
  58. 'user_phone' => $user_phone,
  59. 'captcha' => $captcha,
  60. );
  61. //验证数据 BEGIN
  62. $rule = [
  63. ['user_email', 'require|min:2', '帐号为必填|帐号长度至少为2位'],
  64. ['user_password', 'require|min:2', '密码为必填|帐号长度至少为6位'],
  65. ['captcha', 'require|min:3', '验证码为必填|帐号长度至少为3位'],
  66. ];
  67. $validate = new Validate($rule);
  68. $validate_result = $validate->check($data);
  69. if (!$validate_result) {
  70. $this->error($validate->getError());
  71. }
  72. //验证数据 END
  73. if (!captcha_check(input('post.captcha'))) {
  74. //验证失败
  75. $this->error('验证码错误');
  76. }
  77. $condition['user_email'] = $user_email;
  78. $user_info = db('user')->where($condition)->find();
  79. if (is_array($user_info) and !empty($user_info)) {
  80. $this->success('账户已存在');
  81. } else {
  82. $user_info = array(
  83. 'user_email' => $user_email,
  84. 'user_password' => md5($user_password),
  85. 'user_phone' => $user_phone,
  86. 'user_type' => $user_type,
  87. 'user_addTime' => TIMESTAMP,
  88. 'user_loginTime' => TIMESTAMP
  89. );
  90. $id = db('user')->insertGetId($user_info);
  91. $userinfo = array(
  92. 'user_id' => $id
  93. );
  94. db('userinfo')->insertGetId($userinfo);
  95. //设置 session
  96. session('user_id', $id);
  97. session('user_email', $user_info['user_email']);
  98. if(!empty($http)){
  99. header("location:$http");
  100. }else{
  101. return $this->redirect('User/Index/index');
  102. }
  103. }
  104. } else {
  105. if(!empty($_SERVER['HTTP_REFERER'])){
  106. $http = $_SERVER['HTTP_REFERER'];
  107. $this->assign('http', $http);
  108. }
  109. return $this->fetch();
  110. }
  111. }
  112. }
  113. ?>