term_post.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. $channelInfo = new Channal($redis);
  23. if ($_POST["id"] != "" && !isset($_POST['save_as'])) {
  24. #更新
  25. #先查询是否有权限
  26. #是否这个术语的作者
  27. $query = "SELECT id,channal,owner from "._TABLE_TERM_." where guid= ? ";
  28. $stmt = $PDO->prepare($query);
  29. $stmt->execute(array($_POST["id"]));
  30. if ($stmt) {
  31. $Fetch = $stmt->fetch(PDO::FETCH_ASSOC);
  32. if($Fetch){
  33. if($Fetch['owner']!=$_COOKIE["userid"]){
  34. #不是这个术语的作者,查是否是channel的有编辑权限者
  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= ? , 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["language"],
  56. $_POST["note"],
  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 "._TABLE_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. #TODO
  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($_POST["channal"]==""){
  111. $owner_uid = $_COOKIE["user_uid"];
  112. }else{
  113. $channel = $channelInfo->getChannal($_POST["channal"]);
  114. if($channelInfo){
  115. $owner_uid = $channel["owner_uid"];
  116. }else{
  117. $owner_uid = $_COOKIE["user_uid"];
  118. }
  119. }
  120. if ($stmt) {
  121. $Fetch = $stmt->fetch(PDO::FETCH_ASSOC);
  122. if($Fetch){
  123. $respond['status'] = 1;
  124. $respond['message'] = "已经有同样的记录";
  125. echo json_encode($respond, JSON_UNESCAPED_UNICODE);
  126. exit;
  127. }
  128. }
  129. $parm = [
  130. $snowflake->id(),
  131. UUID::v4(),
  132. $_POST["word"],
  133. pali2english($_POST["word"]),
  134. $_POST["mean"],
  135. $_POST["mean2"],
  136. $_POST["tag"],
  137. $_POST["channal"],
  138. $_POST["language"],
  139. $_POST["note"],
  140. $owner_uid,
  141. $_COOKIE["user_id"],
  142. mTime(),
  143. mTime()
  144. ];
  145. $query = "INSERT INTO "._TABLE_TERM_."
  146. (
  147. id,
  148. guid,
  149. word,
  150. word_en,
  151. meaning,
  152. other_meaning,
  153. tag,
  154. channal,
  155. language,
  156. note,
  157. owner,
  158. editor_id,
  159. create_time,
  160. modify_time
  161. )
  162. VALUES (?, ? , ?, ?, ?, ?, ?, ?, ?, ? , ?, ?, ?, ?) ";
  163. $stmt = @PDO_Execute($query, $parm);
  164. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  165. $error = PDO_ErrorInfo();
  166. $respond['status'] = 1;
  167. $respond['message'] = $error[2] . $query;
  168. } else {
  169. $respond['status'] = 0;
  170. $respond['message'] = $_POST["word"];
  171. $respond['data'] = [
  172. "id"=>$parm[0],
  173. "guid"=>$parm[1],
  174. "word"=>$parm[2],
  175. "word_en"=>$parm[3],
  176. "meaning"=>$parm[4],
  177. "other_meaning"=>$parm[5],
  178. "tag"=>$parm[6],
  179. "channal"=>$parm[7],
  180. "language"=>$parm[8],
  181. "note"=>$parm[9],
  182. "owner"=>$parm[10]
  183. ];
  184. }
  185. }
  186. #更新 redis
  187. if ($redis != false) {
  188. {
  189. # code...
  190. $query = "SELECT id,word,meaning,other_meaning,note,owner,language from "._TABLE_TERM_." where word = ? ";
  191. $stmt = $PDO->prepare($query);
  192. $stmt->execute(array($_POST["word"]));
  193. if ($stmt) {
  194. $Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
  195. $redisWord=array();
  196. foreach ($Fetch as $one) {
  197. # code...
  198. $redisWord[] = array($one["id"],
  199. $one["word"],
  200. "",
  201. "",
  202. "",
  203. $one["meaning"]."$".$one["other_meaning"],
  204. $one["note"],
  205. "",
  206. "",
  207. 1,
  208. 100,
  209. $one["owner"],
  210. "term",
  211. $one["language"]
  212. );
  213. }
  214. $redis->hSet("dict://term",$_POST["word"],json_encode($redisWord,JSON_UNESCAPED_UNICODE));
  215. }
  216. }
  217. }
  218. #更新redis结束
  219. echo json_encode($respond, JSON_UNESCAPED_UNICODE);