Register.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. public function index()
  18. {
  19. if (request()->isPost()) {
  20. $user_email = input('post.user_email');
  21. $user_password = input('post.user_password');
  22. $confirm_password = input('post.confirm_password');
  23. $user_phone = input('post.user_phone');
  24. $captcha = input('post.captcha');
  25. //验证密码
  26. if ($user_password != $confirm_password) {
  27. //验证失败
  28. $this->error('密码不一致');
  29. }
  30. $data = array(
  31. 'user_email' => $user_email,
  32. 'user_password' => $user_password,
  33. 'user_phone' => $user_phone,
  34. 'captcha' => $captcha,
  35. );
  36. //验证数据 BEGIN
  37. $rule = [
  38. ['user_email', 'require|min:2', '帐号为必填|帐号长度至少为2位'],
  39. ['user_password', 'require|min:2', '密码为必填|帐号长度至少为6位'],
  40. ['captcha', 'require|min:3', '验证码为必填|帐号长度至少为3位'],
  41. ];
  42. $validate = new Validate($rule);
  43. $validate_result = $validate->check($data);
  44. if (!$validate_result) {
  45. $this->error($validate->getError());
  46. }
  47. //验证数据 END
  48. if (!captcha_check(input('post.captcha'))) {
  49. //验证失败
  50. $this->error('验证码错误');
  51. }
  52. $condition['user_email'] = $user_email;
  53. $user_info = db('user')->where($condition)->find();
  54. if (is_array($user_info) and !empty($user_info)) {
  55. $this->success('账户已存在');
  56. } else {
  57. $user_info = array(
  58. 'user_email' => $user_email,
  59. 'user_password' => md5($user_password),
  60. 'user_phone' => $user_phone,
  61. 'user_addTime' => TIMESTAMP,
  62. 'user_loginTime' => TIMESTAMP
  63. );
  64. $id = db('user')->insertGetId($user_info);
  65. //设置 session
  66. session('user_id', $id);
  67. session('user_email', $user_info['user_email']);
  68. return $this->redirect('User/Index/index');
  69. }
  70. } else {
  71. return $this->fetch();
  72. }
  73. }
  74. }
  75. ?>