12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace app\api\controller;
- use app\api\ApiBaseController;
- 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) || empty($template_param)) {
- 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);
- }
- }
- $res = SmsService::apiSend($mobile, $content, $type, $template, $template_param);
- //记录日志
- $log = [
- 'ip' => $this->ip,
- 'token' => $this->token,
- 'mobile' => $mobile,
- '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]);
- }
- }
|