Sms.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. use think\Hook;
  7. /**
  8. * 手机短信接口
  9. */
  10. class Sms extends Api
  11. {
  12. protected $noNeedLogin = '*';
  13. protected $noNeedRight = '*';
  14. /**
  15. * 发送验证码
  16. *
  17. * @ApiMethod (POST)
  18. * @param string $mobile 手机号asdasdas
  19. * @param string $event 事件名称,register注册,changemobile更换手机号,changepwd修改密码,resetpwd重置密码,mobilelogin登录,提交订单order,商务合作cooperate,投诉complaint
  20. * @ApiReturnParams (name=code,description="2是未注册")
  21. */
  22. public function send()
  23. {
  24. $this->_validate([
  25. 'event'=>['require'],
  26. ]);
  27. $mobile = $this->request->post("mobile");
  28. $event = $this->request->post("event");
  29. $event = $event ? $event : 'register';
  30. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  31. $this->error(__('手机号不正确'));
  32. }
  33. $last = Smslib::get($mobile, $event);
  34. if ($last && time() - $last['createtime'] < 60) {
  35. $this->error(__('发送频繁'));
  36. }
  37. $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
  38. if ($ipSendTotal >= 5) {
  39. $this->error(__('发送频繁'));
  40. }
  41. $userinfo = User::getByMobile($mobile);
  42. if ($event == 'register' && $userinfo) {
  43. //已被注册
  44. $this->error(__('已被注册'));
  45. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  46. //被占用
  47. $this->error(__('已被占用'));
  48. } elseif (in_array($event, ['changepwd', 'resetpwd','mobilelogin']) && !$userinfo) {
  49. //未注册
  50. $this->error(__('未注册'),null,2);
  51. }
  52. if (!Hook::get('sms_send')) {
  53. $this->error(__('请在后台插件管理安装短信验证插件'));
  54. }
  55. $ret = Smslib::send($mobile, null, $event);
  56. if ($ret) {
  57. $this->success(__('发送成功'));
  58. } else {
  59. $this->error(__('发送失败,请检查短信配置是否正确'));
  60. }
  61. }
  62. /**
  63. * 检测验证码
  64. *
  65. * @ApiMethod (POST)
  66. * @param string $mobile 手机号
  67. * @param string $event 事件名称
  68. * @param string $captcha 验证码
  69. */
  70. public function check()
  71. {
  72. $mobile = $this->request->post("mobile");
  73. $event = $this->request->post("event");
  74. $event = $event ? $event : 'register';
  75. $captcha = $this->request->post("captcha");
  76. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  77. $this->error(__('手机号不正确'));
  78. }
  79. if ($event) {
  80. $userinfo = User::getByMobile($mobile);
  81. if ($event == 'register' && $userinfo) {
  82. //已被注册
  83. $this->error(__('已被注册'));
  84. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  85. //被占用
  86. $this->error(__('已被占用'));
  87. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  88. //未注册
  89. $this->error(__('未注册'));
  90. }
  91. }
  92. $ret = Smslib::check($mobile, $captcha, $event);
  93. if ($ret) {
  94. $this->success(__('成功'));
  95. } else {
  96. $this->error(__('验证码不正确'));
  97. }
  98. }
  99. }