Sms.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. public function send()
  35. {
  36. //接收短信参数
  37. $type = input('post.type', '');
  38. $mobile = input('post.mobile', '');
  39. $template_code = input('post.template_code', '');
  40. $template_param = input('post.template_param', []);
  41. if (empty($mobile) || empty($template_code)) {
  42. ajax_error('手机号或模板号不能为空!');
  43. }
  44. //短信内容
  45. $template = SmsTemplateModel::where('code', $template_code)->find();
  46. if (empty($template)) {
  47. ajax_error('模板不存在!');
  48. }
  49. $content = $template['content'];
  50. if (!empty($template_param)) {
  51. foreach ($template_param as $k => $v) {
  52. $content = str_replace('${' . $k . '}', $v, $content);
  53. }
  54. }
  55. //默认平台
  56. if (empty($type)) {
  57. $type = SettingModel::getConfigValue('sms_type');
  58. if (empty($type)) {
  59. return ['code' => 1, 'msg' => '未配置默认模板'];
  60. }
  61. }
  62. $res = SmsService::apiSend($mobile, $content, $type, $template, $template_param);
  63. //记录日志
  64. $log = [
  65. 'ip' => $this->ip,
  66. 'token' => $this->token,
  67. 'mobile' => $mobile,
  68. 'type' => $type,
  69. 'content' => $content,
  70. 'template_code' => $template_code,
  71. 'template_param' => json_encode($template_param),
  72. 'status' => $res['code'] ? 2 : 1,
  73. 'error' => $res['code'] ? $res['msg'] : '',
  74. ];
  75. SmsLogModel::create($log);
  76. //返回结果
  77. if ($res['code']) {
  78. ajax_error($res['msg']);
  79. }
  80. ajax_success();
  81. }
  82. public function getTemplateList()
  83. {
  84. $map = $this->dealLikeInput(['code']);
  85. $list = SmsTemplateModel::field(['code', 'content', 'comment'])
  86. ->where($map)
  87. ->limit(input('limit', 10))
  88. ->page(input('page', 1))
  89. ->select();
  90. $count = SmsTemplateModel::where($map)->count();
  91. ajax_success(['list' => $list, 'count' => $count]);
  92. }
  93. }