GtOderApi.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. //此处用于感叹号
  24. // 执行查询
  25. $sql = "SELECT * FROM cityCode WHERE postProvinceName = '$sf' AND postCityName = '$city' AND postDistrictName = '$xian'";
  26. $result = $con->query($sql);
  27. // 检查是否有匹配的结果
  28. if ($result->num_rows > 0) {
  29. // 获取查询结果的第一行数据
  30. $row = $result->fetch_assoc();
  31. // 提取需要的字段值并存储为变量
  32. $postProvinceCode = $row['postProvinceCode'];
  33. $postCityCode = $row['postCityCode'];
  34. $postDistrictCode = $row['postDistrictCode'];
  35. } else {
  36. // echo "没有对应代码~,请联系管理员更新~";
  37. }
  38. // 执行查询
  39. $sqlconfig = "SELECT * FROM config_api WHERE groups = 'gantanhao'";
  40. $result = $con->query($sqlconfig);
  41. // 检查是否有匹配的结果
  42. if ($result->num_rows > 0) {
  43. // 获取查询结果的第一行数据
  44. $row = $result->fetch_assoc();
  45. // 提取需要的字段值并存储为变量
  46. $userid = $row['userid'];
  47. $token = $row['token'];
  48. } else {
  49. // echo "没有配置接口~,请联系管理员配置~";
  50. $response = array('status' => -5, 'msg' => '该地区没有符合的城市编码!');
  51. echo json_encode($response);
  52. }
  53. //结束用于感叹号
  54. // 查询 proxy 表,根据 proxy_id 获取代理级别、proxy_upid 和 proxy_acc
  55. $sql = "SELECT * FROM proxy WHERE proxy_id = '$proxy_id'";
  56. $result = mysqli_query($con, $sql);
  57. if ($result) {
  58. $row = mysqli_fetch_assoc($result);
  59. $group_id = $row['group_id'];
  60. $proxy_upid = $row['proxy_upid'];
  61. $proxy_acc = $row['proxy_acc'];
  62. // 查询 proxy_group 表,获取对应的 group_name 和 group_portion
  63. $group_name = "";
  64. $group_portion = 0;
  65. $sql = "SELECT * FROM proxy_group WHERE group_id = '$group_id'";
  66. $result = mysqli_query($con, $sql);
  67. if ($result) {
  68. $row = mysqli_fetch_assoc($result);
  69. $group_name = $row['group_name'];
  70. $group_portion = $row['group_portion'];
  71. }
  72. // 根据代理级别进行不同的查询
  73. if ($group_name == '三级代理') {
  74. // 查询上级代理
  75. $sql = "SELECT * FROM proxy WHERE proxy_id = '$proxy_upid'";
  76. $result = mysqli_query($con, $sql);
  77. if ($result) {
  78. $row = mysqli_fetch_assoc($result);
  79. $proxy_upid_2 = $row['proxy_upid'];
  80. $proxy_acc_2 = $row['proxy_acc'];
  81. $group_id_2 = $row['group_id'];
  82. //取比例
  83. $sql_group_2 = "SELECT * FROM proxy_group WHERE group_id = '$group_id_2'";
  84. $result_group_2 = mysqli_query($con, $sql_group_2);
  85. if ($result_group_2) {
  86. $row_group_2 = mysqli_fetch_assoc($result_group_2);
  87. $group_portion_2 = $row_group_2['group_portion'];
  88. }
  89. $newprice_a = $proxy_price + $group_portion;
  90. $newprice_b = $proxy_price + $group_portion - $group_portion_2;
  91. $newprice_2 = $newprice_a - $newprice_b;
  92. // 查询上上级代理
  93. $sql = "SELECT * FROM proxy WHERE proxy_id = '$proxy_upid_2'";
  94. $result = mysqli_query($con, $sql);
  95. if ($result) {
  96. $row = mysqli_fetch_assoc($result);
  97. $proxy_acc_3 = $row['proxy_acc'];
  98. $group_id_3 = $row['group_id'];
  99. //取比例
  100. $sql_group_3 = "SELECT * FROM proxy_group WHERE group_id = '$group_id_3'";
  101. $result_group_3 = mysqli_query($con, $sql_group_3);
  102. if ($result_group_3) {
  103. $row_group_3 = mysqli_fetch_assoc($result_group_3);
  104. $group_portion_3 = $row_group_3['group_portion'];
  105. }
  106. $newprice_a = $proxy_price + $group_portion;
  107. $newprice_b = $proxy_price + $group_portion - $group_portion_3;
  108. $newprice_3 = $newprice_a - $newprice_b;
  109. // 判断是否有name,uid,phone,dizhi这四项信息,如果有任何一项为空,则返回错误信息
  110. if (empty($name) || empty($uid) || empty($phone) || empty($dizhi)) {
  111. $response = array('status' => -1, 'msg' => '信息不完整,请重新输入');
  112. echo json_encode($response);
  113. return;
  114. }
  115. // 判断订单是否已经提交过
  116. session_start();
  117. if (isset($_SESSION['order_id']) && $_SESSION['order_id'] == $oderid) {
  118. $response = array('status' => 1, 'msg' => '订单已经提交过了');
  119. echo json_encode($response);
  120. return;
  121. }
  122. $data_query = mysqli_query($con, "select * from oder where uid='$uid'");
  123. $count = mysqli_num_rows($data_query);
  124. if ($count > 10) {
  125. // 用户已经申请过了,返回状态码1和提示信息
  126. $response = array('status' => 1, 'msg' => '您申请的订单已超限,请联系客服');
  127. echo json_encode($response);
  128. } else {
  129. // 订单写入数据库成功后执行写入佣金表
  130. $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')");
  131. // 写入佣金表
  132. $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')");
  133. }
  134. }
  135. }
  136. } elseif ($group_name == '二级代理') {
  137. // 查询上级代理
  138. $sql = "SELECT * FROM proxy WHERE proxy_id = '$proxy_upid'";
  139. $result = mysqli_query($con, $sql);
  140. if ($result) {
  141. $row = mysqli_fetch_assoc($result);
  142. $proxy_acc_2 = $row['proxy_acc'];
  143. $group_id_2 = $row['group_id'];
  144. //取比例
  145. $sql_group_2 = "SELECT * FROM proxy_group WHERE group_id = '$group_id_2'";
  146. $result_group_2 = mysqli_query($con, $sql_group_2);
  147. if ($result_group_2) {
  148. $row_group_2 = mysqli_fetch_assoc($result_group_2);
  149. $group_portion_2 = $row_group_2['group_portion'];
  150. }
  151. $newprice_a = $proxy_price + $group_portion;
  152. $newprice_b = $proxy_price + $group_portion - $group_portion_2;
  153. $newprice_2 = $newprice_a - $newprice_b;
  154. // 判断是否有name,uid,phone,dizhi这四项信息,如果有任何一项为空,则返回错误信息
  155. if (empty($name) || empty($uid) || empty($phone) || empty($dizhi)) {
  156. $response = array('status' => -1, 'msg' => '信息不完整,请重新输入');
  157. echo json_encode($response);
  158. return;
  159. }
  160. // 判断订单是否已经提交过
  161. session_start();
  162. if (isset($_SESSION['order_id']) && $_SESSION['order_id'] == $oderid) {
  163. $response = array('status' => 1, 'msg' => '订单已经提交过了');
  164. echo json_encode($response);
  165. return;
  166. }
  167. $data_query = mysqli_query($con, "select * from oder where uid='$uid'");
  168. $count = mysqli_num_rows($data_query);
  169. if ($count > 10) {
  170. // 用户已经申请过了,返回状态码1和提示信息
  171. $response = array('status' => 1, 'msg' => '您申请的订单已超限,请联系客服');
  172. echo json_encode($response);
  173. } else {
  174. // 订单写入数据库成功后执行写入佣金表
  175. $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')");
  176. // 写入佣金表
  177. $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')");
  178. }
  179. }
  180. }elseif ($group_name == '一级代理') {
  181. // 判断是否有name,uid,phone,dizhi这四项信息,如果有任何一项为空,则返回错误信息
  182. if (empty($name) || empty($uid) || empty($phone) || empty($dizhi)) {
  183. $response = array('status' => -1, 'msg' => '信息不完整,请重新输入');
  184. echo json_encode($response);
  185. return;
  186. }
  187. // 判断订单是否已经提交过
  188. session_start();
  189. if (isset($_SESSION['order_id']) && $_SESSION['order_id'] == $oderid) {
  190. $response = array('status' => 1, 'msg' => '订单已经提交过了');
  191. echo json_encode($response);
  192. return;
  193. }
  194. $data_query = mysqli_query($con, "select * from oder where uid='$uid'");
  195. $count = mysqli_num_rows($data_query);
  196. if ($count > 10) {
  197. // 用户已经申请过了,返回状态码1和提示信息
  198. $response = array('status' => 1, 'msg' => '您申请的订单已超限,请联系客服');
  199. echo json_encode($response);
  200. } else {
  201. // 订单写入数据库成功后执行写入佣金表
  202. $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')");
  203. // 写入佣金表
  204. $data_revenue = mysqli_query($con, "INSERT INTO revenue(account, oderid, name, price, beizhu, cr_time) VALUES ('$proxy_acc', '$oderid', '$name', '$proxy_price', '$beizhu', '$time')");
  205. }
  206. } elseif ($group_name == '运营平台') {
  207. // 判断是否有name,uid,phone,dizhi这四项信息,如果有任何一项为空,则返回错误信息
  208. if (empty($name) || empty($uid) || empty($phone) || empty($dizhi)) {
  209. $response = array('status' => -1, 'msg' => '信息不完整,请重新输入');
  210. echo json_encode($response);
  211. return;
  212. }
  213. // 判断订单是否已经提交过
  214. session_start();
  215. if (isset($_SESSION['order_id']) && $_SESSION['order_id'] == $oderid) {
  216. $response = array('status' => 1, 'msg' => '订单已经提交过了');
  217. echo json_encode($response);
  218. return;
  219. }
  220. $data_query = mysqli_query($con, "select * from oder where uid='$uid'");
  221. $count = mysqli_num_rows($data_query);
  222. if ($count > 10) {
  223. // 用户已经申请过了,返回状态码1和提示信息
  224. $response = array('status' => 1, 'msg' => '您申请的订单已超限,请联系客服');
  225. echo json_encode($response);
  226. } else {
  227. // 订单写入数据库成功后执行写入佣金表
  228. $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')");
  229. // 写入佣金表
  230. $data_revenue = mysqli_query($con, "INSERT INTO revenue(account, oderid, name, price, beizhu, cr_time) VALUES ('$proxy_acc', '$oderid', '$name', '$proxy_price', '$beizhu', '$time')");
  231. }
  232. }
  233. $sql = "SELECT haokaid FROM list WHERE name='$goods'";
  234. $res = mysqli_query($con, $sql);
  235. if (mysqli_num_rows($res) > 0) {
  236. $row = mysqli_fetch_assoc($res);
  237. $haokaid = $row['haokaid'];
  238. }
  239. if ($haokaid === null || $haokaid === '') {
  240. // $haokaid为null或空,执行特定的代码
  241. $response = array('status' => 0, 'msg' => '申请成功');
  242. echo json_encode($response);
  243. // 将订单号写入session
  244. $_SESSION['order_id'] = $oderid;
  245. //发送短信通知
  246. sendSms($appId, $appKey, $phoneNumber, $templateId, $smsSign);
  247. exit;
  248. } else {
  249. function buildSign($param, $token)
  250. {
  251. $str = 'share_id=' . $param['share_id'] . '&sku=' . $param['sku'] . '&source_id=' . $param['source_id'] . $token;
  252. $sign = md5($str);
  253. return $sign;
  254. }
  255. // 构造请求参数
  256. $params = array(
  257. 'sku' => $haokaid,
  258. 'share_id' => $userid,
  259. 'source_id' => $oderid,
  260. 'id_name' => $name,
  261. 'id_num' => $uid,
  262. 'mobile' => $phone,
  263. 'name' => $name,
  264. 'province' => $sf,
  265. 'city' => $city,
  266. 'district' => $xian,
  267. 'address' => $dizhi,
  268. );
  269. // 计算签名
  270. $sign = buildSign($params, $token);
  271. // 添加签名到请求参数
  272. $params['sign'] = $sign;
  273. // 发送请求
  274. $ch = curl_init();
  275. curl_setopt($ch, CURLOPT_URL, 'http://notify.91haoka.cn/api/plan-market/order/purchase');
  276. curl_setopt($ch, CURLOPT_POST, true);
  277. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  278. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  279. $response = curl_exec($ch);
  280. curl_close($ch);
  281. // echo "data:" . $response . "\n";
  282. // exit;
  283. // 解析响应数据
  284. $result = json_decode($response, true);
  285. if ($result) {
  286. if ($result['data']['status'] == 120) {
  287. // 更新订单状态
  288. $update_sql = "UPDATE oder SET status=1, beizhu='$beizhus' WHERE oderid='$oderid'";
  289. $update_sql_revenue = "UPDATE revenue SET oder_sta=1 WHERE oderid='$oderid'";
  290. $update_query = mysqli_query($con, $update_sql);
  291. if ($update_query) {
  292. // 更新成功,返回状态码0和提示信息
  293. $response = array('status' => 0, 'msg' => '申请成功');
  294. echo json_encode($response);
  295. // 将订单号写入session
  296. $_SESSION['order_id'] = $oderid;
  297. //发送短信通知
  298. sendSms($appId, $appKey, $phoneNumber, $templateId, $smsSign);
  299. } else {
  300. // 更新失败,返回状态码-2和提示信息
  301. $response = array('status' => -2, 'msg' => '更新订单状态失败');
  302. echo json_encode($response);
  303. }
  304. } else {
  305. // 下单失败,返回接口返回的错误信息
  306. $response = array('status' => $result['code'], 'msg' => $result['message']);
  307. echo json_encode($response);
  308. }
  309. } else {
  310. // 解析失败,返回错误信息
  311. $response = array('status' => -1, 'msg' => '解析响应数据失败');
  312. echo json_encode($response);
  313. }
  314. }
  315. }