DictController.php 11 KB

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