浏览代码

test-update

zhaozhen 2 年之前
父节点
当前提交
3dcb57905f
共有 1 个文件被更改,包括 136 次插入1 次删除
  1. 136 1
      app/Console/Commands/Test.php

+ 136 - 1
app/Console/Commands/Test.php

@@ -697,7 +697,7 @@ class Test extends Command
697 697
                 }
698 698
 
699 699
                 $pid = null;
700
-                $res = MassMsgService::drawPid($data, $pid, 'test');
700
+                $res = $this->drawPid($data, $pid, 'test');
701 701
                 if ($res) {
702 702
                     if (!is_null($pid)) {
703 703
                         $upDb['pids'] = $pid;
@@ -757,4 +757,139 @@ class Test extends Command
757 757
             $idMin = $list->max('id');
758 758
         }
759 759
     }
760
+
761
+    public function drawPid($attachments, &$pid, $logName)
762
+    {
763
+        Log::logInfo('提取开始', $attachments, $logName);
764
+
765
+        $drawPids = [];
766
+        foreach ($attachments as $datum) {
767
+            if (isset($datum['msgtype'])) {
768
+                $url = null;
769
+                // 小程序
770
+                if (
771
+                    ($datum['msgtype'] == 'miniprogram')
772
+                    &&
773
+                    isset($datum['miniprogram']['page'])
774
+                    &&
775
+                    !empty($datum['miniprogram']['page'])
776
+                ) {
777
+                    $url = $datum['miniprogram']['page'];
778
+                }
779
+
780
+                // 链接
781
+                if (
782
+                    ($datum['msgtype'] == 'link')
783
+                    &&
784
+                    isset($datum['link']['url'])
785
+                    &&
786
+                    !empty($datum['link']['url'])
787
+                ) {
788
+                    $url = $datum['link']['url'];
789
+                }
790
+
791
+                // H5推广
792
+                if (
793
+                    ($datum['msgtype'] == 'promote')
794
+                    &&
795
+                    isset($datum['promote']['jump_url'])
796
+                    &&
797
+                    !empty($datum['promote']['jump_url'])
798
+                ) {
799
+                    $url = $datum['promote']['jump_url'];
800
+                }
801
+
802
+                // 欢迎语中的H5推广会被转换成普通链接方式
803
+                if (
804
+                    ($datum['msgtype'] == 'link')
805
+                    &&
806
+                    isset($datum['link']['jump_url'])
807
+                    &&
808
+                    !empty($datum['link']['jump_url'])
809
+                ) {
810
+                    $url = $datum['link']['jump_url'];
811
+                }
812
+
813
+                // 属于其他类型
814
+                if (is_null($url)) continue;
815
+
816
+                Log::logInfo('提取URL', [
817
+                    'url'       => $url
818
+                ], $logName);
819
+
820
+                // 处理url
821
+                $filedExistNum = 0;
822
+                $this->dealUrl($url, $filedExistNum, $drawPids, $logName);
823
+
824
+                if ($filedExistNum > 1) {
825
+                    Log::logError('提取推广链接PID存在多个字段', $datum, $logName);
826
+                    return false;
827
+                }
828
+            }
829
+
830
+            if (count($drawPids) > 1) {
831
+                Log::logError('提取推广链接PID存在多个值', $datum, $logName);
832
+                return false;
833
+            }
834
+
835
+            if (!empty($drawPids)) $pid = current($drawPids);
836
+
837
+            Log::logInfo('提取结束', $attachments, $logName);
838
+        }
839
+
840
+        return true;
841
+    }
842
+
843
+    public function dealUrl($url, &$filedExistNum, &$drawPids, $logName)
844
+    {
845
+        $url = str_replace("%25", "%", $url);
846
+        $url = str_replace("&", "&", $url);
847
+
848
+        $url = urldecode($url);
849
+        $urlArr = parse_url($url);
850
+
851
+        if (!isset($urlArr['query']) && empty($urlArr['query'])) return false;
852
+
853
+        Log::logInfo('提取url', [
854
+            'url'           => $url,
855
+            'filedExistNum' => $filedExistNum,
856
+            'drawPids'      => $drawPids
857
+        ], $logName);
858
+
859
+        parse_str($urlArr['query'], $query);
860
+
861
+        // 柚子 pathId
862
+        if (isset($query['pathId']) && !empty($query['pathId'])) {
863
+            $filedExistNum ++;
864
+            $drawPids[] = $query['pathId'];
865
+        }
866
+
867
+        // 嘉书 article
868
+        if (isset($query['article']) && !empty($query['article'])) {
869
+            $filedExistNum ++;
870
+            $drawPids[] = $query['article'];
871
+        }
872
+
873
+        // 迈步 pid
874
+        if (isset($query['pid']) && !empty($query['pid'])) {
875
+            $filedExistNum ++;
876
+            $drawPids[] = $query['pid'];
877
+        }
878
+
879
+        // 点众阳光 referral_id
880
+        if (isset($query['referral_id']) && !empty($query['referral_id'])) {
881
+            $filedExistNum ++;
882
+            $drawPids[] = $query['referral_id'];
883
+        }
884
+
885
+        foreach ($query as $urlVal) {
886
+            self::dealUrl($urlVal, $filedExistNum, $drawPids, $logName);
887
+        }
888
+
889
+        Log::logInfo('提取url结果', [
890
+            'url'           => $url,
891
+            'filedExistNum' => $filedExistNum,
892
+            'drawPids'      => $drawPids
893
+        ], $logName);
894
+    }
760 895
 }