ChOderApi.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. require('SendSms.php');
  3. $goods = $_POST['goods'];
  4. $name = $_POST['name'];
  5. $uid = $_POST['uid'];
  6. $phone = $_POST['phone'];
  7. $sf = $_POST['sf'];
  8. $city = $_POST['city'];
  9. $xian = $_POST['xian'];
  10. $dizhi = $_POST['dizhi'];
  11. $oderid = $_POST['oderid'];
  12. $time = $_POST['time'];
  13. $channel = $_POST['channel'];
  14. $price = $_POST['price'];
  15. $beizhu = "已经提交运营商审核";
  16. include_once("../untils/conn.php");
  17. if ($con) {
  18. mysqli_query($con, "set names utf8");
  19. // 判断是否有name,uid,phone,dizhi这四项信息,如果有任何一项为空,则返回错误信息
  20. if (empty($name) || empty($uid) || empty($phone) || empty($dizhi)) {
  21. $response = array('status' => -1, 'msg' => '信息不完整,请重新输入');
  22. echo json_encode($response);
  23. return;
  24. }
  25. // 判断订单是否已经提交过
  26. session_start();
  27. if (isset($_SESSION['order_id']) && $_SESSION['order_id'] == $oderid) {
  28. $response = array('status' => 1, 'msg' => '订单已经提交过了');
  29. echo json_encode($response);
  30. return;
  31. }
  32. $data_query = mysqli_query($con, "select * from oder where uid='$uid'");
  33. $count = mysqli_num_rows($data_query);
  34. if ($count > 10) {
  35. // 用户已经申请过了,返回状态码1和提示信息
  36. $response = array('status' => 1, 'msg' => '您申请的订单已超限,请联系客服');
  37. echo json_encode($response);
  38. } else {
  39. // 判断$channel变量是否为空,如果为空则将其赋值为null
  40. if(empty($channel)){
  41. $channel = 0;
  42. }
  43. $data_insert = mysqli_query($con, "insert into oder(goods,name,uid,phone,sf,city,xian,dizhi,oderid,time,price,channel) values('$goods','$name','$uid','$phone','$sf','$city','$xian','$dizhi','$oderid','$time','$price','$channel')");
  44. if ($data_insert) {
  45. $sql = "SELECT haokaid FROM list WHERE name='$goods'";
  46. $res = mysqli_query($con, $sql);
  47. if(mysqli_num_rows($res) > 0){
  48. $row = mysqli_fetch_assoc($res);
  49. $haokaid = $row['haokaid'];
  50. }
  51. if ($haokaid == 0) {
  52. // haokaid等于0,直接输出申请成功
  53. $response = array('status' => 0, 'msg' => '申请成功');
  54. echo json_encode($response);
  55. // 将订单号写入session
  56. $_SESSION['order_id'] = $oderid;
  57. //发送短信通知
  58. sendSms($appId, $appKey, $phoneNumber, $templateId, $smsSign);
  59. } else {
  60. $sql = "SELECT * FROM kefu";
  61. $res = mysqli_query($con, $sql);
  62. if(mysqli_num_rows($res) > 0){
  63. $row = mysqli_fetch_assoc($res);
  64. $user_id = $row['appid'];
  65. $secret = $row['key'];
  66. }
  67. // 计算签名
  68. $user_sign = md5 ("Address=".$dizhi."&Area=".$xian."&City=".$city."&DownOrderID=".$oderid."&IDCard=".$uid."&Name=".$name."&Phone=".$phone."&ProductID=".$haokaid."&Province=".$sf."&ThirdPhone=&Timestamp=".$time."&user_id=".$user_id.$secret);
  69. // echo $user_sign;
  70. // exit ;
  71. // 构造请求参数
  72. $params = array(
  73. 'user_id' => $user_id,
  74. 'Timestamp' => $time,
  75. 'Name' => $name,
  76. 'Phone' => $phone,
  77. 'IDCard' => $uid,
  78. 'Province' => $sf,
  79. 'City' => $city,
  80. 'Area' => $xian,
  81. 'Address' => $dizhi,
  82. 'ProductID' => $haokaid,
  83. 'DownOrderID' => $oderid,
  84. 'ThirdPhone' => '',
  85. 'user_sign' => $user_sign,
  86. );
  87. // 构造请求头
  88. $headers = array(
  89. 'Content-Type: application/x-www-form-urlencoded'
  90. );
  91. // 发送请求
  92. $ch = curl_init();
  93. curl_setopt($ch, CURLOPT_URL, 'https://haokaopenapi.lot-ml.com/api/order/ApiToOrder');
  94. curl_setopt($ch, CURLOPT_POST, true);
  95. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
  96. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  97. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  98. $response = curl_exec($ch);
  99. curl_close($ch);
  100. // 解析响应数据
  101. $result = json_decode($response, true);
  102. if ($result['code'] == 0) {
  103. // 更新订单状态
  104. $update_sql = "UPDATE oder SET status=1, beizhu='$beizhu' WHERE oderid='$oderid'";
  105. $update_query = mysqli_query($con, $update_sql);
  106. if ($update_query) {
  107. // 更新成功,返回状态码0和提示信息
  108. $response = array('status' => 0, 'msg' => '申请成功');
  109. echo json_encode($response);
  110. //发送短信通知
  111. sendSms($appId, $appKey, $phoneNumber, $templateId, $smsSign);
  112. } else {
  113. // 更新失败,返回状态码-2和提示信息
  114. $response = array('status' => -2, 'msg' => '更新订单状态失败');
  115. echo json_encode($response);
  116. }
  117. } else {
  118. // 下单失败,返回接口返回的错误信息
  119. $response = array('status' => $result['code'], 'msg' => $result['message']);
  120. echo json_encode($response);
  121. }
  122. }
  123. } else {
  124. // 信息不全,返回状态码-1和提示信息
  125. $response = array('status' => -1, 'msg' => '哦豁,又出错了,找客服吧~');
  126. echo json_encode($response);
  127. }
  128. }
  129. }