ChOderApi.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. require('SendSms.php');
  3. session_start();
  4. include_once("../../untils/conn.php");
  5. mysqli_query($con, "set names utf8");
  6. $goods = mysqli_real_escape_string($con, $_POST['goods']);
  7. $name = mysqli_real_escape_string($con, $_POST['name']);
  8. $uid = mysqli_real_escape_string($con, $_POST['uid']);
  9. $phone = mysqli_real_escape_string($con, $_POST['phone']);
  10. $sf = mysqli_real_escape_string($con, $_POST['sf']);
  11. $city = mysqli_real_escape_string($con, $_POST['city']);
  12. $xian = mysqli_real_escape_string($con, $_POST['xian']);
  13. $dizhi = mysqli_real_escape_string($con, $_POST['dizhi']);
  14. $oderid = mysqli_real_escape_string($con, $_POST['oderid']);
  15. $time = mysqli_real_escape_string($con, $_POST['time']);
  16. $price = mysqli_real_escape_string($con, $_POST['price']);
  17. $proxy_id = mysqli_real_escape_string($con, $_POST['proxyid']);
  18. $proxy_price = mysqli_real_escape_string($con, $_POST['proxy_prices']);
  19. $api_type = mysqli_real_escape_string($con, $_POST['apiType']);
  20. // 外加佣金参数
  21. $beizhu = "订单号:" . $oderid . "返佣¥" . $proxy_price;
  22. $beizhus = "已经提交运营商审核";
  23. // 查询 proxy 表,根据 proxy_id 获取代理级别、proxy_upid 和 proxy_acc
  24. $sql = "SELECT * FROM proxy WHERE proxy_id = '$proxy_id'";
  25. $result = mysqli_query($con, $sql);
  26. if ($result) {
  27. $row = mysqli_fetch_assoc($result);
  28. $group_id = $row['group_id'];
  29. $proxy_upid = $row['proxy_upid'];
  30. $proxy_acc = $row['proxy_acc'];
  31. // 查询 proxy_group 表,获取对应的 group_name 和 group_portion
  32. $group_name = "";
  33. $group_portion = 0;
  34. $sql = "SELECT * FROM proxy_group WHERE group_id = '$group_id'";
  35. $result = mysqli_query($con, $sql);
  36. if ($result) {
  37. $row = mysqli_fetch_assoc($result);
  38. $group_name = $row['group_name'];
  39. $group_portion = $row['group_portion'];
  40. }
  41. // 根据代理级别进行不同的查询
  42. if ($group_name == '三级代理') {
  43. // 查询上级代理
  44. $sql = "SELECT * FROM proxy WHERE proxy_id = '$proxy_upid'";
  45. $result = mysqli_query($con, $sql);
  46. if ($result) {
  47. $row = mysqli_fetch_assoc($result);
  48. $proxy_upid_2 = $row['proxy_upid'];
  49. $proxy_acc_2 = $row['proxy_acc'];
  50. $group_id_2 = $row['group_id'];
  51. //取比例
  52. $sql_group_2 = "SELECT * FROM proxy_group WHERE group_id = '$group_id_2'";
  53. $result_group_2 = mysqli_query($con, $sql_group_2);
  54. if ($result_group_2) {
  55. $row_group_2 = mysqli_fetch_assoc($result_group_2);
  56. $group_portion_2 = $row_group_2['group_portion'];
  57. }
  58. $newprice_a = $proxy_price + $group_portion;
  59. $newprice_b = $proxy_price + $group_portion - $group_portion_2;
  60. $newprice_2 = $newprice_a - $newprice_b;
  61. // 查询上上级代理
  62. $sql = "SELECT * FROM proxy WHERE proxy_id = '$proxy_upid_2'";
  63. $result = mysqli_query($con, $sql);
  64. if ($result) {
  65. $row = mysqli_fetch_assoc($result);
  66. $proxy_acc_3 = $row['proxy_acc'];
  67. $group_id_3 = $row['group_id'];
  68. //取比例
  69. $sql_group_3 = "SELECT * FROM proxy_group WHERE group_id = '$group_id_3'";
  70. $result_group_3 = mysqli_query($con, $sql_group_3);
  71. if ($result_group_3) {
  72. $row_group_3 = mysqli_fetch_assoc($result_group_3);
  73. $group_portion_3 = $row_group_3['group_portion'];
  74. }
  75. $newprice_a = $proxy_price + $group_portion;
  76. $newprice_b = $proxy_price + $group_portion - $group_portion_3;
  77. $newprice_3 = $newprice_a - $newprice_b;
  78. // 判断是否有name,uid,phone,dizhi这四项信息,如果有任何一项为空,则返回错误信息
  79. if (empty($name) || empty($uid) || empty($phone) || empty($dizhi)) {
  80. $response = array('status' => -1, 'msg' => '信息不完整,请重新输入');
  81. echo json_encode($response);
  82. return;
  83. }
  84. // 判断订单是否已经提交过
  85. session_start();
  86. if (isset($_SESSION['order_id']) && $_SESSION['order_id'] == $oderid) {
  87. $response = array('status' => 1, 'msg' => '订单已经提交过了');
  88. echo json_encode($response);
  89. return;
  90. }
  91. $data_query = mysqli_query($con, "select * from oder where uid='$uid'");
  92. $count = mysqli_num_rows($data_query);
  93. if ($count > 10) {
  94. // 用户已经申请过了,返回状态码1和提示信息
  95. $response = array('status' => 1, 'msg' => '您申请的订单已超限,请联系客服');
  96. echo json_encode($response);
  97. } else {
  98. // 订单写入数据库成功后执行写入佣金表
  99. $data_insert = mysqli_query($con, "INSERT INTO oder(goods, name, uid, phone, sf, city, xian, dizhi, oderid, time, proxy_id, proxy_price, price, api_type) VALUES ('$goods', '$name', '$uid', '$phone', '$sf', '$city', '$xian', '$dizhi', '$oderid', '$time', '$proxy_id', '$proxy_price', '$price', '$api_type')");
  100. // 写入佣金表
  101. $data_revenue = mysqli_query($con, "INSERT INTO revenue(account, oderid, name, price, beizhu, cr_time, proxy_upid, proxy_prices, proxy_upids, proxy_pricess) VALUES ('$proxy_acc', '$oderid', '$name', '$proxy_price', '$beizhu', '$time', '$proxy_acc_2', '$newprice_2', '$proxy_acc_3', '$newprice_3')");
  102. }
  103. }
  104. }
  105. } elseif ($group_name == '二级代理') {
  106. // 查询上级代理
  107. $sql = "SELECT * FROM proxy WHERE proxy_id = '$proxy_upid'";
  108. $result = mysqli_query($con, $sql);
  109. if ($result) {
  110. $row = mysqli_fetch_assoc($result);
  111. $proxy_acc_2 = $row['proxy_acc'];
  112. $group_id_2 = $row['group_id'];
  113. //取比例
  114. $sql_group_2 = "SELECT * FROM proxy_group WHERE group_id = '$group_id_2'";
  115. $result_group_2 = mysqli_query($con, $sql_group_2);
  116. if ($result_group_2) {
  117. $row_group_2 = mysqli_fetch_assoc($result_group_2);
  118. $group_portion_2 = $row_group_2['group_portion'];
  119. }
  120. $newprice_a = $proxy_price + $group_portion;
  121. $newprice_b = $proxy_price + $group_portion - $group_portion_2;
  122. $newprice_2 = $newprice_a - $newprice_b;
  123. // 判断是否有name,uid,phone,dizhi这四项信息,如果有任何一项为空,则返回错误信息
  124. if (empty($name) || empty($uid) || empty($phone) || empty($dizhi)) {
  125. $response = array('status' => -1, 'msg' => '信息不完整,请重新输入');
  126. echo json_encode($response);
  127. return;
  128. }
  129. // 判断订单是否已经提交过
  130. session_start();
  131. if (isset($_SESSION['order_id']) && $_SESSION['order_id'] == $oderid) {
  132. $response = array('status' => 1, 'msg' => '订单已经提交过了');
  133. echo json_encode($response);
  134. return;
  135. }
  136. $data_query = mysqli_query($con, "select * from oder where uid='$uid'");
  137. $count = mysqli_num_rows($data_query);
  138. if ($count > 10) {
  139. // 用户已经申请过了,返回状态码1和提示信息
  140. $response = array('status' => 1, 'msg' => '您申请的订单已超限,请联系客服');
  141. echo json_encode($response);
  142. } else {
  143. // 订单写入数据库成功后执行写入佣金表
  144. $data_insert = mysqli_query($con, "INSERT INTO oder(goods, name, uid, phone, sf, city, xian, dizhi, oderid, time, proxy_id, proxy_price, price ,api_type) VALUES ('$goods', '$name', '$uid', '$phone', '$sf', '$city', '$xian', '$dizhi', '$oderid', '$time', '$proxy_id', '$proxy_price', '$price', '$api_type')");
  145. // 写入佣金表
  146. $data_revenue = mysqli_query($con, "INSERT INTO revenue(account, oderid, name, price, beizhu, cr_time, proxy_upid, proxy_prices) VALUES ('$proxy_acc', '$oderid', '$name', '$proxy_price', '$beizhu', '$time', '$proxy_acc_2', '$newprice_2')");
  147. }
  148. }
  149. }elseif ($group_name == '一级代理') {
  150. // 判断是否有name,uid,phone,dizhi这四项信息,如果有任何一项为空,则返回错误信息
  151. if (empty($name) || empty($uid) || empty($phone) || empty($dizhi)) {
  152. $response = array('status' => -1, 'msg' => '信息不完整,请重新输入');
  153. echo json_encode($response);
  154. return;
  155. }
  156. // 判断订单是否已经提交过
  157. session_start();
  158. if (isset($_SESSION['order_id']) && $_SESSION['order_id'] == $oderid) {
  159. $response = array('status' => 1, 'msg' => '订单已经提交过了');
  160. echo json_encode($response);
  161. return;
  162. }
  163. $data_query = mysqli_query($con, "select * from oder where uid='$uid'");
  164. $count = mysqli_num_rows($data_query);
  165. if ($count > 10) {
  166. // 用户已经申请过了,返回状态码1和提示信息
  167. $response = array('status' => 1, 'msg' => '您申请的订单已超限,请联系客服');
  168. echo json_encode($response);
  169. } else {
  170. // 订单写入数据库成功后执行写入佣金表
  171. $data_insert = mysqli_query($con, "INSERT INTO oder(goods, name, uid, phone, sf, city, xian, dizhi, oderid, time, proxy_id, proxy_price, price ,api_type) VALUES ('$goods', '$name', '$uid', '$phone', '$sf', '$city', '$xian', '$dizhi', '$oderid', '$time', '$proxy_id', '$proxy_price', '$price' , '$api_type')");
  172. // 写入佣金表
  173. $data_revenue = mysqli_query($con, "INSERT INTO revenue(account, oderid, name, price, beizhu, cr_time) VALUES ('$proxy_acc', '$oderid', '$name', '$proxy_price', '$beizhu', '$time')");
  174. }
  175. } elseif ($group_name == '运营平台') {
  176. // 判断是否有name,uid,phone,dizhi这四项信息,如果有任何一项为空,则返回错误信息
  177. if (empty($name) || empty($uid) || empty($phone) || empty($dizhi)) {
  178. $response = array('status' => -1, 'msg' => '信息不完整,请重新输入');
  179. echo json_encode($response);
  180. return;
  181. }
  182. // 判断订单是否已经提交过
  183. session_start();
  184. if (isset($_SESSION['order_id']) && $_SESSION['order_id'] == $oderid) {
  185. $response = array('status' => 1, 'msg' => '订单已经提交过了');
  186. echo json_encode($response);
  187. return;
  188. }
  189. $data_query = mysqli_query($con, "select * from oder where uid='$uid'");
  190. $count = mysqli_num_rows($data_query);
  191. if ($count > 10) {
  192. // 用户已经申请过了,返回状态码1和提示信息
  193. $response = array('status' => 1, 'msg' => '您申请的订单已超限,请联系客服');
  194. echo json_encode($response);
  195. } else {
  196. // 订单写入数据库成功后执行写入佣金表
  197. $data_insert = mysqli_query($con, "INSERT INTO oder(goods, name, uid, phone, sf, city, xian, dizhi, oderid, time, proxy_id, proxy_price, price ,api_type) VALUES ('$goods', '$name', '$uid', '$phone', '$sf', '$city', '$xian', '$dizhi', '$oderid', '$time', '$proxy_id', '$proxy_price', '$price' , '$api_type')");
  198. // 写入佣金表
  199. $data_revenue = mysqli_query($con, "INSERT INTO revenue(account, oderid, name, price, beizhu, cr_time) VALUES ('$proxy_acc', '$oderid', '$name', '$proxy_price', '$beizhu', '$time')");
  200. }
  201. }
  202. $sql = "SELECT haokaid FROM list WHERE name='$goods'";
  203. $res = mysqli_query($con, $sql);
  204. if (mysqli_num_rows($res) > 0) {
  205. $row = mysqli_fetch_assoc($res);
  206. $haokaid = $row['haokaid'];
  207. }
  208. if ($haokaid == 0) {
  209. // haokaid等于0,直接输出申请成功
  210. $response = array('status' => 0, 'msg' => '申请成功');
  211. echo json_encode($response);
  212. // 将订单号写入session
  213. $_SESSION['order_id'] = $oderid;
  214. //发送短信通知
  215. sendSms($appId, $appKey, $phoneNumber, $templateId, $smsSign);
  216. exit;
  217. } else {
  218. $sql = "SELECT * FROM kefu";
  219. $res = mysqli_query($con, $sql);
  220. if (mysqli_num_rows($res) > 0) {
  221. $row = mysqli_fetch_assoc($res);
  222. $user_id = $row['appid'];
  223. $secret = $row['key'];
  224. }
  225. // 计算签名
  226. $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);
  227. // echo $user_sign;
  228. // exit ;
  229. // 构造请求参数
  230. $params = array(
  231. 'user_id' => $user_id,
  232. 'Timestamp' => $time,
  233. 'Name' => $name,
  234. 'Phone' => $phone,
  235. 'IDCard' => $uid,
  236. 'Province' => $sf,
  237. 'City' => $city,
  238. 'Area' => $xian,
  239. 'Address' => $dizhi,
  240. 'ProductID' => $haokaid,
  241. 'DownOrderID' => $oderid,
  242. 'ThirdPhone' => '',
  243. 'user_sign' => $user_sign,
  244. );
  245. // 构造请求头
  246. $headers = array(
  247. 'Content-Type: application/x-www-form-urlencoded'
  248. );
  249. // 发送请求
  250. $ch = curl_init();
  251. curl_setopt($ch, CURLOPT_URL, 'https://haokaopenapi.lot-ml.com/api/order/ApiToOrder');
  252. curl_setopt($ch, CURLOPT_POST, true);
  253. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
  254. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  255. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  256. $response = curl_exec($ch);
  257. curl_close($ch);
  258. // 解析响应数据
  259. $result = json_decode($response, true);
  260. if ($result['code'] == 0) {
  261. // 更新订单状态
  262. $update_sql = "UPDATE oder SET status=1, beizhu='$beizhus' WHERE oderid='$oderid'";
  263. $update_sql_revenue = "UPDATE revenue SET oder_sta=1 WHERE oderid='$oderid'";
  264. $update_query = mysqli_query($con, $update_sql);
  265. if ($update_query) {
  266. // 更新成功,返回状态码0和提示信息
  267. $response = array('status' => 0, 'msg' => '申请成功');
  268. echo json_encode($response);
  269. //发送短信通知
  270. sendSms($appId, $appKey, $phoneNumber, $templateId, $smsSign);
  271. } else {
  272. // 更新失败,返回状态码-2和提示信息
  273. $response = array('status' => -2, 'msg' => '更新订单状态失败');
  274. echo json_encode($response);
  275. }
  276. } else {
  277. // 下单失败,返回接口返回的错误信息
  278. $response = array('status' => $result['code'], 'msg' => $result['message']);
  279. echo json_encode($response);
  280. }
  281. }
  282. }