Sms.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\model\SmsTemplateModel;
  8. use app\common\service\SmsService;
  9. use think\facade\Log;
  10. class Sms extends ApiBaseController
  11. {
  12. private $ip;
  13. private $token;
  14. public function _init()
  15. {
  16. //权限校验
  17. $token = $this->request->header('token');
  18. $ip = $this->request->ip();
  19. if (empty($token)) {
  20. ajax_success();
  21. }
  22. $auth = SmsAuthModel::where('token', $token)->find();
  23. if (empty($auth)) {
  24. Log::error("无权限token访问,token:{$token},ip:{$ip}");
  25. ajax_success();
  26. }
  27. if ($auth['ip'] != $ip) {
  28. Log::error("无权限ip访问,token:{$token},ip:{$ip}");
  29. ajax_success();
  30. }
  31. $this->ip = $ip;
  32. $this->token = $token;
  33. }
  34. /**
  35. * 短信发送接口
  36. */
  37. public function send()
  38. {
  39. //接收短信参数
  40. $type = input('post.type', '');
  41. $mobile = input('post.mobile', '');
  42. $template_code = input('post.template_code', '');
  43. $template_param = input('post.template_param', []);
  44. if (empty($mobile) || empty($template_code)) {
  45. ajax_error('手机号或模板号不能为空!');
  46. }
  47. //短信内容
  48. $template = SmsTemplateModel::where('code', $template_code)->find();
  49. if (empty($template)) {
  50. ajax_error('模板不存在!');
  51. }
  52. $content = $template['content'];
  53. if (!empty($template_param)) {
  54. foreach ($template_param as $k => $v) {
  55. $content = str_replace('${' . $k . '}', $v, $content);
  56. }
  57. }
  58. //默认平台
  59. if (empty($type)) {
  60. $type = SettingModel::getConfigValue('sms_type');
  61. if (empty($type)) {
  62. return ['code' => 1, 'msg' => '未配置默认模板'];
  63. }
  64. }
  65. $res = SmsService::apiSend($mobile, $content, $type, $template, $template_param);
  66. //记录日志
  67. $log = [
  68. 'ip' => $this->ip,
  69. 'token' => $this->token,
  70. 'mobile' => $mobile,
  71. 'type' => $type,
  72. 'content' => $content,
  73. 'template_code' => $template_code,
  74. 'template_param' => json_encode($template_param),
  75. 'status' => $res['code'] ? 2 : 1,
  76. 'error' => $res['code'] ? $res['msg'] : '',
  77. ];
  78. SmsLogModel::create($log);
  79. //返回结果
  80. if ($res['code']) {
  81. ajax_error($res['msg']);
  82. }
  83. ajax_success();
  84. }
  85. public function getTemplateList()
  86. {
  87. $map = $this->dealLikeInput(['code']);
  88. $list = SmsTemplateModel::field(['code', 'content', 'comment'])
  89. ->where($map)
  90. ->limit(input('limit', 10))
  91. ->page(input('page', 1))
  92. ->select();
  93. $count = SmsTemplateModel::where($map)->count();
  94. ajax_success(['list' => $list, 'count' => $count]);
  95. }
  96. }