|
@@ -0,0 +1,283 @@
|
|
1
|
+<?php namespace App\Console\Commands;
|
|
2
|
+
|
|
3
|
+use App\Order;
|
|
4
|
+use Illuminate\Console\Command;
|
|
5
|
+use Illuminate\Support\Facades\DB;
|
|
6
|
+use Symfony\Component\Console\Input\InputOption;
|
|
7
|
+use Symfony\Component\Console\Input\InputArgument;
|
|
8
|
+
|
|
9
|
+class freightCost extends Command {
|
|
10
|
+
|
|
11
|
+ protected $signature = 'FreightCost';
|
|
12
|
+
|
|
13
|
+ /**
|
|
14
|
+ * The console command description.
|
|
15
|
+ *
|
|
16
|
+ * @var string
|
|
17
|
+ */
|
|
18
|
+ protected $description = '更新发货订单运费成本';
|
|
19
|
+// protected $script = 1;
|
|
20
|
+ protected $apiKey = 'CZx9Ft8GNnV3MO03pqmr7Ghg';
|
|
21
|
+ protected $secretKey = '1iunpfvhk6PAyxqjKOne4drfjDzyPtef';
|
|
22
|
+ protected $accessToken = '';//百度OCR
|
|
23
|
+ protected $page = 1;//当前页
|
|
24
|
+ protected $pages = 1;//总页码
|
|
25
|
+ protected $HTSESSIONID = 'HTSESSIONKEY-1573625655752481066088685965324';
|
|
26
|
+ protected $phone = '15120071946';//卖家云账号
|
|
27
|
+ protected $password = 'Kuxuan1!';//卖家云密码
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+ public function handle() {
|
|
31
|
+// $this->script = $this->argument('script');
|
|
32
|
+ set_time_limit(0);
|
|
33
|
+ ini_set('memory_limit', '1024M');
|
|
34
|
+// if(1 == $this->script){
|
|
35
|
+// $this->getSession();
|
|
36
|
+// } else if(2 == $this->script){
|
|
37
|
+ try{
|
|
38
|
+ $message = ' info start';
|
|
39
|
+ $this->log($message);
|
|
40
|
+ $this->getFreightCost();
|
|
41
|
+ } catch (\Exception $exception) {
|
|
42
|
+ $message = ' error line:'.$exception->getLine().' message:'.$exception->getMessage();
|
|
43
|
+ $this->log($message);
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+// }
|
|
47
|
+ }
|
|
48
|
+
|
|
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);
|
|
57
|
+ //获取图片验证码
|
|
58
|
+ $imgArr = $this->getImage();
|
|
59
|
+ //百度OCR图片识别
|
|
60
|
+ $code = $this->getCode($imgArr['captchBase64Image']);
|
|
61
|
+ //登录接口并获取cookie当中的HTSESSIONID
|
|
62
|
+ $this->login($code,$imgArr['captchaIndex']);
|
|
63
|
+ //登录后查询发货订单列表
|
|
64
|
+ $this->getFreightCost();
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ //获取卖家云图片验证码
|
|
68
|
+ public function getImage() {
|
|
69
|
+ $url = 'http://erp.maijiayun.cn/captcha20/refreshCaptcha/null.ht';
|
|
70
|
+ $method = 'get';
|
|
71
|
+
|
|
72
|
+ $result = $this->curlWithIpPorxy($method,$url);
|
|
73
|
+ if($result['success']){
|
|
74
|
+ return $result['data'];
|
|
75
|
+ } else {
|
|
76
|
+ return false;
|
|
77
|
+ }
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ //获取百度OCR access_token
|
|
81
|
+ public function getAccessToken() {
|
|
82
|
+ $url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=CZx9Ft8GNnV3MO03pqmr7Ghg&client_secret=1iunpfvhk6PAyxqjKOne4drfjDzyPtef&';
|
|
83
|
+ $res = file_get_contents($url);
|
|
84
|
+ $res = json_decode($res,true);
|
|
85
|
+ $this->accessToken = $res['access_token'];
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ //百度OCR 识别图片并返回验证码
|
|
89
|
+ public function getCode($imgStr) {
|
|
90
|
+ $this->getAccessToken();
|
|
91
|
+ $header = array(
|
|
92
|
+ 'Content-Type: application/x-www-form-urlencoded',
|
|
93
|
+ );
|
|
94
|
+ $method = 'post';
|
|
95
|
+ $url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token='.$this->accessToken;
|
|
96
|
+ $imgArr = explode(',',$imgStr);
|
|
97
|
+ $params = array(
|
|
98
|
+ 'image' => $imgArr[1]
|
|
99
|
+ );
|
|
100
|
+
|
|
101
|
+ $res = $this->curlWithIpPorxy($method,$url,$params,$header);
|
|
102
|
+ if($res['words_result'][0]['words']){
|
|
103
|
+ return $res['words_result'][0]['words'];
|
|
104
|
+ } else {
|
|
105
|
+ $this->getSession();
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ }
|
|
109
|
+
|
|
110
|
+ //卖家云登录接口获取HTSESSIONID
|
|
111
|
+ public function login($imgCaptcha,$imgCaptchaKey) {
|
|
112
|
+ $url = 'http://erp.maijiayun.cn/login.ht';
|
|
113
|
+ $method = 'post';
|
|
114
|
+ $params['identifier'] = $this->phone;
|
|
115
|
+ $params['type'] = 'sms';
|
|
116
|
+ $params['password'] = md5($this->password);
|
|
117
|
+ $params['imgCaptchaKey'] = $imgCaptchaKey;
|
|
118
|
+ $params['imgCaptcha'] = $imgCaptcha;
|
|
119
|
+ $params['eid'] = 'RLMHCBEVA65INDEQ675TEJBW54PQHGD6A62PW2L7G2QZEPVNR5MNRVUVNPMKUOXBCQOOJV4CUWH26XU3L77NFNIB4U';
|
|
120
|
+ $params['ati'] = '1543460228652';
|
|
121
|
+
|
|
122
|
+ $header = array(
|
|
123
|
+ 'Content-Type:application/x-www-form-urlencoded; charset=UTF-8',
|
|
124
|
+ );
|
|
125
|
+ $res = $this->curlWithIpPorxy2($method,$url,$params,$header);
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ //根据日期获取发货订单列表
|
|
129
|
+ public function getFreightCost($times = 0) {
|
|
130
|
+ $message = ' info getFreightCost times:'.$times.' page:'.$this->page;
|
|
131
|
+ $this->log($message);
|
|
132
|
+ $url = 'http://erp.maijiayun.cn/dd/print/statistics/list/deliver.ht';
|
|
133
|
+ $method = 'post';
|
|
134
|
+// $startDate = '2019-10-15 00:00:00';
|
|
135
|
+ $startDate = date('Y-m-d 00:00:00',strtotime('-10 days'));
|
|
136
|
+ $endDate = date('Y-m-d 23:59:59',time());
|
|
137
|
+
|
|
138
|
+ $params = array(
|
|
139
|
+ 'andQuery'=>'[{"type":"NotEqualCondition","column":"expressCode","value":""},
|
|
140
|
+ {"type":"GreaterEqualCondition","column":"deliveryTime","value":"'.$startDate.'"},
|
|
141
|
+ {"type":"LessEqualCondition","column":"deliveryTime","value":"'.$endDate.'"}]',
|
|
142
|
+ 'endTime'=>$endDate,
|
|
143
|
+ 'startTime'=>$startDate,
|
|
144
|
+ 'sort_by'=>'deliveryTime',
|
|
145
|
+ 'per_page'=>'10',
|
|
146
|
+ 'page'=>$this->page,
|
|
147
|
+ 'order'=>'desc',
|
|
148
|
+ );
|
|
149
|
+ $header = array(
|
|
150
|
+ 'Content-Type:application/json',
|
|
151
|
+ );
|
|
152
|
+ $cookie = 'HTSESSIONID='.$this->HTSESSIONID;
|
|
153
|
+ $res = $this->curlWithIpPorxy($method,$url,$params,$header,0,'json',$cookie);
|
|
154
|
+
|
|
155
|
+ //cookie过期 重新登录
|
|
156
|
+ if(isset($res['type']) && 'MJYLoginException' == $res['type']){
|
|
157
|
+ $this->getSession();
|
|
158
|
+ }
|
|
159
|
+
|
|
160
|
+ //失败次数超过3次自动停止
|
|
161
|
+ if($times >= 3){
|
|
162
|
+// print_r($res);
|
|
163
|
+ $message = ' fail getFreightCost times:'.$times.' page:'.$this->page;
|
|
164
|
+ $this->log($message);
|
|
165
|
+ echo 'FAIL';die;
|
|
166
|
+ }
|
|
167
|
+
|
|
168
|
+ //没有返回正确数据格式 重新请求一次
|
|
169
|
+ if(!isset($res['totalPageNum'])){
|
|
170
|
+ $times++;
|
|
171
|
+ $this->getFreightCost($times);
|
|
172
|
+ }
|
|
173
|
+ $this->pages = $res['totalPageNum'];
|
|
174
|
+
|
|
175
|
+ //更新订单运费
|
|
176
|
+ $this->updateFiveDaysOrder($res['page']);
|
|
177
|
+ if($this->page < $this->pages){
|
|
178
|
+ //继续请求下一页数据
|
|
179
|
+ $this->page++;
|
|
180
|
+ $this->getFreightCost();
|
|
181
|
+ } else {
|
|
182
|
+ die;
|
|
183
|
+ }
|
|
184
|
+ }
|
|
185
|
+
|
|
186
|
+ //更新近五日C仓订单
|
|
187
|
+ public function updateFiveDaysOrder( $data ) {
|
|
188
|
+ foreach( $data as $value ) {
|
|
189
|
+ if( $value['logisticsCost'] > 0 ){
|
|
190
|
+ $freightCost = $value['logisticsCost'];
|
|
191
|
+ $re = DB::table('order_copy')->where('logistics_id',$value['expressCode'])->where('is_del',0)->where('warehouse',3)
|
|
192
|
+ ->update(['freight_cost'=>$freightCost]);
|
|
193
|
+// echo $value['expressCode'].' ';
|
|
194
|
+ $message = ' success update code:'.$value['logisticsCost'];
|
|
195
|
+ $this->log($message);
|
|
196
|
+ } else {
|
|
197
|
+ continue;
|
|
198
|
+ }
|
|
199
|
+ }
|
|
200
|
+ }
|
|
201
|
+
|
|
202
|
+ //获取接口返回数据
|
|
203
|
+ private function curlWithIpPorxy($method, $url, $data = array(), $headers = false, $times = 0, $datatype = 'form-data',$cookie = false) {
|
|
204
|
+ $times++;
|
|
205
|
+ $ch = curl_init(); //初始化curl
|
|
206
|
+// curl_setopt($ch, CURLOPT_COOKIEJAR, storage_path()."/logs/cookie_jar.txt");
|
|
207
|
+ curl_setopt($ch, CURLOPT_URL, $url);
|
|
208
|
+ curl_setopt($ch, CURLOPT_HEADER, 0); //
|
|
209
|
+ curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
|
210
|
+ if($headers){
|
|
211
|
+ curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
212
|
+ }
|
|
213
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
|
|
214
|
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //绕过ssl验证
|
|
215
|
+ curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
|
216
|
+ if ($method == 'post') {
|
|
217
|
+ curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
|
|
218
|
+ if ($datatype == 'form-data') {
|
|
219
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
|
|
220
|
+ } else if ($datatype == 'json') {
|
|
221
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
|
222
|
+ }
|
|
223
|
+ }
|
|
224
|
+ if ($cookie) {
|
|
225
|
+ curl_setopt($ch, CURLOPT_COOKIE, $cookie);
|
|
226
|
+ }
|
|
227
|
+
|
|
228
|
+ $result = curl_exec($ch); //运行curl
|
|
229
|
+ if ($result === false && $times < 4) { //接口超时
|
|
230
|
+ return self::curlWithIpPorxy($method, $url, $data, $headers, $times);
|
|
231
|
+ }
|
|
232
|
+ if ($result === false) {
|
|
233
|
+ return $result;
|
|
234
|
+ }
|
|
235
|
+ return json_decode($result, true);
|
|
236
|
+ }
|
|
237
|
+
|
|
238
|
+ //需要获取返回的response body中的cookie值
|
|
239
|
+ private function curlWithIpPorxy2($method, $url, $data = array(), $headers = false, $times = 0, $datatype = 'form-data') {
|
|
240
|
+ $times++;
|
|
241
|
+ $ch = curl_init(); //初始化curl
|
|
242
|
+// curl_setopt($ch, CURLOPT_COOKIEJAR, storage_path()."/logs/cookie_jar.txt");
|
|
243
|
+ curl_setopt($ch, CURLOPT_URL, $url);
|
|
244
|
+ curl_setopt($ch, CURLOPT_HEADER, 1); //
|
|
245
|
+ curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
|
246
|
+ if($headers){
|
|
247
|
+ curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
248
|
+ }
|
|
249
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
|
|
250
|
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //绕过ssl验证
|
|
251
|
+ curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
|
252
|
+ if ($method == 'post') {
|
|
253
|
+ curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
|
|
254
|
+ if ($datatype == 'form-data') {
|
|
255
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
|
|
256
|
+ } else if ($datatype == 'json') {
|
|
257
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
|
258
|
+ }
|
|
259
|
+ }
|
|
260
|
+
|
|
261
|
+ $result = curl_exec($ch); //运行curl
|
|
262
|
+ if ($result === false && $times < 4) { //接口超时
|
|
263
|
+ return $this->curlWithIpPorxy($method, $url, $data, $headers, $times);
|
|
264
|
+ }
|
|
265
|
+ if ($result === false) {
|
|
266
|
+ return $result;
|
|
267
|
+ }
|
|
268
|
+ // 解析HTTP数据流
|
|
269
|
+ if(preg_match('/Set-Cookie:[\s]+([^=]+)=([^;]+)/i', $result,$match)) {
|
|
270
|
+ $cookies[$match[1]] = $match[2];
|
|
271
|
+ $this->HTSESSIONID = $cookies['HTSESSIONID'];
|
|
272
|
+// print_r($cookies['HTSESSIONID']);
|
|
273
|
+ return $cookies['HTSESSIONID'];
|
|
274
|
+// foreach ($cookies as $cookieKey => $cookieVal ) {
|
|
275
|
+// setcookie($cookieKey,$cookieVal);
|
|
276
|
+// }
|
|
277
|
+ } else {
|
|
278
|
+ return $this->curlWithIpPorxy($method, $url, $data, $headers, $times);
|
|
279
|
+ }
|
|
280
|
+// return json_decode($result, true);
|
|
281
|
+ }
|
|
282
|
+
|
|
283
|
+}
|