getUserInfo.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. if (!session_id()) {
  3. session_start();
  4. }
  5. require_once("config.php");
  6. if (!isset($_POST['appToken'])) {
  7. $data = [
  8. 'code' => 1000,
  9. 'msg' => 'URL缺少appToken参数'
  10. ];
  11. exit(json_encode($data));
  12. }
  13. if (!isset($_POST['activityID'])) {
  14. $data = [
  15. 'code' => 1001,
  16. 'msg' => 'URL缺少activityID参数'
  17. ];
  18. exit(json_encode($data));
  19. }
  20. $appToken = $_POST['appToken'];
  21. $activityID = $_POST['activityID'];
  22. $params = array();
  23. $params['token'] = $appToken;
  24. $ch = curl_init();
  25. $header = array();
  26. $header[] = 'Content-Type: application/json;charset=UTF-8';
  27. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  28. $data = $params;
  29. if (is_array($params) && count($params) > 0) {
  30. $data = http_build_query($params);
  31. }
  32. $url = stripos(USER_API, '?') ? USER_API . '&' . $data : USER_API . '?' . $data;
  33. curl_setopt($ch, CURLOPT_URL, $url);
  34. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  35. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  36. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  37. $result = curl_exec($ch);
  38. curl_close($ch);
  39. $result = json_decode($result, true);
  40. if(ENABLE_TEST == 1 && SKIP_USER_API == 1)
  41. {
  42. $result = [
  43. "code" => 200,
  44. "data" => [
  45. "userNumber" => "11197381_xh",
  46. "userName" => "毛清艳",
  47. "userId" => 25592,
  48. "umNo" => "E00011197381"
  49. ]
  50. ];
  51. }
  52. else if ($result['code'] != 200) {
  53. $data = [
  54. 'code' => $result['code'],
  55. 'msg' => $result['msg']
  56. ];
  57. exit(json_encode($data));
  58. }
  59. $config = [
  60. 'token' => session_id(),
  61. 'tips' => '',
  62. 'activityName' => '',
  63. 'lotteryItems' => [],
  64. 'lotteryPrize' => [],
  65. 'questions' => []
  66. ];
  67. $dbLink = new mysqli(DB_HOST, DB_USER, DB_PWD, DB_NAME, DB_PORT);
  68. if ($dbLink->connect_error) {
  69. if (isset($_GET["showerror"])) {
  70. die("连接失败:" . $dbLink->connect_error);
  71. }
  72. }
  73. //获取活动名称和领奖提示
  74. $sqlConfig = "SELECT `activity_name`, `activity_tips`, `bg_img`, `title_img` FROM `pingan_activity_list` WHERE `id` = $activityID";
  75. $configResult = $dbLink->query($sqlConfig);
  76. if ($configResult == false) {
  77. $data = [
  78. 'code' => 1002,
  79. 'msg' => '活动不存在'
  80. ];
  81. exit(json_encode($data));
  82. }
  83. $configData = $configResult->fetch_assoc();
  84. $configResult->free();
  85. if (count($configData) > 0) {
  86. $config['tips'] = $configData['activity_tips'];
  87. $config['activityName'] = $configData['activity_name'];
  88. $config['bg_img'] = $configData['bg_img'] ? (URL_FOR_SAVE_PATH . $configData['bg_img']) : null;
  89. $config['title_img'] = $configData['title_img'] ? (URL_FOR_SAVE_PATH . $configData['title_img']) : null;
  90. }
  91. //获取奖品列表
  92. $prizeSql = "SELECT `order`, `prize_name`, `prize_type`, `prize_img` FROM `pingan_prize_config` WHERE `activity_id` = $activityID ORDER BY `order` ASC";
  93. //echo $prizeSql;
  94. $prizeResult = $dbLink->query($prizeSql);
  95. while ($prizeData = $prizeResult->fetch_assoc()) {
  96. $config['lotteryPrize'][$prizeData['order']] = $prizeData['prize_name'];
  97. $config['lotteryItems'][$prizeData['order']] = $prizeData['prize_type'];
  98. $config['lotteryImgs'][$prizeData['order']] = $prizeData['prize_img'] ? (URL_FOR_SAVE_PATH . $prizeData['prize_img']) : null;
  99. }
  100. $prizeResult->free();
  101. //获取问题列表
  102. $questionSql = "SELECT `id`, `type`, `question`, `options` FROM `pingan_question_list` WHERE `activity_id` = $activityID ORDER BY `order` ASC";
  103. //echo $questionSql;
  104. $questionResult = $dbLink->query($questionSql);
  105. while ($questionData = $questionResult->fetch_assoc()) {
  106. $config['questions'][] = array(
  107. 'id' => $questionData['id'],
  108. 'type' => $questionData['type'],
  109. 'question' => $questionData['question'],
  110. 'options' => $questionData['options'] == null ? null : json_decode($questionData['options'], true)
  111. );
  112. }
  113. $questionResult->free();
  114. //获取玩家信息
  115. $userID = $result['data']['userId'];
  116. $sql = "SELECT `user_id`, `user_name`, `user_number`, `options`, `custom_input`, `status`, `prize`, `update_time` FROM `pingan_user_info` WHERE `user_id` = $userID AND `activity_id` = $activityID";
  117. //echo $sql;
  118. $queryResult = $dbLink->query($sql);
  119. $rows = $queryResult->fetch_assoc();
  120. $queryResult->free();
  121. if ($rows == false || count($rows) == 0) {
  122. $insertSql = 'INSERT INTO `pingan_user_info` (`activity_id`,`user_id`, `user_number`, `user_name`, `um`) VALUES(?, ?, ?, ?, ?)';
  123. if (!($stmt = $dbLink->prepare($insertSql))) {
  124. echo "Prepare failed: (" . $dbLink->errno . ") " . $dbLink->error;
  125. }
  126. $stmt->bind_param("iisss", $activityID, $userID, $result['data']['userNumber'], $result['data']['userName'], $result['data']['umNo']);
  127. $stmt->execute();
  128. $queryResult = $dbLink->query($sql);
  129. $rows = $queryResult->fetch_assoc();
  130. $queryResult->free();
  131. $data = [
  132. 'code' => 200,
  133. 'data' => array_merge($rows, $config)
  134. ];
  135. echo json_encode($data);
  136. } else {
  137. $data = [
  138. 'code' => 200,
  139. 'data' => array_merge($rows, $config)
  140. ];
  141. echo json_encode($data);
  142. }
  143. $_SESSION['userID'] = $userID;
  144. $dbLink->close();