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]); } }