| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 | 
							- <?php
 
- namespace app\api\controller;
 
- use app\api\ApiBaseController;
 
- use app\common\model\SettingModel;
 
- use app\common\model\SmsAuthModel;
 
- use app\common\model\SmsLogModel;
 
- use app\common\model\SmsTemplateModel;
 
- use app\common\service\SmsService;
 
- use think\facade\Log;
 
- class Sms extends ApiBaseController
 
- {
 
-     private $ip;
 
-     private $token;
 
-     public function _init()
 
-     {
 
-         //权限校验
 
-         $token = $this->request->header('token');
 
-         $ip    = $this->request->ip();
 
-         if (empty($token)) {
 
-             ajax_success();
 
-         }
 
-         $auth = SmsAuthModel::where('token', $token)->find();
 
-         if (empty($auth)) {
 
-             Log::error("无权限token访问,token:{$token},ip:{$ip}");
 
-             ajax_success();
 
-         }
 
-         if ($auth['ip'] != $ip) {
 
-             Log::error("无权限ip访问,token:{$token},ip:{$ip}");
 
-             ajax_success();
 
-         }
 
-         $this->ip    = $ip;
 
-         $this->token = $token;
 
-     }
 
-     public function send()
 
-     {
 
-         //接收短信参数
 
-         $type           = input('post.type', '');
 
-         $mobile         = input('post.mobile', '');
 
-         $template_code  = input('post.template_code', '');
 
-         $template_param = input('post.template_param', []);
 
-         if (empty($mobile) || empty($template_code)) {
 
-             ajax_error('手机号或模板号不能为空!');
 
-         }
 
-         //短信内容
 
-         $template = SmsTemplateModel::where('code', $template_code)->find();
 
-         if (empty($template)) {
 
-             ajax_error('模板不存在!');
 
-         }
 
-         $content = $template['content'];
 
-         if (!empty($template_param)) {
 
-             foreach ($template_param as $k => $v) {
 
-                 $content = str_replace('${' . $k . '}', $v, $content);
 
-             }
 
-         }
 
-         //默认平台
 
-         if (empty($type)) {
 
-             $type = SettingModel::getConfigValue('sms_type');
 
-             if (empty($type)) {
 
-                 return ['code' => 1, 'msg' => '未配置默认模板'];
 
-             }
 
-         }
 
-         $res = SmsService::apiSend($mobile, $content, $type, $template, $template_param);
 
-         //记录日志
 
-         $log = [
 
-             'ip'             => $this->ip,
 
-             'token'          => $this->token,
 
-             'mobile'         => $mobile,
 
-             'type'           => $type,
 
-             'content'        => $content,
 
-             'template_code'  => $template_code,
 
-             'template_param' => json_encode($template_param),
 
-             'status'         => $res['code'] ? 2 : 1,
 
-             'error'          => $res['code'] ? $res['msg'] : '',
 
-         ];
 
-         SmsLogModel::create($log);
 
-         //返回结果
 
-         if ($res['code']) {
 
-             ajax_error($res['msg']);
 
-         }
 
-         ajax_success();
 
-     }
 
-     public function getTemplateList()
 
-     {
 
-         $map = $this->dealLikeInput(['code']);
 
-         $list  = SmsTemplateModel::field(['code', 'content', 'comment'])
 
-             ->where($map)
 
-             ->limit(input('limit', 10))
 
-             ->page(input('page', 1))
 
-             ->select();
 
-         $count = SmsTemplateModel::where($map)->count();
 
-         ajax_success(['list' => $list, 'count' => $count]);
 
-     }
 
- }
 
 
  |