Sfoglia il codice sorgente

爬取卖家云订单脚本以及用户订单统计

shensong 5 anni fa
parent
commit
77da5da3a9
1 ha cambiato i file con 35 aggiunte e 8 eliminazioni
  1. 35 8
      app/Console/Commands/freightCost.php

+ 35 - 8
app/Console/Commands/freightCost.php

@@ -2,6 +2,7 @@
2 2
 
3 3
 use App\Order;
4 4
 use Illuminate\Console\Command;
5
+use Illuminate\Support\Facades\DB;
5 6
 use Symfony\Component\Console\Input\InputOption;
6 7
 use Symfony\Component\Console\Input\InputArgument;
7 8
 
@@ -33,11 +34,26 @@ class freightCost extends Command {
33 34
 //        if(1 == $this->script){
34 35
 //            $this->getSession();
35 36
 //        } else if(2 == $this->script){
37
+        try{
38
+            $message = ' info start';
39
+            $this->log($message);
36 40
             $this->getFreightCost();
41
+        } catch (\Exception $exception) {
42
+            $message = ' error line:'.$exception->getLine().' message:'.$exception->getMessage();
43
+            $this->log($message);
44
+        }
45
+
37 46
 //        }
38 47
     }
39 48
 
40
-    public function getSession() {
49
+    public function log($message = '') {
50
+        error_log(date('Y-m-d H:i:s',time()).$message." \n ",3,"/log/seafood_log/".date('Y-m-d',time()).'freightCost.log');
51
+//        print_r($message."\n");
52
+    }
53
+
54
+    public function getSession($times = 0) {
55
+        $message = ' info getSession';
56
+        $this->log($message);
41 57
         //获取图片验证码
42 58
         $imgArr = $this->getImage();
43 59
         //百度OCR图片识别
@@ -83,7 +99,12 @@ class freightCost extends Command {
83 99
         );
84 100
 
85 101
         $res = $this->curlWithIpPorxy($method,$url,$params,$header);
86
-        return $res['words_result'][0]['words'];
102
+        if($res['words_result'][0]['words']){
103
+            return $res['words_result'][0]['words'];
104
+        } else {
105
+            $this->getSession();
106
+        }
107
+
87 108
     }
88 109
 
89 110
     //卖家云登录接口获取HTSESSIONID
@@ -106,10 +127,12 @@ class freightCost extends Command {
106 127
 
107 128
     //根据日期获取发货订单列表
108 129
     public function getFreightCost($times = 0) {
130
+        $message = ' info getFreightCost times:'.$times.' page:'.$this->page;
131
+        $this->log($message);
109 132
         $url = 'http://erp.maijiayun.cn/dd/print/statistics/list/deliver.ht';
110 133
         $method = 'post';
111 134
 //        $startDate = '2019-10-15 00:00:00';
112
-        $startDate = date('Y-m-d 00:00:00',strtotime('-5 days'));
135
+        $startDate = date('Y-m-d 00:00:00',strtotime('-30 days'));
113 136
         $endDate = date('Y-m-d 23:59:59',time());
114 137
 
115 138
         $params = array(
@@ -136,7 +159,9 @@ class freightCost extends Command {
136 159
 
137 160
         //失败次数超过3次自动停止
138 161
         if($times >= 3){
139
-            print_r($res);
162
+//            print_r($res);
163
+            $message = ' fail getFreightCost times:'.$times.' page:'.$this->page;
164
+            $this->log($message);
140 165
             echo 'FAIL';die;
141 166
         }
142 167
 
@@ -158,14 +183,16 @@ class freightCost extends Command {
158 183
         }
159 184
     }
160 185
 
161
-    //更新近十日订单
186
+    //更新近五日C仓订单
162 187
     public function updateFiveDaysOrder( $data ) {
163 188
         foreach( $data as $value ) {
164 189
             if( $value['logisticsCost'] > 0 ){
165 190
                 $freightCost = $value['logisticsCost'];
166
-                $re = Order::where('logistics_id',$value['expressCode'])->where('is_del',0)->where('warehouse',3)
191
+                $re = DB::table('order_copy')->where('logistics_id',$value['expressCode'])->where('is_del',0)->where('warehouse',3)
167 192
                     ->update(['freight_cost'=>$freightCost]);
168
-                echo $value['expressCode'].' ';
193
+//                echo $value['expressCode'].' ';
194
+                $message = ' success update code:'.$value['logisticsCost'];
195
+                $this->log($message);
169 196
             } else {
170 197
                 continue;
171 198
             }
@@ -242,7 +269,7 @@ class freightCost extends Command {
242 269
         if(preg_match('/Set-Cookie:[\s]+([^=]+)=([^;]+)/i', $result,$match)) {
243 270
             $cookies[$match[1]] = $match[2];
244 271
             $this->HTSESSIONID = $cookies['HTSESSIONID'];
245
-            print_r($cookies['HTSESSIONID']);
272
+//            print_r($cookies['HTSESSIONID']);
246 273
             return $cookies['HTSESSIONID'];
247 274
 //            foreach ($cookies as $cookieKey => $cookieVal ) {
248 275
 //                setcookie($cookieKey,$cookieVal);