RegisterController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: Powerless < wzxaini9@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\user\controller;
  12. use cmf\controller\HomeBaseController;
  13. use think\facade\Validate;
  14. use app\user\model\UserModel;
  15. class RegisterController extends HomeBaseController
  16. {
  17. /**
  18. * 前台用户注册
  19. */
  20. public function index()
  21. {
  22. $redirect = $this->request->post("redirect");
  23. if (empty($redirect)) {
  24. $redirect = $this->request->server('HTTP_REFERER');
  25. } else {
  26. $redirect = base64_decode($redirect);
  27. }
  28. session('login_http_referer', $redirect);
  29. if (cmf_is_user_login()) {
  30. return redirect($this->request->root() . '/');
  31. } else {
  32. return $this->fetch(":register");
  33. }
  34. }
  35. /**
  36. * 前台用户注册提交
  37. */
  38. public function doRegister()
  39. {
  40. if ($this->request->isPost()) {
  41. $rules = [
  42. 'captcha' => 'require',
  43. 'code' => 'require',
  44. 'password' => 'require|min:6|max:32',
  45. ];
  46. $isOpenRegistration = cmf_is_open_registration();
  47. if ($isOpenRegistration) {
  48. unset($rules['code']);
  49. }
  50. $validate = new \think\Validate($rules);
  51. $validate->message([
  52. 'code.require' => '验证码不能为空',
  53. 'password.require' => '密码不能为空',
  54. 'password.max' => '密码不能超过32个字符',
  55. 'password.min' => '密码不能小于6个字符',
  56. 'captcha.require' => '验证码不能为空',
  57. ]);
  58. $data = $this->request->post();
  59. if (!$validate->check($data)) {
  60. $this->error($validate->getError());
  61. }
  62. $captchaId = empty($data['_captcha_id']) ? '' : $data['_captcha_id'];
  63. if (!cmf_captcha_check($data['captcha'], $captchaId)) {
  64. $this->error('验证码错误');
  65. }
  66. if (!$isOpenRegistration) {
  67. $errMsg = cmf_check_verification_code($data['username'], $data['code']);
  68. if (!empty($errMsg)) {
  69. $this->error($errMsg);
  70. }
  71. }
  72. $register = new UserModel();
  73. $user['user_pass'] = $data['password'];
  74. if (Validate::is($data['username'], 'email')) {
  75. $user['user_email'] = $data['username'];
  76. $log = $register->register($user, 3);
  77. } else if (cmf_check_mobile($data['username'])) {
  78. $user['mobile'] = $data['username'];
  79. $log = $register->register($user, 2);
  80. } else {
  81. $log = 2;
  82. }
  83. $sessionLoginHttpReferer = session('login_http_referer');
  84. $redirect = empty($sessionLoginHttpReferer) ? cmf_get_root() . '/' : $sessionLoginHttpReferer;
  85. switch ($log) {
  86. case 0:
  87. $this->success('注册成功', $redirect);
  88. break;
  89. case 1:
  90. $this->error("您的账户已注册过");
  91. break;
  92. case 2:
  93. $this->error("您输入的账号格式错误");
  94. break;
  95. default :
  96. $this->error('未受理的请求');
  97. }
  98. } else {
  99. $this->error("请求错误");
  100. }
  101. }
  102. }