Bez popisu

freightCost.php 10KB

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