这个类对前段提交的查询进行分类处理,如果是查询则返回翻译,如果是发送的补全提示,则返回最近接用户输入的前10个单词,供用户选择
<?php
require_once "storeWord.class.php";
require_once "filterword.class.php";
require_once "format.class.php";
require_once "processCheck.class.php";
require_once "cookie.php";
require_once "sql.class.php";
require_once "./addword/addredis.class.php";
header("Content-type: plain/text; charset=utf-8");
header("Cache-control:no-store");
if(!empty($_GET['enword']))
{
$en=strtolower(trim($_GET['enword']));
$filter = new Filter($en);
$en=$filter->getResult();
$check = new Check();
$translation = new FormatTrans;
//返回ajax查询结果
if($_GET['type']==1)
{
$redis=new StoreWord();
$addredis = new AddRedis();
$wordZone=$check->matchDic($en);
//将查询过的单词根据cookie写入到数据库
$username=setOrGetUser();
$sql = new Sql($username, $en);
//$sql->setWord($en);
$ch=$redis->getWord($wordZone,$en);
$res['flag']=1;
$trans='';
if($ch===0)
{
//从redis中找到用户添加的单词对应的ID,并根据此ID从MySQL中找到用户添加的词条对应的翻译
$chinese = $addredis->getTrans($en);
$trans = ($chinese == 0 ? array("没有找到对应的翻译") : $chinese);
//$trans = array("没有找到对应的翻译");
}
else
{
$trans = $translation->trans($ch);
}
$res['result']=$trans;
echo json_encode($res);
}
//返回ajax自动补全集
else if($_GET['type']==2)
{
$matchWord=$check->ajaxMatch($en);
//file_put_contents("word.txt", $en."---".serialize($matchWord)."\r\n", FILE_APPEND);
$res['flag']=2;
$res['result']=$matchWord;
echo json_encode($res);
}
}
else
{
file_put_contents('log.txt', "receive NON data \r\n",FILE_APPEND);
}