Brak opisu

freightCost.php 9.4KB

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