DictController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Facades\App;
  4. use App\Models\UserDict;
  5. use App\Models\DictInfo;
  6. use App\Models\GroupMember;
  7. use Illuminate\Http\Request;
  8. use App\Tools\CaseMan;
  9. use Illuminate\Support\Facades\Log;
  10. use App\Http\Api\DictApi;
  11. use App\Http\Api\AuthApi;
  12. require_once __DIR__."/../../../public/app/dict/grm_abbr.php";
  13. class DictController extends Controller
  14. {
  15. /**
  16. * Display a listing of the resource.
  17. *
  18. * @return \Illuminate\Http\Response
  19. */
  20. public function index(Request $request)
  21. {
  22. //
  23. $startAt = microtime(true);
  24. $output = [];
  25. $wordDataOutput = [];
  26. $dictListOutput = [];
  27. $caseListOutput = [];
  28. $indexCol = ['word','note','dict_id'];
  29. $words = [];
  30. $word_base = [];
  31. $searched = [];
  32. $words[$request->get('word')] = [];
  33. $userLang = $request->get('lang',"zh");
  34. /**
  35. * 临时代码判断是否在缅汉字典群里面。在群里的用户可以产看缅汉字典pdf
  36. */
  37. $user = AuthApi::current($request);
  38. if($user){
  39. $inMyHanGroup = GroupMember::where('group_id','905af467-1bde-4d2c-8dc7-49cfb74e0b09')
  40. ->where('user_id',$user['user_uid'])->exists();
  41. }else{
  42. $inMyHanGroup = false;
  43. }
  44. if (App::environment('local')) {
  45. // The environment is local
  46. $inMyHanGroup = true;
  47. }
  48. $resultCount=0;
  49. for ($i=0; $i < 2; $i++) {
  50. # code...
  51. $word_base = [];
  52. foreach ($words as $word => $case) {
  53. # code...
  54. $searched[] = $word;
  55. $table = UserDict::select($indexCol)
  56. ->where('word',$word)
  57. ->where('source','_PAPER_');
  58. if(!$inMyHanGroup){
  59. $table = $table->where('dict_id','<>','8ae6e0f5-f04c-49fc-a355-4885cc08b4b3');
  60. //测试代码
  61. //$table = $table->where('dict_id','<>','ac9b7b73-b9c0-4d31-a5c9-7c6dc5a2c187');
  62. }
  63. $result = $table->get();
  64. $resultCount += count($result);
  65. $anchor = $word;
  66. $wordData=[
  67. 'word'=> $word,
  68. 'factors'=> "",
  69. 'parents'=> "",
  70. 'case'=> [],
  71. 'grammar'=>$case,
  72. 'anchor'=> $anchor,
  73. 'dict' => [],
  74. ];
  75. /**
  76. * 按照语言调整词典顺序
  77. * 算法:准备理想的词典顺序容器。
  78. * 将查询的结果放置在对应的容器中。
  79. * 最后将结果扁平化
  80. * 准备字典容器
  81. * $wordDict = [
  82. * "zh"=>[
  83. * "0d79e8e8-1430-4c99-a0f1-b74f2b4b26d8"=>[];
  84. * ]
  85. * ]
  86. */
  87. foreach (DictApi::langOrder($userLang) as $langId) {
  88. # code...
  89. $dictContainer = [];
  90. foreach (DictApi::dictOrder($langId) as $dictId) {
  91. $dictContainer[$dictId] = [];
  92. }
  93. $wordDict[$langId] = $dictContainer;
  94. }
  95. $dictList=[
  96. 'href'=> '#'.$anchor,
  97. 'title'=> "{$word}",
  98. 'children' => [],
  99. ];
  100. foreach ($result as $key => $value) {
  101. # code...
  102. $dictInfo= DictInfo::find($value->dict_id);
  103. $dict_lang = explode('-',$dictInfo->dest_lang);
  104. $anchor = "{$word}-{$dictInfo->shortname}";
  105. $currData = [
  106. 'dictname'=> $dictInfo->name,
  107. 'shortname'=> $dictInfo->shortname,
  108. 'description'=>$dictInfo->description,
  109. 'dict_id' => $value->dict_id,
  110. 'lang' => $dict_lang[0],
  111. 'word'=> $word,
  112. 'note'=> $this->GrmAbbr($value->note,0),
  113. 'anchor'=> $anchor,
  114. ];
  115. if(isset($wordDict[$dict_lang[0]])){
  116. if(isset($wordDict[$dict_lang[0]][$value->dict_id])){
  117. array_push($wordDict[$dict_lang[0]][$value->dict_id],$currData);
  118. }else{
  119. array_push($wordDict[$dict_lang[0]]["others"],$currData);
  120. }
  121. }else{
  122. array_push($wordDict['others']['others'],$currData);
  123. }
  124. }
  125. /**
  126. * 把树状数据变为扁平数据
  127. */
  128. foreach ($wordDict as $oneLang) {
  129. # code...
  130. foreach ($oneLang as $langId => $dictId) {
  131. # code...
  132. foreach ($dictId as $oneData) {
  133. # code...
  134. $wordData['dict'][] = $oneData;
  135. if(isset($dictList['children']) && count($dictList['children'])>0){
  136. $lastHref = end($dictList['children'])['href'];
  137. }else{
  138. $lastHref = '';
  139. }
  140. $currHref = '#'.$oneData['anchor'];
  141. if($lastHref !== $currHref){
  142. $dictList['children'][] = [
  143. 'href'=> $currHref,
  144. 'title'=> $oneData['shortname'],
  145. ];
  146. }
  147. }
  148. }
  149. }
  150. $wordDataOutput[]=$wordData;
  151. $dictListOutput[]=$dictList;
  152. //TODO 加变格查询
  153. $case = new CaseMan();
  154. $parent = $case->WordToBase($word);
  155. foreach ($parent as $base => $case) {
  156. # code...
  157. if(!in_array($base,$searched)){
  158. $word_base[$base] = $case;
  159. }
  160. }
  161. }
  162. if(count($word_base)===0){
  163. break;
  164. }else{
  165. $words = $word_base;
  166. }
  167. }
  168. if($resultCount<3){
  169. //查询内文
  170. $table = UserDict::select($indexCol)
  171. ->where('note','like','%'.$word.'%')
  172. ->where('language','<>','my')
  173. ->take(5)
  174. ->get();
  175. $resultCount += count($table);
  176. $wordData=[
  177. 'word'=> $word,
  178. 'factors'=> "",
  179. 'parents'=> "",
  180. 'case'=> [],
  181. 'grammar'=>[],
  182. 'anchor'=> $anchor,
  183. 'dict' => [],
  184. ];
  185. foreach ($table as $key => $value) {
  186. $dictInfo= DictInfo::find($value->dict_id);
  187. $dict_lang = explode('-',$dictInfo->dest_lang);
  188. $anchor = "{$word}-{$dictInfo->shortname}";
  189. $currData = [
  190. 'dictname'=> $dictInfo->name,
  191. 'shortname'=> $dictInfo->shortname,
  192. 'description'=>$dictInfo->description,
  193. 'dict_id' => $value->dict_id,
  194. 'lang' => $dict_lang[0],
  195. 'word'=> $word,
  196. 'note'=> $this->GrmAbbr($value->note,0),
  197. 'anchor'=> $anchor,
  198. ];
  199. $wordData['dict'][] = $currData;
  200. }
  201. $wordDataOutput[]=$wordData;
  202. }
  203. $output['words'] = $wordDataOutput;
  204. $output['dictlist'] = $dictListOutput;
  205. $output['caselist'] = $caseListOutput;
  206. $output['time'] = microtime(true) - $startAt;
  207. $output['count'] = $resultCount;
  208. return $this->ok($output);
  209. }
  210. /**
  211. * Store a newly created resource in storage.
  212. *
  213. * @param \Illuminate\Http\Request $request
  214. * @return \Illuminate\Http\Response
  215. */
  216. public function store(Request $request)
  217. {
  218. //
  219. }
  220. /**
  221. * Display the specified resource.
  222. *
  223. * @param \App\Models\UserDict $userDict
  224. * @return \Illuminate\Http\Response
  225. */
  226. public function show(UserDict $userDict)
  227. {
  228. //
  229. }
  230. /**
  231. * Update the specified resource in storage.
  232. *
  233. * @param \Illuminate\Http\Request $request
  234. * @param \App\Models\UserDict $userDict
  235. * @return \Illuminate\Http\Response
  236. */
  237. public function update(Request $request, UserDict $userDict)
  238. {
  239. //
  240. }
  241. /**
  242. * Remove the specified resource from storage.
  243. *
  244. * @param \App\Models\UserDict $userDict
  245. * @return \Illuminate\Http\Response
  246. */
  247. public function destroy(UserDict $userDict)
  248. {
  249. //
  250. }
  251. private function GrmAbbr($input,$dictid){
  252. $mean = $input;
  253. $replaced = array();
  254. foreach (GRM_ABBR as $key => $value) {
  255. if(in_array($value["abbr"],$replaced)){
  256. continue;
  257. }else{
  258. $replaced[] = $value["abbr"];
  259. }
  260. if($dictid !== 0){
  261. if($value["dictid"]=== $dictid && strpos($input,$value["abbr"]."|") == false){
  262. $mean = str_ireplace($value["abbr"],"|@{$value["abbr"]}-{$value["replace"]}",$mean);
  263. }
  264. }else{
  265. if( strpos($mean,"|@".$value["abbr"]) == false){
  266. $props=base64_encode(\json_encode(['text'=>$value["abbr"],'gid'=>$value["replace"]]));
  267. $tpl = "<MdTpl name='grammar-pop' tpl='grammar-pop' props='{$props}'></MdTpl>";
  268. $mean = str_ireplace($value["abbr"],$tpl,$mean);
  269. }
  270. }
  271. }
  272. return $mean;
  273. }
  274. }