Sms.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\ApiBaseController;
  4. use app\common\model\SettingModel;
  5. use app\common\model\SmsAuthModel;
  6. use app\common\model\SmsLogModel;
  7. use app\common\service\SmsService;
  8. use think\facade\Log;
  9. class Sms extends ApiBaseController
  10. {
  11. public function send()
  12. {
  13. //权限校验
  14. $token = $this->request->header('token');
  15. $ip = $this->request->ip();
  16. if (empty($token)) {
  17. ajax_success();
  18. }
  19. $auth = SmsAuthModel::where('token', $token)->find();
  20. if (empty($auth)) {
  21. Log::error("无权限token访问,token:{$token},ip:{$ip}");
  22. ajax_success();
  23. }
  24. if ($auth['ip'] != $ip) {
  25. Log::error("无权限ip访问,token:{$token},ip:{$ip}");
  26. ajax_success();
  27. }
  28. //发送短信
  29. $type = input('post.type', '');
  30. $mobile = input('post.mobile', '');
  31. $content = input('post.content', '');
  32. if (empty($mobile) || empty($content)) {
  33. ajax_error('手机号或内容不能为空!');
  34. }
  35. $res = SmsService::apiSend($mobile, $content, $type);
  36. //记录日志
  37. $log = [
  38. 'ip' => $ip,
  39. 'token' => $token,
  40. 'mobile' => $mobile,
  41. 'content' => $content,
  42. 'status' => $res['code'] ? 2 : 1,
  43. 'error' => $res['code'] ? $res['msg'] : '',
  44. ];
  45. SmsLogModel::create($log);
  46. //返回结果
  47. if ($res['code']) {
  48. ajax_error($res['msg']);
  49. }
  50. ajax_success();
  51. }
  52. }