term_post.php 5.9 KB

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