HkOderApi.php 16 KB

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