Sms.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace app\index\controller;
  3. use app\common\controller\Frontend;
  4. use app\common\library\Email;
  5. use app\common\library\Sms as Smslib;
  6. use app\common\model\User;
  7. use think\Session;
  8. /**
  9. * 短信控制器
  10. */
  11. class Sms extends Frontend
  12. {
  13. /**
  14. * 发送验证码
  15. *
  16. * 必选参数:mobile,type<br>
  17. * 可选参数:无
  18. */
  19. public function send()
  20. {
  21. $this->code = -1;
  22. $mobile = $this->request->post("mobile");
  23. $type = $this->request->post("type");
  24. $type = $type ? $type : 'register';
  25. $last = Smslib::get($mobile, $type);
  26. if ($last && time() - $last['createtime'] < 60)
  27. {
  28. //发送频繁
  29. $this->msg = __('SMS sent frequently');
  30. return;
  31. }
  32. if ($type)
  33. {
  34. $userinfo = User::getByMobile($mobile);
  35. if ($type == 'register' && $userinfo)
  36. {
  37. //注册账号
  38. $this->msg = __('The phone number already exists');
  39. return;
  40. }
  41. else if ($type == 'changepwd' && !$userinfo)
  42. {
  43. //修改密码
  44. $this->msg = __('The phone number not exists');
  45. return;
  46. }
  47. else if (in_array($type, ['changemobile', 'bindmobile']) && $userinfo)
  48. {
  49. //修改手机号
  50. $this->msg = __('The phone number already exists');
  51. return;
  52. }
  53. }
  54. $ret = Smslib::send($mobile, '', $type);
  55. if ($ret)
  56. {
  57. $this->code = 1;
  58. $this->msg = "发送成功";
  59. }
  60. else
  61. {
  62. $this->msg = __('Send failed, please try again later');
  63. }
  64. return;
  65. }
  66. /**
  67. * 检测验证码
  68. *
  69. * 必选参数:mobile,type,captchacode<br>
  70. * 可选参数:无
  71. */
  72. public function check()
  73. {
  74. $this->code = -1;
  75. $mobile = $this->request->post("mobile");
  76. $type = $this->request->post("type");
  77. $type = $type ? $type : 'register';
  78. $captchacode = $this->request->post("captchacode");
  79. if ($type)
  80. {
  81. $userinfo = User::getByMobile($mobile);
  82. if ($type == 'register' && $userinfo)
  83. {
  84. //注册账号
  85. $this->msg = __('The phone number already exists');
  86. return;
  87. }
  88. else if ($type == 'changepwd' && !$userinfo)
  89. {
  90. //修改密码
  91. $this->msg = __('The phone number note exists');
  92. return;
  93. }
  94. else if (in_array($type, ['changemobile', 'bindmobile']) && $userinfo)
  95. {
  96. //修改手机号
  97. $this->msg = __('The phone number already exists');
  98. return;
  99. }
  100. }
  101. $ret = Smslib::check($mobile, $captchacode, $type);
  102. if ($ret)
  103. {
  104. $this->code = 1;
  105. }
  106. else
  107. {
  108. $this->msg = __('The captcha code not correct');
  109. }
  110. return;
  111. }
  112. public function sendemail()
  113. {
  114. $this->code = -1;
  115. $email = $this->request->post("email");
  116. $type = $this->request->post("type");
  117. $type = $type ? $type : 'register';
  118. $name = "email{$type}";
  119. $session = session($name);
  120. if (!$session)
  121. {
  122. if (time() - $session['time'] < 120)
  123. {
  124. $this->msg = "发送邮箱验证码过于频繁";
  125. return;
  126. }
  127. }
  128. if (Smslib::sendemail($email, '', $type))
  129. {
  130. $this->code = 1;
  131. $this->msg = "发送成功";
  132. }
  133. else
  134. {
  135. $this->msg = "发送邮件失败!请稍后重试!";
  136. }
  137. }
  138. public function checkemail()
  139. {
  140. $this->code = -1;
  141. $email = $this->request->post("email");
  142. $type = $this->request->post("type");
  143. $type = $type ? $type : 'register';
  144. $ret = Smslib::checkemail($email, $captchacode, $type);
  145. if ($ret)
  146. {
  147. $this->code = 1;
  148. }
  149. else
  150. {
  151. $this->msg = __('The captcha code not correct');
  152. }
  153. }
  154. }