Bez popisu

freightCost.php 10KB

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