term_post.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /*
  3. 修改术语
  4. */
  5. require_once "../config.php";
  6. require_once "../public/_pdo.php";
  7. require_once '../public/function.php';
  8. require_once "../redis/function.php";
  9. require_once "../channal/function.php";
  10. $redis = redis_connect();
  11. #未登录不能修改
  12. if (isset($_COOKIE["userid"]) == false) {
  13. $respond['status'] = 1;
  14. $respond['message'] = "not yet log in";
  15. echo json_encode($respond, JSON_UNESCAPED_UNICODE);
  16. exit;
  17. }
  18. $respond = array("status" => 0, "message" => "");
  19. PDO_Connect("" . _FILE_DB_TERM_);
  20. if ($_POST["id"] != "") {
  21. #更新
  22. #先查询是否有权限
  23. #是否这个术语的作者
  24. $query = "SELECT id,channal,owner from term where guid= ? ";
  25. $stmt = $PDO->prepare($query);
  26. $stmt->execute(array($_POST["id"]));
  27. if ($stmt) {
  28. $Fetch = $stmt->fetch(PDO::FETCH_ASSOC);
  29. if($Fetch){
  30. if($Fetch['owner']!=$_COOKIE["userid"]){
  31. #不是这个术语的作者,查是否是channel的有编辑权限者
  32. $channelInfo = new Channal($redis);
  33. $channelPower = $channelInfo->getPower($Fetch['channal']);
  34. if($channelPower<20){
  35. $respond['status'] = 1;
  36. $respond['message'] = "no power";
  37. echo json_encode($respond, JSON_UNESCAPED_UNICODE);
  38. exit;
  39. }
  40. }
  41. }else{
  42. $respond['status'] = 1;
  43. $respond['message'] = "no word";
  44. echo json_encode($respond, JSON_UNESCAPED_UNICODE);
  45. exit;
  46. }
  47. }
  48. $query = "UPDATE term SET meaning= ? ,other_meaning = ? , tag= ? ,channal = ? , language = ? , note = ? , receive_time= ?, modify_time= ? where guid= ? ";
  49. $stmt = @PDO_Execute($query,
  50. array($_POST["mean"],
  51. $_POST["mean2"],
  52. $_POST["tag"],
  53. $_POST["channal"],
  54. $_POST["language"],
  55. $_POST["note"],
  56. mTime(),
  57. mTime(),
  58. $_POST["id"],
  59. ));
  60. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  61. $error = PDO_ErrorInfo();
  62. $respond['status'] = 1;
  63. $respond['message'] = $error[2] . $query;
  64. } else {
  65. $respond['status'] = 0;
  66. $respond['message'] = $_POST["word"];
  67. $respond['data'] = ["guid"=>$_POST["id"],
  68. "word"=>$_POST["word"],
  69. "word_en"=>$_POST["word"],
  70. "meaning"=>$_POST["mean"],
  71. "other_meaning"=>$_POST["mean2"],
  72. "tag"=>$_POST["tag"],
  73. "channal"=>$_POST["channal"],
  74. "language"=>$_POST["language"],
  75. "note"=>$_POST["note"],
  76. "owner"=>$_COOKIE["userid"]
  77. ];
  78. }
  79. } else {
  80. #新建
  81. if(trim($_POST["word"])==""){
  82. $respond['status'] = 1;
  83. $respond['message'] = "pali word cannot be empty";
  84. echo json_encode($respond, JSON_UNESCAPED_UNICODE);
  85. exit;
  86. }
  87. if(trim($_POST["mean"])==""){
  88. $respond['status'] = 1;
  89. $respond['message'] = "meaning cannot be empty";
  90. echo json_encode($respond, JSON_UNESCAPED_UNICODE);
  91. exit;
  92. }
  93. if(trim($_POST["language"])==""){
  94. $respond['status'] = 1;
  95. $respond['message'] = "language cannot be empty";
  96. echo json_encode($respond, JSON_UNESCAPED_UNICODE);
  97. exit;
  98. }
  99. #先查询是否有重复数据
  100. if($_POST["channal"]==""){
  101. $query = "SELECT id from term where word= ? and language=? and tag=? and owner = ? ";
  102. $stmt = $PDO->prepare($query);
  103. $stmt->execute(array($_POST["word"],$_POST["language"],$_POST["tag"],$_COOKIE["userid"]));
  104. }else{
  105. $query = "SELECT id from term where word= ? and channal=? and tag=? and owner = ? ";
  106. $stmt = $PDO->prepare($query);
  107. $stmt->execute(array($_POST["word"],$_POST["channal"],$_POST["tag"],$_COOKIE["userid"]));
  108. }
  109. if ($stmt) {
  110. $Fetch = $stmt->fetch(PDO::FETCH_ASSOC);
  111. if($Fetch){
  112. $respond['status'] = 1;
  113. $respond['message'] = "已经有同样的记录";
  114. echo json_encode($respond, JSON_UNESCAPED_UNICODE);
  115. exit;
  116. }
  117. }
  118. $parm[] = UUID::v4();
  119. $parm[] = $_POST["word"];
  120. $parm[] = pali2english($_POST["word"]);
  121. $parm[] = $_POST["mean"];
  122. $parm[] = $_POST["mean2"];
  123. $parm[] = $_POST["tag"];
  124. $parm[] = $_POST["channal"];
  125. $parm[] = $_POST["language"];
  126. $parm[] = $_POST["note"];
  127. $parm[] = $_COOKIE["userid"];
  128. $parm[] = 0;
  129. $parm[] = mTime();
  130. $parm[] = mTime();
  131. $parm[] = mTime();
  132. $query = "INSERT INTO term (id, guid, word, word_en, meaning, other_meaning, tag, channal, language,note,owner,hit,create_time,modify_time,receive_time )
  133. VALUES (NULL, ? , ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";
  134. $stmt = @PDO_Execute($query, $parm);
  135. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  136. $error = PDO_ErrorInfo();
  137. $respond['status'] = 1;
  138. $respond['message'] = $error[2] . $query;
  139. } else {
  140. $respond['status'] = 0;
  141. $respond['message'] = $_POST["word"];
  142. $respond['data'] = ["guid"=>$parm[0],
  143. "word"=>$parm[1],
  144. "word_en"=>$parm[2],
  145. "meaning"=>$parm[3],
  146. "other_meaning"=>$parm[4],
  147. "tag"=>$parm[5],
  148. "channal"=>$parm[6],
  149. "language"=>$parm[7],
  150. "note"=>$parm[8],
  151. "owner"=>$parm[9]
  152. ];
  153. }
  154. }
  155. #更新 redis
  156. if ($redis != false) {
  157. {
  158. # code...
  159. $query = "SELECT id,word,meaning,other_meaning,note,owner,language from term where word = ? ";
  160. $stmt = $PDO->prepare($query);
  161. $stmt->execute(array($_POST["word"]));
  162. if ($stmt) {
  163. $Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
  164. $redisWord=array();
  165. foreach ($Fetch as $one) {
  166. # code...
  167. $redisWord[] = array($one["id"],
  168. $one["word"],
  169. "",
  170. "",
  171. "",
  172. $one["meaning"]."$".$one["other_meaning"],
  173. $one["note"],
  174. "",
  175. "",
  176. 1,
  177. 100,
  178. $one["owner"],
  179. "term",
  180. $one["language"]
  181. );
  182. }
  183. $redis->hSet("dict://term",$_POST["word"],json_encode($redisWord,JSON_UNESCAPED_UNICODE));
  184. }
  185. }
  186. }
  187. #更新redis结束
  188. echo json_encode($respond, JSON_UNESCAPED_UNICODE);