HkOderApi.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. $sql = "SELECT * FROM config_api WHERE groups = 'haokeyun'";
  52. $res = mysqli_query($con, $sql);
  53. if (mysqli_num_rows($res) > 0) {
  54. $row = mysqli_fetch_assoc($res);
  55. $userid = $row['userid'];
  56. $token = $row['token'];
  57. $gdkey = $row['gdkey'];
  58. }
  59. // 拼装高德地图接口URL
  60. $address = urlencode($sf . $city . $xian . $dizhi);
  61. $url = "https://restapi.amap.com/v3/geocode/geo?key=$gdkey&address=$address&city=";
  62. // 发送请求获取城市编码
  63. $response = file_get_contents($url);
  64. $data = json_decode($response, true);
  65. if ($data['status'] == "1" && $data['count'] > 0) {
  66. $citycode = $data['geocodes'][0]['citycode'];
  67. $address_code = $citycode;
  68. } else {
  69. $response = array('status' => -4, 'msg' => '没有符合的地区编码~');
  70. echo json_encode($response);
  71. }
  72. function buildSign($param, $token)
  73. {
  74. ksort($param);
  75. $arr = [];
  76. foreach ($param as $k => $v) {
  77. $arr[] = $k . "=" . $v;
  78. }
  79. $str = join(";", $arr);
  80. $str .= $token;
  81. $sign = strtoupper(md5($str));
  82. return $sign;
  83. }
  84. // 构造请求参数
  85. $params = array(
  86. 'userId' => $userid,
  87. 'goodsCode' => $haokaid,
  88. 'orderSn' => $oderid,
  89. 'cusName' => $name,
  90. 'cusPhone' => $phone,
  91. 'cusIdno' => $uid,
  92. 'consignee' => $name,
  93. 'province' => $sf,
  94. 'city' => $city,
  95. 'district' => $xian,
  96. 'address_code' => $address_code,
  97. 'address' => $dizhi,
  98. );
  99. // 计算签名
  100. $sign = buildSign($params, $token);
  101. // 添加签名到请求参数
  102. $params['sign'] = $sign;
  103. // 构造请求头
  104. $headers = array(
  105. 'Content-Type: application/json'
  106. );
  107. // 发送请求
  108. $ch = curl_init();
  109. curl_setopt($ch, CURLOPT_URL, 'https://haokeyun.vip/api/outside/order/submit_order');
  110. curl_setopt($ch, CURLOPT_POST, true);
  111. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
  112. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  113. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  114. $response = curl_exec($ch);
  115. curl_close($ch);
  116. // echo "data:" . $response . "\n";
  117. // exit;
  118. // 解析响应数据
  119. $result = json_decode($response, true);
  120. if ($result['code'] == 200) {
  121. // 更新订单状态
  122. $update_sql = "UPDATE oder SET status=1, beizhu='$beizhu' WHERE oderid='$oderid'";
  123. $update_query = mysqli_query($con, $update_sql);
  124. if ($update_query) {
  125. // 更新成功,返回状态码0和提示信息
  126. $response = array('status' => 0, 'msg' => '申请成功');
  127. echo json_encode($response);
  128. // 将订单号写入session
  129. $_SESSION['order_id'] = $oderid;
  130. //发送短信通知
  131. sendSms($appId, $appKey, $phoneNumber, $templateId, $smsSign);
  132. } else {
  133. // 更新失败,返回状态码-2和提示信息
  134. $response = array('status' => -2, 'msg' => '更新订单状态失败');
  135. echo json_encode($response);
  136. }
  137. } else {
  138. // 下单失败,返回接口返回的错误信息
  139. $response = array('status' => $result['code'], 'msg' => $result['message']);
  140. echo json_encode($response);
  141. }
  142. } else {
  143. // 信息不全,返回状态码-1和提示信息
  144. $response = array('status' => -1, 'msg' => '哦豁,又出错了,找客服吧~');
  145. echo json_encode($response);
  146. }
  147. }
  148. }