DictController.php 10 KB

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