123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- <?php namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- class freightCost extends Command {
- protected $signature = 'FreightCost';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '更新发货订单运费成本';
- // protected $script = 1;
- protected $apiKey = 'CZx9Ft8GNnV3MO03pqmr7Ghg';
- protected $secretKey = '1iunpfvhk6PAyxqjKOne4drfjDzyPtef';
- protected $accessToken = '';//百度OCR
- protected $page = 1;//当前页
- protected $pages = 1;//总页码
- protected $HTSESSIONID = 'HTSESSIONKEY-1573625655752481066088685965324';
- protected $phone = '15120071946';//卖家云账号
- protected $password = 'Kuxuan1!';//卖家云密码
- public function handle() {
- // $this->script = $this->argument('script');
- set_time_limit(0);
- ini_set('memory_limit', '1024M');
- // if(1 == $this->script){
- // $this->getSession();
- // } else if(2 == $this->script){
- try{
- $message = ' info start';
- $this->log($message);
- $this->getFreightCost();
- } catch (\Exception $exception) {
- $message = ' error line:'.$exception->getLine().' message:'.$exception->getMessage();
- $this->log($message);
- }
- // }
- }
- public function log($message = '') {
- error_log(date('Y-m-d H:i:s',time()).$message." \n ",3,"/log/seafood_log/".date('Y-m-d',time()).'freightCost.log');
- // print_r($message."\n");
- }
- public function getSession($times = 0) {
- $message = ' info getSession';
- $this->log($message);
- //获取图片验证码
- $imgArr = $this->getImage();
- //百度OCR图片识别
- $code = $this->getCode($imgArr['captchBase64Image']);
- //登录接口并获取cookie当中的HTSESSIONID
- $this->login($code,$imgArr['captchaIndex']);
- //登录后查询发货订单列表
- $this->getFreightCost();
- }
- //获取卖家云图片验证码
- public function getImage() {
- $url = 'http://erp.maijiayun.cn/captcha20/refreshCaptcha/null.ht';
- $method = 'get';
- $result = $this->curlWithIpPorxy($method,$url);
- if($result['success']){
- return $result['data'];
- } else {
- return false;
- }
- }
- //获取百度OCR access_token
- public function getAccessToken() {
- $url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=CZx9Ft8GNnV3MO03pqmr7Ghg&client_secret=1iunpfvhk6PAyxqjKOne4drfjDzyPtef&';
- $res = file_get_contents($url);
- $res = json_decode($res,true);
- $this->accessToken = $res['access_token'];
- }
- //百度OCR 识别图片并返回验证码
- public function getCode($imgStr) {
- $this->getAccessToken();
- $header = array(
- 'Content-Type: application/x-www-form-urlencoded',
- );
- $method = 'post';
- $url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token='.$this->accessToken;
- $imgArr = explode(',',$imgStr);
- $params = array(
- 'image' => $imgArr[1]
- );
- $res = $this->curlWithIpPorxy($method,$url,$params,$header);
- if(isset($res['words_result'][0]['words'])){
- return $res['words_result'][0]['words'];
- } else {
- $this->getSession();
- }
- }
- //卖家云登录接口获取HTSESSIONID
- public function login($imgCaptcha,$imgCaptchaKey) {
- $url = 'http://erp.maijiayun.cn/login.ht';
- $method = 'post';
- $params['identifier'] = $this->phone;
- $params['type'] = 'sms';
- $params['password'] = md5($this->password);
- $params['imgCaptchaKey'] = $imgCaptchaKey;
- $params['imgCaptcha'] = $imgCaptcha;
- $params['eid'] = 'RLMHCBEVA65INDEQ675TEJBW54PQHGD6A62PW2L7G2QZEPVNR5MNRVUVNPMKUOXBCQOOJV4CUWH26XU3L77NFNIB4U';
- $params['ati'] = '1543460228652';
- $header = array(
- 'Content-Type:application/x-www-form-urlencoded; charset=UTF-8',
- );
- $res = $this->curlWithIpPorxy2($method,$url,$params,$header);
- }
- //根据日期获取发货订单列表
- public function getFreightCost($times = 0) {
- $message = ' info getFreightCost times:'.$times.' page:'.$this->page;
- $this->log($message);
- $url = 'http://erp.maijiayun.cn/dd/print/statistics/list/deliver.ht';
- $method = 'post';
- // $startDate = '2019-10-15 00:00:00';
- $startDate = date('Y-m-d 00:00:00',strtotime('-2 days'));
- $endDate = date('Y-m-d 23:59:59',time());
- $params = array(
- 'andQuery'=>'[{"type":"NotEqualCondition","column":"expressCode","value":""},
- {"type":"GreaterEqualCondition","column":"deliveryTime","value":"'.$startDate.'"},
- {"type":"LessEqualCondition","column":"deliveryTime","value":"'.$endDate.'"}]',
- 'endTime'=>$endDate,
- 'startTime'=>$startDate,
- 'sort_by'=>'deliveryTime',
- 'per_page'=>'10',
- 'page'=>$this->page,
- 'order'=>'desc',
- );
- $header = array(
- 'Content-Type:application/json',
- );
- $cookie = 'HTSESSIONID='.$this->HTSESSIONID;
- $res = $this->curlWithIpPorxy($method,$url,$params,$header,0,'json',$cookie);
- //cookie过期 重新登录
- if(isset($res['type']) && 'MJYLoginException' == $res['type']){
- $this->getSession();
- }
- //失败次数超过3次自动停止
- if($times >= 3){
- // print_r($res);
- $message = ' fail getFreightCost times:'.$times.' page:'.$this->page;
- $this->log($message);
- echo 'FAIL';die;
- }
- //没有返回正确数据格式 重新请求一次
- if(!isset($res['totalPageNum'])){
- $times++;
- $this->getFreightCost($times);
- }
- $this->pages = $res['totalPageNum'];
- //更新订单运费
- $this->updateFiveDaysOrder($res['page']);
- if($this->page < $this->pages){
- //继续请求下一页数据
- $this->page++;
- sleep(1);
- $this->getFreightCost();
- } else {
- die;
- }
- }
- //更新近日C仓订单
- public function updateFiveDaysOrder( $data ) {
- foreach( $data as $value ) {
- if( $value['logisticsCost'] > 0 && $value['expressCode'] != '288839495613'){
- $freightCost = $value['logisticsCost'];
- $re = DB::table('order')->where('logistics_id',$value['expressCode'])->where('is_del',0)->where('warehouse',3)
- ->update(['freight_cost'=>$freightCost]);
- // echo $value['expressCode'].' ';
- $message = ' success update code:'.$value['expressCode'].' freight_cost:'.$freightCost.' result:'.$re;
- $this->log($message);
- } else {
- continue;
- }
- }
- }
- //获取接口返回数据
- private function curlWithIpPorxy($method, $url, $data = array(), $headers = false, $times = 0, $datatype = 'form-data',$cookie = false) {
- $times++;
- $ch = curl_init(); //初始化curl
- // curl_setopt($ch, CURLOPT_COOKIEJAR, storage_path()."/logs/cookie_jar.txt");
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HEADER, 0); //
- curl_setopt($ch, CURLOPT_TIMEOUT, 5);
- if($headers){
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- }
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //绕过ssl验证
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- if ($method == 'post') {
- curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
- if ($datatype == 'form-data') {
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
- } else if ($datatype == 'json') {
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
- }
- }
- if ($cookie) {
- curl_setopt($ch, CURLOPT_COOKIE, $cookie);
- }
- $result = curl_exec($ch); //运行curl
- if ($result === false && $times < 4) { //接口超时
- return self::curlWithIpPorxy($method, $url, $data, $headers, $times);
- }
- if ($result === false) {
- return $result;
- }
- return json_decode($result, true);
- }
- //需要获取返回的response body中的cookie值
- private function curlWithIpPorxy2($method, $url, $data = array(), $headers = false, $times = 0, $datatype = 'form-data') {
- $times++;
- $ch = curl_init(); //初始化curl
- // curl_setopt($ch, CURLOPT_COOKIEJAR, storage_path()."/logs/cookie_jar.txt");
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HEADER, 1); //
- curl_setopt($ch, CURLOPT_TIMEOUT, 5);
- if($headers){
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- }
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //绕过ssl验证
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- if ($method == 'post') {
- curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
- if ($datatype == 'form-data') {
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
- } else if ($datatype == 'json') {
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
- }
- }
- $result = curl_exec($ch); //运行curl
- if ($result === false && $times < 4) { //接口超时
- return $this->curlWithIpPorxy($method, $url, $data, $headers, $times);
- }
- if ($result === false) {
- return $result;
- }
- // 解析HTTP数据流
- if(preg_match('/Set-Cookie:[\s]+([^=]+)=([^;]+)/i', $result,$match)) {
- $cookies[$match[1]] = $match[2];
- $this->HTSESSIONID = $cookies['HTSESSIONID'];
- // print_r($cookies['HTSESSIONID']);
- return $cookies['HTSESSIONID'];
- // foreach ($cookies as $cookieKey => $cookieVal ) {
- // setcookie($cookieKey,$cookieVal);
- // }
- } else {
- return $this->curlWithIpPorxy($method, $url, $data, $headers, $times);
- }
- // return json_decode($result, true);
- }
- }
|