| 
					
				 | 
			
			
				@@ -2,16 +2,18 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 namespace app\api\controller; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-use ali\AliyunSMS; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 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 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    public function send() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private $ip; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private $token; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public function _init() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         //权限校验 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         $token = $this->request->header('token'); 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -30,27 +32,39 @@ class Sms extends ApiBaseController 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             ajax_success(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        //发送短信 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $this->ip = $ip; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $this->token = $token; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public function send() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //接收短信参数 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         $type           = input('post.type', ''); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         $mobile         = input('post.mobile', ''); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        $content        = input('post.content', ''); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         $template_code  = input('post.template_code', ''); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        $template_param = input('post.template_param', ''); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if ($type == 'ali') { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (empty($mobile) || empty($template_code) || empty($template_param)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                ajax_error('手机号或模板号或模板参数不能为空!'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (empty($mobile) || empty($content)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                ajax_error('手机号或内容不能为空!'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $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_code, $template_param); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $res = SmsService::apiSend($mobile, $content, $type, $template, $template_param); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         //记录日志 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         $log = [ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            'ip'             => $ip, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            'token'          => $token, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            'ip'             => $this->ip, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            'token'          => $this->token, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             'mobile'         => $mobile, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             'content'        => $content, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             'template_code'  => $template_code, 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -66,4 +80,18 @@ class Sms extends ApiBaseController 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         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]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 |