GtOderApi.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. $thirdphone = $_POST['thirdphone'];
  16. $beizhu = "已经提交运营商审核";
  17. include_once("../untils/conn.php");
  18. // 执行查询
  19. $sql = "SELECT * FROM cityCode WHERE postProvinceName = '$sf' AND postCityName = '$city' AND postDistrictName = '$xian'";
  20. $result = $con->query($sql);
  21. // 检查是否有匹配的结果
  22. if ($result->num_rows > 0) {
  23. // 获取查询结果的第一行数据
  24. $row = $result->fetch_assoc();
  25. // 提取需要的字段值并存储为变量
  26. $postProvinceCode = $row['postProvinceCode'];
  27. $postCityCode = $row['postCityCode'];
  28. $postDistrictCode = $row['postDistrictCode'];
  29. } else {
  30. // echo "没有对应代码~,请联系管理员更新~";
  31. }
  32. // 执行查询
  33. $sqlconfig = "SELECT * FROM config_api WHERE groups = 'gantanhao'";
  34. $result = $con->query($sqlconfig);
  35. // 检查是否有匹配的结果
  36. if ($result->num_rows > 0) {
  37. // 获取查询结果的第一行数据
  38. $row = $result->fetch_assoc();
  39. // 提取需要的字段值并存储为变量
  40. $userid = $row['userid'];
  41. $token = $row['token'];
  42. } else {
  43. // echo "没有配置接口~,请联系管理员配置~";
  44. $response = array('status' => -5, 'msg' => '该地区没有符合的城市编码!');
  45. echo json_encode($response);
  46. }
  47. if ($con) {
  48. mysqli_query($con, "set names utf8");
  49. // 判断是否有name,uid,phone,dizhi这四项信息,如果有任何一项为空,则返回错误信息
  50. if (empty($name) || empty($uid) || empty($phone) || empty($dizhi)) {
  51. $response = array('status' => -1, 'msg' => '信息不完整,请重新输入');
  52. echo json_encode($response);
  53. return;
  54. }
  55. // 判断订单是否已经提交过
  56. session_start();
  57. if (isset($_SESSION['order_id']) && $_SESSION['order_id'] == $oderid) {
  58. $response = array('status' => 1, 'msg' => '订单已经提交过了');
  59. echo json_encode($response);
  60. return;
  61. }
  62. $data_query = mysqli_query($con, "select * from oder where uid='$uid'");
  63. $count = mysqli_num_rows($data_query);
  64. if ($count > 10) {
  65. // 用户已经申请过了,返回状态码1和提示信息
  66. $response = array('status' => 1, 'msg' => '您申请的订单已超限,请联系客服');
  67. echo json_encode($response);
  68. } else {
  69. // 判断$channel变量是否为空,如果为空则将其赋值为null
  70. if(empty($channel)){
  71. $channel = 0;
  72. }
  73. $data_insert = mysqli_query($con, "insert into oder(goods,name,uid,phone,sf,city,xian,dizhi,oderid,time,price,channel,thirdphone) values('$goods','$name','$uid','$phone','$sf','$city','$xian','$dizhi','$oderid','$time','$price','$channel','$thirdphone')");
  74. if ($data_insert) {
  75. $sql = "SELECT haokaid FROM list WHERE name='$goods'";
  76. $res = mysqli_query($con, $sql);
  77. if(mysqli_num_rows($res) > 0){
  78. $row = mysqli_fetch_assoc($res);
  79. $haokaid = $row['haokaid'];
  80. }
  81. if ($haokaid === null || $haokaid === '') {
  82. // $haokaid为null或空,执行特定的代码
  83. $response = array('status' => 0, 'msg' => '申请成功');
  84. echo json_encode($response);
  85. //发送短信通知
  86. sendSms($appId, $appKey, $phoneNumber, $templateId, $smsSign);
  87. } else {
  88. function buildSign($param, $token)
  89. {
  90. $str = 'share_id=' . $param['share_id'] . '&sku=' . $param['sku'] . '&source_id=' . $param['source_id'] . $token;
  91. $sign = md5($str);
  92. return $sign;
  93. }
  94. // 构造请求参数
  95. $params = array(
  96. 'sku' => $haokaid,
  97. 'share_id' => $userid,
  98. 'source_id' => $oderid,
  99. 'id_name' => $name,
  100. 'id_num' => $uid,
  101. 'mobile' => $phone,
  102. 'name' => $name,
  103. 'province' => $sf,
  104. 'city' => $city,
  105. 'district' => $xian,
  106. 'address' => $dizhi,
  107. );
  108. // 计算签名
  109. $sign = buildSign($params, $token);
  110. // 添加签名到请求参数
  111. $params['sign'] = $sign;
  112. // 发送请求
  113. $ch = curl_init();
  114. curl_setopt($ch, CURLOPT_URL, 'http://notify.91haoka.cn/api/plan-market/order/purchase');
  115. curl_setopt($ch, CURLOPT_POST, true);
  116. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  117. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  118. $response = curl_exec($ch);
  119. curl_close($ch);
  120. // echo "data:" . $response . "\n";
  121. // exit;
  122. // 解析响应数据
  123. $result = json_decode($response, true);
  124. if ($result) {
  125. if ($result['data']['status'] == 120) {
  126. // 更新订单状态
  127. $update_sql = "UPDATE oder SET status=1, beizhu='$beizhu' WHERE oderid='$oderid'";
  128. $update_query = mysqli_query($con, $update_sql);
  129. if ($update_query) {
  130. // 更新成功,返回状态码0和提示信息
  131. $response = array('status' => 0, 'msg' => '申请成功');
  132. echo json_encode($response);
  133. // 将订单号写入session
  134. $_SESSION['order_id'] = $oderid;
  135. //发送短信通知
  136. sendSms($appId, $appKey, $phoneNumber, $templateId, $smsSign);
  137. } else {
  138. // 更新失败,返回状态码-2和提示信息
  139. $response = array('status' => -2, 'msg' => '更新订单状态失败');
  140. echo json_encode($response);
  141. }
  142. } else {
  143. // 下单失败,返回接口返回的错误信息
  144. $response = array('status' => $result['code'], 'msg' => $result['message']);
  145. echo json_encode($response);
  146. }
  147. } else {
  148. // 解析失败,返回错误信息
  149. $response = array('status' => -1, 'msg' => '解析响应数据失败');
  150. echo json_encode($response);
  151. }
  152. }
  153. } else {
  154. // 信息不全,返回状态码-1和提示信息
  155. $response = array('status' => -1, 'msg' => '哦豁,又出错了,找客服吧~');
  156. echo json_encode($response);
  157. }
  158. }
  159. }
  160. ?>