VerificationCodeController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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: Dean <zxxjjforever@163.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\user\controller;
  12. use cmf\controller\HomeBaseController;
  13. use think\facade\Validate;
  14. class VerificationCodeController extends HomeBaseController
  15. {
  16. public function send()
  17. {
  18. if (!$this->request->isPost()) {
  19. $this->error('非法请求!');
  20. }
  21. $validate = new \think\Validate();
  22. $validate->rule([
  23. 'username' => 'require',
  24. 'captcha' => 'require',
  25. ]);
  26. $validate->message([
  27. 'username.require' => '请输入手机号或邮箱!',
  28. 'captcha.require' => '图片验证码不能为空',
  29. ]);
  30. $data = $this->request->param();
  31. if (!$validate->check($data)) {
  32. $this->error($validate->getError());
  33. }
  34. $captchaId = empty($data['captcha_id']) ? '' : $data['captcha_id'];
  35. if (!cmf_captcha_check($data['captcha'], $captchaId, false)) {
  36. $this->error('图片验证码错误!');
  37. }
  38. $registerCaptcha = session('register_captcha');
  39. session('register_captcha', $data['captcha']);
  40. if ($registerCaptcha == $data['captcha']) {
  41. cmf_captcha_check($data['captcha'], $captchaId, true);
  42. $this->error('请输入新图片验证码!');
  43. }
  44. $accountType = '';
  45. if (Validate::is($data['username'], 'email')) {
  46. $accountType = 'email';
  47. } else if (cmf_check_mobile($data['username'])) {
  48. $accountType = 'mobile';
  49. } else {
  50. $this->error("请输入正确的手机或者邮箱格式!");
  51. }
  52. if (isset($data['type']) && $data['type'] == 'register') {
  53. if ($accountType == 'email') {
  54. $findUserCount = db('user')->where('user_email', $data['username'])->count();
  55. } else if ($accountType == 'mobile') {
  56. $findUserCount = db('user')->where('mobile', $data['username'])->count();
  57. }
  58. if ($findUserCount > 0) {
  59. $this->error('账号已注册!');
  60. }
  61. }
  62. //TODO 限制 每个ip 的发送次数
  63. $code = cmf_get_verification_code($data['username']);
  64. if (empty($code)) {
  65. $this->error("验证码发送过多,请明天再试!");
  66. }
  67. if ($accountType == 'email') {
  68. $emailTemplate = cmf_get_option('email_template_verification_code');
  69. $user = cmf_get_current_user();
  70. $username = empty($user['user_nickname']) ? $user['user_login'] : $user['user_nickname'];
  71. $message = htmlspecialchars_decode($emailTemplate['template']);
  72. $message = $this->view->display($message, ['code' => $code, 'username' => $username]);
  73. $subject = empty($emailTemplate['subject']) ? 'ThinkCMF验证码' : $emailTemplate['subject'];
  74. $result = cmf_send_email($data['username'], $subject, $message);
  75. if (empty($result['error'])) {
  76. cmf_verification_code_log($data['username'], $code);
  77. $this->success("验证码已经发送成功!");
  78. } else {
  79. $this->error("邮箱验证码发送失败:" . $result['message']);
  80. }
  81. } else if ($accountType == 'mobile') {
  82. $param = ['mobile' => $data['username'], 'code' => $code];
  83. $result = hook_one("send_mobile_verification_code", $param);
  84. if ($result !== false && !empty($result['error'])) {
  85. $this->error($result['message']);
  86. }
  87. if ($result === false) {
  88. $this->error('未安装验证码发送插件,请联系管理员!');
  89. }
  90. $expireTime = empty($result['expire_time']) ? 0 : $result['expire_time'];
  91. cmf_verification_code_log($data['username'], $code, $expireTime);
  92. if (!empty($result['message'])) {
  93. $this->success($result['message']);
  94. } else {
  95. $this->success('验证码已经发送成功!');
  96. }
  97. }
  98. }
  99. }