Sms.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhengmingwei
  5. * Date: 2020/4/27
  6. * Time: 12:45 PM
  7. */
  8. namespace addons\unishop\controller;
  9. use app\common\library\Sms as Smslib;
  10. use addons\unishop\model\User;
  11. /**
  12. * 短信
  13. */
  14. class Sms extends Base
  15. {
  16. protected $noNeedLogin = '*';
  17. protected $noNeedRight = '*';
  18. /**
  19. * @ApiTitle (发送验证码)
  20. * @ApiSummary (发送验证码)
  21. * @ApiMethod (POST)
  22. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  23. * @ApiParams (name="mobile", type="string", required=true, description="手机号")
  24. * @ApiParams (name="event", type="string", required=true, description="动作:register=注册,resetpwd=重置密码")
  25. * @ApiReturn ({"code":1,"msg":"发送成功","data":1})
  26. *
  27. */
  28. public function send()
  29. {
  30. $mobile = $this->request->post("mobile");
  31. $event = $this->request->post("event");
  32. $event = $event ? $event : 'register';
  33. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  34. $this->error(__('手机号不正确'));
  35. }
  36. $last = Smslib::get($mobile, $event);
  37. if ($last && time() - $last['createtime'] < 60) {
  38. $this->error(__('发送频繁'));
  39. }
  40. $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
  41. if ($ipSendTotal >= 5) {
  42. $this->error(__('发送频繁'));
  43. }
  44. if ($event) {
  45. $userinfo = User::getByMobile($mobile);
  46. if ($event == 'register' && $userinfo) {
  47. //已被注册
  48. $this->error(__('手机号已被注册'));
  49. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  50. //被占用
  51. $this->error(__('手机号已被占用'));
  52. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  53. //未注册
  54. $this->error(__('未注册'));
  55. }
  56. }
  57. $ret = Smslib::send($mobile, null, $event);
  58. if ($ret) {
  59. $this->success(__('发送成功'), 1);
  60. } else {
  61. $this->error(__('发送失败'), 0);
  62. }
  63. }
  64. }