Sms.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Sms as Smslib;
  5. use app\common\model\User;
  6. /**
  7. * 手机短信接口
  8. */
  9. class Sms extends Api
  10. {
  11. protected $noNeedLogin = '*';
  12. protected $noNeedRight = '*';
  13. public function _initialize()
  14. {
  15. parent::_initialize();
  16. }
  17. /**
  18. * 发送验证码
  19. *
  20. * @param string $mobile 手机号
  21. * @param string $event 事件名称
  22. */
  23. public function send()
  24. {
  25. $mobile = $this->request->request("mobile");
  26. $event = $this->request->request("event");
  27. $event = $event ? $event : 'register';
  28. $last = Smslib::get($mobile, $event);
  29. if ($last && time() - $last['createtime'] < 60)
  30. {
  31. $this->error(__('发送频繁'));
  32. }
  33. if ($event)
  34. {
  35. $userinfo = User::getByMobile($mobile);
  36. if ($event == 'register' && $userinfo)
  37. {
  38. //已被注册
  39. $this->error(__('已被注册'));
  40. }
  41. else if (in_array($event, ['changemobile']) && $userinfo)
  42. {
  43. //被占用
  44. $this->error(__('已被占用'));
  45. }
  46. else if (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo)
  47. {
  48. //未注册
  49. $this->error(__('未注册'));
  50. }
  51. }
  52. $ret = Smslib::send($mobile, NULL, $event);
  53. if ($ret)
  54. {
  55. $this->success(__('发送成功'));
  56. }
  57. else
  58. {
  59. $this->error(__('发送失败'));
  60. }
  61. }
  62. /**
  63. * 检测验证码
  64. *
  65. * @param string $mobile 手机号
  66. * @param string $event 事件名称
  67. * @param string $captcha 验证码
  68. */
  69. public function check()
  70. {
  71. $mobile = $this->request->request("mobile");
  72. $event = $this->request->request("event");
  73. $event = $event ? $event : 'register';
  74. $captcha = $this->request->request("captcha");
  75. if ($event)
  76. {
  77. $userinfo = User::getByMobile($mobile);
  78. if ($event == 'register' && $userinfo)
  79. {
  80. //已被注册
  81. $this->error(__('已被注册'));
  82. }
  83. else if (in_array($event, ['changemobile']) && $userinfo)
  84. {
  85. //被占用
  86. $this->error(__('已被占用'));
  87. }
  88. else if (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo)
  89. {
  90. //未注册
  91. $this->error(__('未注册'));
  92. }
  93. }
  94. $ret = Smslib::check($mobile, $captcha, $event);
  95. if ($ret)
  96. {
  97. $this->success(__('成功'));
  98. }
  99. else
  100. {
  101. $this->error(__('验证码不正确'));
  102. }
  103. }
  104. }