dict_lookup_pre.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. //查询参考字典
  3. include("../log/pref_log.php");
  4. require_once '../config.php';
  5. require_once '../public/_pdo.php';
  6. require_once '../redis/function.php';
  7. require_once '../dict/function.php';
  8. if (isset($_GET["language"])) {
  9. $currLanguage = $_GET["language"];
  10. } else {
  11. if (isset($_COOKIE["language"])) {
  12. $currLanguage = $_COOKIE["language"];
  13. } else {
  14. $currLanguage = "en";
  15. }
  16. }
  17. $currLanguage = explode("-", $currLanguage)[0];
  18. if (isset($_GET["word"]) && !empty($_GET["word"])) {
  19. $word = $_GET["word"];
  20. } else {
  21. echo json_encode(array(), JSON_UNESCAPED_UNICODE);
  22. exit;
  23. }
  24. $redis = redis_connect();
  25. if($redis!==false){
  26. $arrWordIdx = $redis->hget("ref_dict_idx",$word);
  27. if($arrWordIdx===FALSE){
  28. PDO_Connect(_FILE_DB_REF_INDEX_);
  29. $query = "SELECT word,count from " . _TABLE_REF_INDEX_ . " where eword like ? OR word like ? limit 0,15";
  30. $Fetch = PDO_FetchAll($query, array($word . '%', $word . '%'));
  31. $arrWordIdx=json_encode($Fetch, JSON_UNESCAPED_UNICODE);
  32. $redis->hset("ref_dict_idx",$word,$arrWordIdx);
  33. }
  34. $arrResult = json_decode($arrWordIdx,true);
  35. foreach ($arrResult as $key => $value) {
  36. # 获取字典里的第一个意思
  37. $arrResult[$key]["mean"]=getRefFirstMeaning($arrResult[$key]["word"],$currLanguage,$redis);
  38. }
  39. echo json_encode($arrResult, JSON_UNESCAPED_UNICODE);
  40. exit;
  41. }
  42. else
  43. {
  44. PDO_Connect(_FILE_DB_REF_INDEX_);
  45. $query = "SELECT word,count from " . _TABLE_REF_INDEX_ . " where eword like ? OR word like ? limit 0,15";
  46. $Fetch = PDO_FetchAll($query, array($word . '%', $word . '%'));
  47. PDO_Connect(_FILE_DB_REF_, _DB_USERNAME_, _DB_PASSWORD_);
  48. $query = "SELECT mean from " . _TABLE_DICT_REF_ . " where word = ? and language = ? limit 0,1";
  49. foreach ($Fetch as $key => $value) {
  50. # code...
  51. $mean = PDO_FetchRow($query, array($value["word"], $currLanguage));
  52. if ($mean) {
  53. $Fetch[$key]["mean"] = $mean["mean"];
  54. } else {
  55. if ($currLanguage != "en") {
  56. $mean = PDO_FetchRow($query, array($value["word"], "en"));
  57. if ($mean) {
  58. $Fetch[$key]["mean"] = $mean["mean"];
  59. } else {
  60. $Fetch[$key]["mean"] = "";
  61. }
  62. } else {
  63. $Fetch[$key]["mean"] = "";
  64. }
  65. }
  66. }
  67. echo json_encode($Fetch, JSON_UNESCAPED_UNICODE);
  68. }
  69. PrefLog();