BroadbandOrder.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Anchor;
  4. use App\Models\Order;
  5. use App\Models\Package;
  6. use App\Models\Channe;
  7. use Illuminate\Support\Facades\Log;
  8. class BroadbandOrder extends \Illuminate\Console\Command
  9. {
  10. protected $signature = "broadband:order";
  11. protected $description = 'broadband order';
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. }
  16. public function handle()
  17. {
  18. // 0未支付,1已支付,2申请退款,3已退款,4不同意,5退款失败 6退款中 pay_status
  19. $packages = Package::where('is_upload', 1)
  20. // ->where('operator_id', 6)
  21. ->where('product_type',1)
  22. ->get();
  23. $ids = [];
  24. foreach ($packages as $p) {
  25. array_push($ids, $p->id);
  26. }
  27. // $orderss = [
  28. // '20240927161554995579'
  29. // ];
  30. $today = date('Y-m-d', strtotime("-10 day"));
  31. $orders = Order::where('open_status', 0)
  32. // ->where('pay_status', 0)
  33. ->whereIn('package_id', $ids)
  34. ->whereDate('created_at', ">", $today)
  35. ->where('order_type', 1)
  36. // ->whereIn('order_no', $orderss)
  37. ->get();
  38. foreach ($orders as $order) {
  39. if ($order['agent_pro_id'] == 107 || $order['agent_id'] == 107) {
  40. continue;
  41. }
  42. $this->send($order);
  43. }
  44. $orders = Order::where('open_status', 1)
  45. ->whereIn('package_id', $ids)
  46. ->whereDate('created_at', ">", $today)
  47. ->where('order_type', 1)
  48. // ->whereIn('order_no', $orderss)
  49. ->get();
  50. foreach ($orders as $order) {
  51. if ($order['agent_pro_id'] == 107 || $order['agent_id'] == 107) {
  52. continue;
  53. }
  54. $this->check($order);
  55. }
  56. Log::info('宽带订单同步完成,等待下次同步');
  57. $i = 0;
  58. while ($i < 120) {
  59. sleep(1);
  60. $i++;
  61. }
  62. $this->handle();
  63. }
  64. public function send($order)
  65. {
  66. // $package = Package::where('id', $order->package_id)->first();
  67. // if(empty($package->out_package_id)) return;
  68. $url = "";
  69. $data = [
  70. 'business_order_no' => $order->order_no, //订单号
  71. 'iden_card_name' => $order->id_card_name, //身份证姓名
  72. 'iden_card_id' => $order->id_card, //身份证号
  73. 'contact' => $order->contact, //联系人姓名
  74. 'mobile' => $order->mobile, //联系人电话
  75. 'address' => $order->express_address, //收货地址
  76. 'package_id' => $order->out_package_id, //商品编码(对应的集客仓的商品编码)
  77. 'remark' => $order->remark,
  78. ];
  79. Log::info($order->order_no.'分销宽带订单同步提交:'.json_encode($data,JSON_UNESCAPED_UNICODE));
  80. $res = $this->req($url, $data);
  81. Log::info($order->order_no.'分销宽带订单同步返回:'.json_encode($res,JSON_UNESCAPED_UNICODE));
  82. if(empty($res)) return ["code" => 2, "msg" => "服务器请求超时"];
  83. if(!empty($res["data"]["order_no"])){
  84. $updata = [
  85. 'open_status' => 1,
  86. 'api_order_no' => $res["data"]["order_no"]
  87. ];
  88. }else{
  89. $updata = [
  90. 'open_status' => 3,
  91. 'open_message' => $res["error"]
  92. ];
  93. }
  94. if (!empty($updata)) {
  95. $order->update($updata);
  96. }
  97. }
  98. public function check($order)
  99. {
  100. $url = "";
  101. $data = [
  102. "business_order_no" => $order->order_no
  103. ];
  104. $res = $this->reqGet($url, $data);
  105. // Log::info('宽带订单查询返回:'.json_encode($res,JSON_UNESCAPED_UNICODE));
  106. if(empty($res)) return false;
  107. $updata = [];
  108. if(empty($res["data"])) return;
  109. $resData = $res["data"][count($res["data"]) - 1];
  110. if($resData["open_status"] == 3){
  111. $updata = [
  112. 'open_status' => 2,
  113. 'phonenum' => $resData["phonenum"],
  114. ];
  115. }
  116. if($resData["open_status"] == 4){
  117. $updata = [
  118. 'open_status' => 3,
  119. 'open_message' => $resData["open_message"],
  120. ];
  121. }
  122. // $orderInfo = Order::where('order_no', $order->order_no)->first();
  123. if (!empty($updata)) {
  124. $order->update($updata);
  125. }
  126. }
  127. public function reqGet($url, $data)
  128. {
  129. $headers = array(
  130. "Content-Type:application/json;charset=utf-8",
  131. "Authorization:Bearer 980|vOkaPQixrLHNh97344ON0tlpY7YPDLl4vUnqp8KV5e33b43a"
  132. );
  133. $url = $url .'?'. http_build_query($data);
  134. $curl = curl_init();// 初始化
  135. curl_setopt($curl, CURLOPT_URL, $url);// 设置url路径
  136. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true) ;// 将 curl_exec()获取的信息以文件流的形式返回,而不是直接输出
  137. curl_setopt($curl, CURLOPT_BINARYTRANSFER, true) ;// 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
  138. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);// 添加头信息
  139. curl_setopt($curl, CURLINFO_HEADER_OUT, true); // CURLINFO_HEADER_OUT选项可以拿到请求头信息
  140. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);// 不验证SSL
  141. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);// 不验证SSL
  142. curl_setopt($curl, CURLOPT_TIMEOUT, 10);// 请求超时
  143. $data = curl_exec($curl);// 执行
  144. curl_close($curl);// 关闭连接
  145. return json_decode($data, true);// 返回数据
  146. }
  147. public function req($url, $data)
  148. {
  149. $data = json_encode($data, JSON_UNESCAPED_UNICODE);
  150. $headers = array(
  151. "Content-Type:application/json;charset=utf-8",
  152. "Authorization:Bearer 980|vOkaPQixrLHNh97344ON0tlpY7YPDLl4vUnqp8KV5e33b43a"
  153. );
  154. $curl = curl_init(); //初始化
  155. curl_setopt($curl, CURLOPT_URL, $url); //设置抓取的url
  156. curl_setopt($curl, CURLOPT_HEADER, false); //设置头文件的信息作为数据流输出
  157. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //设置获取的信息以文件流的形式返回,而不是直接输出。
  158. curl_setopt($curl, CURLOPT_POST, true); //设置post方式提交
  159. curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // 设置post请求参数
  160. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); // 添加头信息
  161. curl_setopt($curl, CURLINFO_HEADER_OUT, true); // CURLINFO_HEADER_OUT选项可以拿到请求头信息
  162. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 不验证SSL
  163. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 不验证SSL
  164. $data = curl_exec($curl);
  165. curl_close($curl);
  166. return json_decode($data,true);
  167. }
  168. }