GteApista.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. include_once("../../untils/conn.php");
  3. // 确认数据库连接成功
  4. if (!$con) {
  5. die("数据库连接失败:" . mysqli_connect_error());
  6. }
  7. mysqli_query($con, "set names utf8");
  8. $sql = "SELECT * FROM kefu";
  9. $res = mysqli_query($con, $sql);
  10. if(mysqli_num_rows($res) > 0){
  11. $row = mysqli_fetch_assoc($res);
  12. $user_id = $row['appid'];
  13. $secret = $row['key'];
  14. }
  15. // 查询符合条件的订单号
  16. $sql = "SELECT oderid FROM oder WHERE card_sta = 0 AND status NOT IN (0, 4) AND api_type = 'yqrapi'";
  17. $res = mysqli_query($con, $sql);
  18. $order_ids = array();
  19. while ($row = mysqli_fetch_assoc($res)) {
  20. $order_ids[] = $row['oderid'];
  21. }
  22. // 遍历订单号数组,查询每个订单的信息并打印
  23. foreach ($order_ids as $order_id) {
  24. $timestamp = time();
  25. $down_order_id = $order_id;
  26. $sign = md5("DownOrderID=" . $down_order_id . "&Timestamp=" . $timestamp . "&user_id=" . $user_id . $secret);
  27. // 发送 POST 请求
  28. $url = "https://haokaopenapi.lot-ml.com/api/order/GetOrderInfo";
  29. $data = array(
  30. 'user_id' => $user_id,
  31. 'Timestamp' => $timestamp,
  32. 'DownOrderID' => $down_order_id,
  33. 'user_sign' => $sign
  34. );
  35. $options = array(
  36. 'http' => array(
  37. 'header' => "Content-type: application/x-www-form-urlencoded",
  38. 'method' => 'POST',
  39. 'content' => http_build_query($data),
  40. ),
  41. );
  42. $context = stream_context_create($options);
  43. $result = file_get_contents($url, false, $context);
  44. // 处理返回结果
  45. $result_arr = json_decode($result, true);
  46. if ($result_arr['code'] == 0) {
  47. $order_info = $result_arr['data'];
  48. $downOrderNo = $order_info['downOrderNo'];
  49. $expressName = $order_info['expressName'];
  50. $expressCode = $order_info['expressCode'];
  51. $orderStatus = $order_info['orderStatus'];
  52. $thirdPhone = $order_info['thirdPhone'];
  53. $remark = $order_info['remark'];
  54. $cardStatus = $order_info['cardStatus'];
  55. // 根据订单状态更新数据库中的订单信息
  56. if ($orderStatus == '审核不通过') {
  57. $update_sql = "UPDATE oder SET status=4, beizhu='$remark' WHERE oderid='$downOrderNo'";
  58. $update_sql_revenue = "UPDATE revenue SET oder_sta=4 WHERE oderid='$downOrderNo'";
  59. } else if ($orderStatus == '已发货') {
  60. $update_sql = "UPDATE oder SET status=2, beizhu='$expressName $expressCode', thirdphone='$thirdPhone' WHERE oderid='$downOrderNo'";
  61. $update_sql_revenue = "UPDATE revenue SET oder_sta=2 WHERE oderid='$downOrderNo'";
  62. // 在已发货状态下判断cardStatus是否为已激活,如果是则更新card_sta为1
  63. if ($cardStatus == '已激活') {
  64. $update_sql .= ", card_sta=1";
  65. }
  66. } else if ($orderStatus == '已完成') {
  67. $update_sql = "UPDATE oder SET status=3, card_sta=1 WHERE oderid='$downOrderNo'";
  68. $update_sql_revenue = "UPDATE revenue SET oder_sta=3 WHERE oderid='$downOrderNo'";
  69. } else {
  70. echo "订单 " . $downOrderNo . " 状态异常:" . $orderStatus . "\n";
  71. continue;
  72. }
  73. $update_res = mysqli_query($con, $update_sql);
  74. $update_res_revenue = mysqli_query($con, $update_sql_revenue);
  75. if ($update_res && $update_res_revenue) {
  76. echo "订单 " . $downOrderNo . " 更新成功\n";
  77. } else {
  78. echo "订单 " . $downOrderNo . " 更新失败:" . mysqli_error($con) . "\n";
  79. }
  80. } else {
  81. echo "查询订单 " . $order_id . " 失败:" . $result_arr['message'] . "\n";
  82. }
  83. }