ajax.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. <?php
  2. header('Content-type: application/json; charset=utf-8');
  3. require_once("config.php");
  4. //error_reporting(E_ERROR);
  5. function toCSV(array $data, array $colHeaders = array(), $asString = false)
  6. {
  7. $stream = ($asString)
  8. ? fopen("php://temp/maxmemory", "w+")
  9. : fopen("php://output", "w");
  10. if (!empty($colHeaders)) {
  11. fputcsv($stream, $colHeaders);
  12. }
  13. foreach ($data as $record) {
  14. fputcsv($stream, $record);
  15. }
  16. if ($asString) {
  17. rewind($stream);
  18. $returnVal = stream_get_contents($stream);
  19. fclose($stream);
  20. return $returnVal;
  21. } else {
  22. fclose($stream);
  23. }
  24. }
  25. $action = strtolower($_GET['action']);
  26. $dbLink = new mysqli(DB_HOST, DB_USER, DB_PWD, DB_NAME, DB_PORT);
  27. if ($dbLink->connect_error) {
  28. if (isset($_GET["showerror"])) {
  29. die("连接失败:" . $dbLink->connect_error);
  30. }
  31. }
  32. switch ($action) {
  33. case "userinfo": {
  34. $page = $_GET['page'];
  35. $pageSize = $_GET['limit'];
  36. $min = ($page - 1) * $pageSize;
  37. $activityID = $_GET['activityID'];
  38. $sql = "SELECT `user_id`, `user_name`, `user_number`, `um`, `options`, `custom_input`, `status`, `prize` FROM `pingan_user_info` WHERE `activity_id` = $activityID limit " . $min . ',' . $pageSize;
  39. $queryResult = $dbLink->query($sql);
  40. $userInfo = array();
  41. $resultCount = 0;
  42. if ($queryResult != false) {
  43. $userInfo = $queryResult->fetch_all(MYSQLI_ASSOC);
  44. $queryResult->free();
  45. }
  46. //获取奖品列表
  47. $prizeCfg = [];
  48. $prizeSql = "SELECT `order`, `prize_name` FROM `pingan_prize_config` WHERE `activity_id` = $activityID ORDER BY `order` ASC";
  49. //echo $prizeSql;
  50. $prizeResult = $dbLink->query($prizeSql);
  51. while ($prizeData = $prizeResult->fetch_assoc()) {
  52. $prizeCfg[$prizeData['order']] = $prizeData['prize_name'];
  53. }
  54. $prizeResult->free();
  55. for ($i = 0; $i < count($userInfo); ++$i) {
  56. $userInfo[$i]['prize'] = intval($userInfo[$i]['prize']) > 0 ? $prizeCfg[$userInfo[$i]['prize']] : "未抽奖";
  57. }
  58. $sqlCount = "SELECT COUNT(*) as cnt FROM `pingan_user_info` WHERE `activity_id` = $activityID";
  59. $queryResult = $dbLink->query($sqlCount);
  60. if ($queryResult != false) {
  61. $resultCount = $queryResult->fetch_assoc();
  62. $queryResult->free();
  63. }
  64. $data = [
  65. 'code' => 0,
  66. 'msg' => "",
  67. "count" => $resultCount['cnt'],
  68. 'data' => $userInfo
  69. ];
  70. echo json_encode($data);
  71. }
  72. break;
  73. case "prizeconfig": {
  74. $page = $_GET['page'];
  75. $pageSize = $_GET['limit'];
  76. $min = ($page - 1) * $pageSize;
  77. $activityID = $_GET['activityID'];
  78. $sql = "SELECT `id`, `order`, `prize_type`, `prize_name`, `prize_img`, `prize_rate`, `sum`, `cur` FROM `pingan_prize_config` WHERE `activity_id` = $activityID limit " . $min . ',' . $pageSize;
  79. $queryResult = $dbLink->query($sql);
  80. $userInfo = array();
  81. $resultCount = 0;
  82. if ($queryResult != false) {
  83. $userInfo = $queryResult->fetch_all(MYSQLI_ASSOC);
  84. $queryResult->free();
  85. }
  86. $sqlCount = "SELECT COUNT(*) as cnt FROM `pingan_prize_config` WHERE `activity_id` = $activityID";
  87. $queryResult = $dbLink->query($sqlCount);
  88. if ($queryResult != false) {
  89. $resultCount = $queryResult->fetch_assoc();
  90. $queryResult->free();
  91. }
  92. $data = [
  93. 'code' => 0,
  94. 'msg' => "",
  95. "count" => $resultCount['cnt'],
  96. 'data' => $userInfo
  97. ];
  98. echo json_encode($data);
  99. }
  100. break;
  101. case "modifyitem": {
  102. $itemID = $_POST['itemID'];
  103. $sum = $_POST['sum'];
  104. $rate = $_POST['rate'];
  105. $type = $_POST['type'];
  106. $name = $_POST['name'];
  107. $updateSql = 'UPDATE `pingan_prize_config` SET `sum` = ?, `prize_rate` = ?, `prize_name` = ?, `prize_type` = ? WHERE `id` = ?';
  108. if (!($stmt = $dbLink->prepare($updateSql))) {
  109. echo "Prepare failed: (" . $dbLink->errno . ") " . $dbLink->error;
  110. }
  111. $stmt->bind_param("idssi", $sum, $rate, $name, $type, $itemID);
  112. $stmt->execute();
  113. echo json_encode(array("code" => 200));
  114. }
  115. break;
  116. case "getconfig": {
  117. $sql = "SELECT `id`, `key`, `value` FROM `pingan_config` ";
  118. $queryResult = $dbLink->query($sql);
  119. $userInfo = array();
  120. $resultCount = 0;
  121. if ($queryResult != false) {
  122. $userInfo = $queryResult->fetch_all(MYSQLI_ASSOC);
  123. $queryResult->free();
  124. }
  125. echo json_encode(array("code" => 200, "data" => $userInfo));
  126. }
  127. break;
  128. case "modifyconfig": {
  129. $key = $_POST['key'];
  130. $value = $_POST['value'];
  131. $updateSql = 'UPDATE `pingan_config` SET `value` = ? WHERE `key` = ?';
  132. if (!($stmt = $dbLink->prepare($updateSql))) {
  133. echo "Prepare failed: (" . $dbLink->errno . ") " . $dbLink->error;
  134. }
  135. $stmt->bind_param("ss", $value, $key);
  136. $stmt->execute();
  137. echo json_encode(array("code" => 200));
  138. }
  139. break;
  140. case "export": {
  141. $activityID = $_GET['activityID'];
  142. $sql = "SELECT `user_id`, `user_name`, `user_number`, `options`, `custom_input`, `prize` FROM `pingan_user_info` WHERE `activity_id` = $activityID";
  143. $queryResult = $dbLink->query($sql);
  144. $userInfo = array();
  145. if ($queryResult != false) {
  146. $userInfo = $queryResult->fetch_all(MYSQLI_ASSOC);
  147. $queryResult->free();
  148. }
  149. header("Content-type:text/csv");
  150. header("Content-Disposition:attachment;filename=用户列表.csv");
  151. header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
  152. header('Expires:0');
  153. header('Pragma:public');
  154. toCSV($userInfo, ['用户ID', '员工姓名', '员工工号', '选择题', '问答题', '中奖奖品ID']);
  155. }
  156. break;
  157. case "getallusers": {
  158. $activityID = $_GET['activityID'];
  159. $sql = "SELECT `user_id`, `user_name`, `user_number`, `um`, `options`, `custom_input`, `prize` FROM `pingan_user_info` WHERE `activity_id` = $activityID";
  160. $queryResult = $dbLink->query($sql);
  161. $userInfo = array();
  162. if ($queryResult != false) {
  163. $userInfo = $queryResult->fetch_all(MYSQLI_ASSOC);
  164. $queryResult->free();
  165. }
  166. //获取奖品列表
  167. $prizeCfg = [];
  168. $prizeSql = "SELECT `order`, `prize_name` FROM `pingan_prize_config` WHERE `activity_id` = $activityID ORDER BY `order` ASC";
  169. //echo $prizeSql;
  170. $prizeResult = $dbLink->query($prizeSql);
  171. while ($prizeData = $prizeResult->fetch_assoc()) {
  172. $prizeCfg[$prizeData['order']] = $prizeData['prize_name'];
  173. }
  174. $prizeResult->free();
  175. for ($i = 0; $i < count($userInfo); ++$i) {
  176. $userInfo[$i]['prize'] = intval($userInfo[$i]['prize']) > 0 ? $prizeCfg[$userInfo[$i]['prize']] : "未抽奖";
  177. }
  178. echo json_encode(array("code" => 200, 'data'=>$userInfo));
  179. }
  180. break;
  181. case "resetdata": {
  182. $activityID = $_GET['activityID'];
  183. $sql = "UPDATE `pingan_prize_config` SET `cur` = 0 WHERE `activity_id` = $activityID";
  184. $dbLink->query($sql);
  185. $sql = "UPDATE `pingan_user_info` SET `status` = 0, `options` = NULL, `custom_input` = NULL, `prize` = 0 WHERE `activity_id` = $activityID";
  186. $dbLink->query($sql);
  187. echo json_encode(array("code" => 200, "msg" => $sql));
  188. }
  189. break;
  190. case "activitylist": {
  191. $page = $_GET['page'];
  192. $pageSize = $_GET['limit'];
  193. $min = ($page - 1) * $pageSize;
  194. $sql = "SELECT `id`, `activity_name`, `activity_tips`, `bg_img`, `title_img` FROM `pingan_activity_list` limit " . $min . ',' . $pageSize;
  195. $queryResult = $dbLink->query($sql);
  196. $userInfo = array();
  197. $resultCount = 0;
  198. if ($queryResult != false) {
  199. $userInfo = $queryResult->fetch_all(MYSQLI_ASSOC);
  200. $queryResult->free();
  201. }
  202. $sqlCount = "SELECT COUNT(*) as cnt FROM `pingan_activity_list` ";
  203. $queryResult = $dbLink->query($sqlCount);
  204. if ($queryResult != false) {
  205. $resultCount = $queryResult->fetch_assoc();
  206. $queryResult->free();
  207. }
  208. $data = [
  209. 'code' => 0,
  210. 'msg' => "",
  211. "count" => $resultCount['cnt'],
  212. 'data' => $userInfo
  213. ];
  214. echo json_encode($data);
  215. }
  216. break;
  217. case "modifyactivity": {
  218. $activityID = $_POST['activityID'];
  219. $activityName = $_POST['activityName'];
  220. $activityTips = $_POST['activityTips'];
  221. $updateSql = 'UPDATE `pingan_activity_list` SET `activity_name` = ?, `activity_tips` = ? WHERE `id` = ?';
  222. if (!($stmt = $dbLink->prepare($updateSql))) {
  223. echo "Prepare failed: (" . $dbLink->errno . ") " . $dbLink->error;
  224. }
  225. $stmt->bind_param("ssi", $activityName, $activityTips, $activityID);
  226. $stmt->execute();
  227. echo json_encode(array("code" => 200));
  228. }
  229. break;
  230. case "delactivity": {
  231. $activityID = $_POST['activityID'];
  232. $delSql = 'DELETE FROM `pingan_activity_list` WHERE `id` = ?';
  233. if (!($stmt = $dbLink->prepare($delSql))) {
  234. echo "Prepare failed: (" . $dbLink->errno . ") " . $dbLink->error;
  235. }
  236. $stmt->bind_param("i", $activityID);
  237. $stmt->execute();
  238. $delSql = 'DELETE FROM `pingan_prize_config` WHERE `activity_id` = ?';
  239. if (!($stmt = $dbLink->prepare($delSql))) {
  240. echo "Prepare failed: (" . $dbLink->errno . ") " . $dbLink->error;
  241. }
  242. $stmt->bind_param("i", $activityID);
  243. $stmt->execute();
  244. echo json_encode(array("code" => 200));
  245. }
  246. break;
  247. case "addactivity": {
  248. $activityName = $_POST['activityName'];
  249. $activityTips = $_POST['activityTips'];
  250. $insertSql = 'INSERT INTO `pingan_activity_list`(`activity_name`, `activity_tips`) VALUES (?, ?)';
  251. if (!($stmt = $dbLink->prepare($insertSql))) {
  252. echo "Prepare failed: (" . $dbLink->errno . ") " . $dbLink->error;
  253. }
  254. $stmt->bind_param("ss", $activityName, $activityTips);
  255. $stmt->execute();
  256. $activityID = mysqli_insert_id($dbLink);
  257. $insertSql = "";
  258. for ($i = 1; $i <= 8; $i++) {
  259. $prize = "奖品" . $i;
  260. $type = "奖项" . $i;
  261. $insertSql = "INSERT INTO `pingan_prize_config`(`order`, `activity_id`, `prize_name`, `prize_type`) VALUES ($i, $activityID, '$prize', '$type')";
  262. $dbLink->query($insertSql);
  263. }
  264. echo json_encode(array("code" => 200));
  265. }
  266. break;
  267. case "getquestionlist": {
  268. $page = $_GET['page'];
  269. $pageSize = $_GET['limit'];
  270. $min = ($page - 1) * $pageSize;
  271. $activityID = intval($_GET['activityID']);
  272. $sql = "SELECT `id`, `order`, `activity_id`, `type`, `question`, `options`, `answer` FROM `pingan_question_list` WHERE `activity_id` = $activityID limit " . $min . ',' . $pageSize;
  273. $queryResult = $dbLink->query($sql);
  274. $userInfo = array();
  275. $resultCount = 0;
  276. if ($queryResult != false) {
  277. $userInfo = $queryResult->fetch_all(MYSQLI_ASSOC);
  278. $queryResult->free();
  279. }
  280. $sqlCount = "SELECT COUNT(*) as cnt FROM `pingan_question_list` WHERE `activity_id` = $activityID";
  281. $queryResult = $dbLink->query($sqlCount);
  282. if ($queryResult != false) {
  283. $resultCount = $queryResult->fetch_assoc();
  284. $queryResult->free();
  285. }
  286. $data = [
  287. 'code' => 0,
  288. 'msg' => "",
  289. "count" => $resultCount['cnt'],
  290. 'data' => $userInfo
  291. ];
  292. echo json_encode($data);
  293. }
  294. break;
  295. case "addquestion": {
  296. $activityID = intval($_POST['activityID']);
  297. $order = intval($_POST['order']);
  298. $curQuestionType = intval($_POST['type']);
  299. $question = strval($_POST['question']);
  300. $options = strval($_POST['options']);
  301. $answers = strval($_POST['answers']);
  302. $options = $options == "" ? null : $options;
  303. $answers = $answers == "" ? null : $answers;
  304. $insertSql = 'INSERT INTO `pingan_question_list`(`order`, `activity_id`, `type`, `question`, `options`, `answer`) VALUES (?, ?, ?, ?, ?, ?)';
  305. if (!($stmt = $dbLink->prepare($insertSql))) {
  306. echo "Prepare failed: (" . $dbLink->errno . ") " . $dbLink->error;
  307. }
  308. $stmt->bind_param("iiisss", $order, $activityID, $curQuestionType, $question, $options, $answers);
  309. $ret = $stmt->execute();
  310. echo json_encode(array("code" => 200, 's' => $answers));
  311. }
  312. break;
  313. case "modifyquestion": {
  314. $questionID = intval($_POST['questionID']);
  315. $activityID = intval($_POST['activityID']);
  316. $order = intval($_POST['order']);
  317. $type = intval($_POST['type']);
  318. $question = strval($_POST['question']);
  319. $options = strval($_POST['options']);
  320. $answers = strval($_POST['answers']);
  321. $options = $options == "" ? null : $options;
  322. $answers = $answers == "" ? null : $answers;
  323. $updateSql = 'UPDATE `pingan_question_list` SET `order` = ?, `type` = ?, `question` = ?, `options` = ?, `answer` = ? WHERE `id` = ?';
  324. if (!($stmt = $dbLink->prepare($updateSql))) {
  325. echo "Prepare failed: (" . $dbLink->errno . ") " . $dbLink->error;
  326. }
  327. $stmt->bind_param("iisssi", $order, $type, $question, $options, $answers, $questionID);
  328. $stmt->execute();
  329. echo json_encode(array("code" => 200, 'options' => $options, 'answers' => $answers));
  330. }
  331. break;
  332. case "delquestion": {
  333. $questionID = $_POST['questionID'];
  334. $delSql = 'DELETE FROM `pingan_question_list` WHERE `id` = ?';
  335. if (!($stmt = $dbLink->prepare($delSql))) {
  336. echo "Prepare failed: (" . $dbLink->errno . ") " . $dbLink->error;
  337. }
  338. $stmt->bind_param("i", $questionID);
  339. $stmt->execute();
  340. echo json_encode(array("code" => 200));
  341. }
  342. break;
  343. case "uploadimg": {
  344. $data = array(
  345. "code" => 200,
  346. "msg" => ""
  347. );
  348. $activityID = intval($_POST['activityID']);
  349. $prizeID = intval($_POST['id']);
  350. $imgFile = $_FILES["prizeImg"];
  351. $imgType = $_POST['img'];
  352. $upType = $_POST['type'];
  353. if(empty($imgFile)){
  354. $data['code'] = 1;
  355. $data['msg'] = "未获取到上传文件";
  356. exit(json_encode($data));
  357. }
  358. $picname = $imgFile["name"];
  359. $picsize = $imgFile["size"];
  360. if($picsize > 1*1024*1024){
  361. $data['code'] = 1;
  362. $data['msg'] = "图片大小不能超过1MB 请重新选择";
  363. exit(json_encode($data));
  364. }
  365. $type = strstr($picname, ".");
  366. if($type!=".png"){
  367. $data['code'] = 2;
  368. $data['msg'] = "请上传png格式的图片";
  369. exit(json_encode($data));
  370. }
  371. $rootDir = 'LotteryCustomImg';
  372. if($upType == "prize")
  373. {
  374. $newPicName = $activityID . '_' . $prizeID . '_' . time() . '_' . rand(1,9999).$type;//1970-1-1
  375. }
  376. else{
  377. $newPicName = $activityID . '_' . $imgType . '_' . time() . '_' . rand(1,9999).$type;//1970-1-1
  378. }
  379. $dateFolderName = date("Y_m_d");
  380. $targetDir = SAVE_ROOT_PATH . $rootDir . '/' . $dateFolderName . '/';
  381. if(!is_dir($targetDir))
  382. {
  383. $succ = mkdir($targetDir, 0755, true);
  384. if(!$succ)
  385. {
  386. $data['code'] = 3;
  387. $data['msg'] = "创建目录" . $targetDir . "失败";
  388. exit(json_encode($data));
  389. }
  390. }
  391. $targetFile = $targetDir . $newPicName;
  392. $saveRet = move_uploaded_file($imgFile['tmp_name'], $targetFile);
  393. if($saveRet == true) {
  394. $dbPath = $rootDir . '/' . $dateFolderName . '/' . $newPicName;
  395. if($upType == "prize")
  396. {
  397. $updateSql = 'UPDATE `pingan_prize_config` SET `prize_img` = ? WHERE `id` = ?';
  398. if (!($stmt = $dbLink->prepare($updateSql))) {
  399. echo "Prepare failed: (" . $dbLink->errno . ") " . $dbLink->error;
  400. }
  401. $stmt->bind_param("si", $dbPath, $prizeID);
  402. $stmt->execute();
  403. }
  404. else{
  405. $updateSql = 'UPDATE `pingan_activity_list` SET `bg_img` = ? WHERE `id` = ?';
  406. if($imgType == 'title')
  407. {
  408. $updateSql = 'UPDATE `pingan_activity_list` SET `title_img` = ? WHERE `id` = ?';
  409. }
  410. if (!($stmt = $dbLink->prepare($updateSql))) {
  411. echo "Prepare failed: (" . $dbLink->errno . ") " . $dbLink->error;
  412. }
  413. $stmt->bind_param("si", $dbPath, $activityID);
  414. $stmt->execute();
  415. }
  416. $data['code'] = 200;
  417. $data['msg'] = "上传成功";
  418. $data['url'] = URL_FOR_SAVE_PATH . $dbPath;
  419. }else{
  420. $data['code'] = 4;
  421. $data['msg'] = "保存图片失败:" . $targetFile;
  422. }
  423. echo json_encode($data);
  424. }
  425. break;
  426. case "delimg": {
  427. $activityID = intval($_POST['activityID']);
  428. $prizeID = intval($_POST['itemID']);
  429. $type = ($_POST['type']);
  430. $data = array(
  431. "code" => 200,
  432. "msg" => ""
  433. );
  434. if($type == 'prize')
  435. {
  436. $updateSql = 'UPDATE `pingan_prize_config` SET `prize_img` = ? WHERE `id` = ?';
  437. if (!($stmt = $dbLink->prepare($updateSql))) {
  438. echo "Prepare failed: (" . $dbLink->errno . ") " . $dbLink->error;
  439. }
  440. $c = null;
  441. $stmt->bind_param("si", $c, $prizeID);
  442. $stmt->execute();
  443. }
  444. else
  445. {
  446. $imgIdx = intval($_POST['imgIdx']);
  447. $updateSql = 'UPDATE `pingan_activity_list` SET `bg_img` = ? WHERE `id` = ?';
  448. if($imgIdx == 2)
  449. {
  450. $updateSql = 'UPDATE `pingan_activity_list` SET `title_img` = ? WHERE `id` = ?';
  451. }
  452. if (!($stmt = $dbLink->prepare($updateSql))) {
  453. echo "Prepare failed: (" . $dbLink->errno . ") " . $dbLink->error;
  454. }
  455. $img = null;
  456. $stmt->bind_param("si", $img, $activityID);
  457. $stmt->execute();
  458. }
  459. echo json_encode($data);
  460. }
  461. break;
  462. default:
  463. echo 'failed';
  464. break;
  465. }