Răsfoiți Sursa

通知接口

shish 5 ani în urmă
părinte
comite
a542677fd8
2 a modificat fișierele cu 37 adăugiri și 11 ștergeri
  1. 4 4
      app/mall/controllers/TestController.php
  2. 33 7
      common/components/noticeUtil.php

+ 4 - 4
app/mall/controllers/TestController.php

@@ -5,19 +5,19 @@ namespace mall\controllers;
 use biz\merchant\services\MerchantService;
 use biz\message\services\InformAdminService;
 use biz\order\services\OrderService;
-use common\components\notice;
+use common\components\noticeUtil;
 use common\components\util;
 use Yii;
 
-class TestController extends BaseController
+class TestController extends PublicController
 {
 
 	public $withoutLogin = ['index'];
 
 	public function actionIndex()
 	{
-		echo 'ok';
-		notice::push();
+		$message = "【花卉宝】我就是我, 是不一样的烟火 通知";
+		noticeUtil::push($message,'15280215347');
 	}
 
 }

+ 33 - 7
common/components/noticeUtil.php

@@ -4,17 +4,43 @@ namespace common\components;
 
 //通知系统
 use Yii;
-class notice
+use yii\helpers\Json;
+use linslin\yii2\curl;
+
+class noticeUtil
 {
 
-	public static function push()
+	public static function push($message,$mobile_list = [])
 	{
 		$url = "https://oapi.dingtalk.com/robot/send?access_token=0856d031e07890f1902496747a0576a49c3df51dcf1e16427168cce12892bedb";
-		$curl = new curl\Curl();
-		$data = array ('msgtype' => 'text','text' => array ('content' => '【花卉宝】错误提示'));
-		$result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
-		$result = Json::decode($result);
-		print_r($result);
+		$data = [
+			'msgtype' => 'text',
+			'text' => ['content' => $message]
+		];
+		if(!empty($mobile_list)){
+			$mobile_list = is_array($mobile_list) == false ? [(string)$mobile_list] : $mobile_list;
+			$data['at']['atMobiles'] = $mobile_list;
+		}
+		
+		$data_string = json_encode($data);
+		self::request($url, $data_string);
+	}
+
+	public static function request($remote_server, $post_string)
+	{
+		$ch = curl_init();
+		curl_setopt($ch, CURLOPT_URL, $remote_server);
+		curl_setopt($ch, CURLOPT_POST, 1);
+		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
+		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));
+		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
+		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+		// 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
+		// curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
+		// curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
+		$data = curl_exec($ch);
+		curl_close($ch);
+		return $data;
 	}
 
 }