Register.php 2.9 KB

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