dyjh 6 years ago
parent
commit
4bc6dacd2e
1 changed files with 67 additions and 42 deletions
  1. 67 42
      app/Http/Controllers/Web/AuthController.php

+ 67 - 42
app/Http/Controllers/Web/AuthController.php

xqd
@@ -72,53 +72,78 @@ class AuthController extends Controller
 
     public function notifyAccount(Request $request)
     {
-        /**/
-        \Log::info("111");
-        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
-        if (!empty($postStr)) {
-            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
-            $fromUsername = $postObj->FromUserName;
-            $toUsername = $postObj->ToUserName;
-            $keyword = trim($postObj->Content);
-            $time = time();
-            $textTpl = "<xml>
-            <ToUserName><![CDATA[%s]]></ToUserName>
-            <FromUserName><![CDATA[%s]]></FromUserName>
-            <CreateTime>%s</CreateTime>
-            <MsgType><![CDATA[%s]]></MsgType>
-            <Content><![CDATA[%s]]></Content>
-            <FuncFlag>0<FuncFlag>
-            </xml>";
-            if (!empty($keyword)) {
-                $msgType = "text";
-                $contentStr = '你好啊,屌丝';
-                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
-                echo $resultStr;
+        $echoStr = $_GET["echostr"];
+        //如果有$echoStr说明是对接
+        if (!empty($echoStr)) {
+            //对接规则
+            $signature = $_GET["signature"];
+            $timestamp = $_GET["timestamp"];
+            $nonce = $_GET["nonce"];
+            $token = 'bshbdajdbjadwedwqer';
+            $tmpArr = array($token, $timestamp, $nonce);
+            sort($tmpArr, SORT_STRING);
+            $tmpStr = implode($tmpArr);
+            $tmpStr = sha1($tmpStr);
+            if ($tmpStr == $signature) {
+                echo $echoStr;
             } else {
-                echo '咋不说哈呢';
+                echo "";
+                exit;
             }
         } else {
-            echo '咋不说哈呢';
-            exit;
-        }
-        $this->checkSignature();
-    }
+            $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
+            if (!empty($postStr)) {
+                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
+                $fromUsername = $postObj->FromUserName;
+                $toUsername = $postObj->ToUserName;
+                $MsgT = $postObj->MsgType;
+                $time = time();
+                //如果用户发的text类型
+                if ($MsgT == "text") {
+                    $key = trim($postObj->Content);
+                    $fromUsername = $postObj->FromUserName;
+                    $textTpl = "<xml>
+                            <ToUserName><![CDATA[%s]]></ToUserName>
+                            <FromUserName><![CDATA[%s]]></FromUserName>
+                            <CreateTime>%s</CreateTime>
+                            <MsgType><![CDATA[%s]]></MsgType>
+                            <Content><![CDATA[%s]]></Content>
+                            </xml>";
+                    $msgType = "text";
+                    $contentStr = "openid是:" . $fromUsername;
+                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
+                    echo $resultStr;
+                    exit;
+                }
 
+                //如果用户发的event(事件)类型
+                if ($MsgT == "event") {
+                    $Event = $postObj->Event;
+                    if ($Event == 'subscribe') {
+                        $contentStr = "欢迎关注";
+                    } else {
+                        $contentStr = "希望您下次关注,但您收不到此条消息了";
+                    }
 
-    private function checkSignature()
-    {
-        $signature = $_GET["signature"];
-        $timestamp = $_GET["timestamp"];
-        $nonce = $_GET["nonce"];
-        $token = 'bshbdajdbjadwedwqer';
-        $tmpArr = array($token, $timestamp, $nonce);
-        sort($tmpArr);
-        $tmpStr = implode($tmpArr);
-        $tmpStr = sha1($tmpStr);
-        if ($tmpStr == $signature) {
-            return true;
-        } else {
-            return false;
+                    $textTpl = "<xml>
+                            <ToUserName><![CDATA[%s]]></ToUserName>
+                            <FromUserName><![CDATA[%s]]></FromUserName>
+                            <CreateTime>%s</CreateTime>
+                            <MsgType><![CDATA[%s]]></MsgType>
+                            <Content><![CDATA[%s]]></Content>
+                            </xml>";
+                    $Title = $postObj->Title;
+                    $Description = $postObj->Description;
+                    $Url = $postObj->Url;
+                    $msgType = 'text';
+                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
+                    echo $resultStr;
+                    exit;
+                }
+            } else {
+                echo "";
+                exit;
+            }
         }
     }