Register.php 3.2 KB

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