Register.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // 用户类型 校验
  40. if (empty($user_type)) {
  41. //验证失败
  42. $this->error('未选择用户类型');
  43. }
  44. // // 协议 校验
  45. // if ($ch_box == false) {
  46. // //验证失败
  47. // $this->error('未勾选协议');
  48. // }
  49. //验证密码
  50. if ($user_password != $confirm_password) {
  51. //验证失败
  52. $this->error('密码不一致');
  53. }
  54. $data = array(
  55. 'user_email' => $user_email,
  56. 'user_password' => $user_password,
  57. 'user_phone' => $user_phone,
  58. 'captcha' => $captcha,
  59. );
  60. //验证数据 BEGIN
  61. $rule = [
  62. ['user_email', 'require|min:2', '帐号为必填|帐号长度至少为2位'],
  63. ['user_password', 'require|min:2', '密码为必填|帐号长度至少为6位'],
  64. ['captcha', 'require|min:3', '验证码为必填|帐号长度至少为3位'],
  65. ];
  66. $validate = new Validate($rule);
  67. $validate_result = $validate->check($data);
  68. if (!$validate_result) {
  69. $this->error($validate->getError());
  70. }
  71. //验证数据 END
  72. if (!captcha_check(input('post.captcha'))) {
  73. //验证失败
  74. $this->error('验证码错误');
  75. }
  76. $condition['user_email'] = $user_email;
  77. $user_info = db('user')->where($condition)->find();
  78. if (is_array($user_info) and !empty($user_info)) {
  79. $this->success('账户已存在');
  80. } else {
  81. $user_info = array(
  82. 'user_email' => $user_email,
  83. 'user_password' => md5($user_password),
  84. 'user_phone' => $user_phone,
  85. 'user_type' => $user_type,
  86. 'user_addTime' => TIMESTAMP,
  87. 'user_loginTime' => TIMESTAMP
  88. );
  89. $id = db('user')->insertGetId($user_info);
  90. $userinfo = array(
  91. 'user_id' => $id
  92. );
  93. db('userinfo')->insertGetId($userinfo);
  94. //设置 session
  95. session('user_id', $id);
  96. session('user_email', $user_info['user_email']);
  97. return $this->redirect('User/Index/index');
  98. }
  99. } else {
  100. return $this->fetch();
  101. }
  102. }
  103. }
  104. ?>