DictController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. 'meta' => json_decode($dictInfo->meta),
  112. 'dict_id' => $value->dict_id,
  113. 'lang' => $dict_lang[0],
  114. 'word' => $word,
  115. 'note' => $this->GrmAbbr($value->note, 0),
  116. 'anchor' => $anchor,
  117. ];
  118. if (isset($wordDict[$dict_lang[0]])) {
  119. if (isset($wordDict[$dict_lang[0]][$value->dict_id])) {
  120. array_push($wordDict[$dict_lang[0]][$value->dict_id], $currData);
  121. } else {
  122. array_push($wordDict[$dict_lang[0]]["others"], $currData);
  123. }
  124. } else {
  125. array_push($wordDict['others']['others'], $currData);
  126. }
  127. }
  128. /**
  129. * 把树状数据变为扁平数据
  130. */
  131. foreach ($wordDict as $oneLang) {
  132. # code...
  133. foreach ($oneLang as $langId => $dictId) {
  134. # code...
  135. foreach ($dictId as $oneData) {
  136. # code...
  137. $wordData['dict'][] = $oneData;
  138. if (isset($dictList['children']) && count($dictList['children']) > 0) {
  139. $lastHref = end($dictList['children'])['href'];
  140. } else {
  141. $lastHref = '';
  142. }
  143. $currHref = '#' . $oneData['anchor'];
  144. if ($lastHref !== $currHref) {
  145. $dictList['children'][] = [
  146. 'href' => $currHref,
  147. 'title' => $oneData['shortname'],
  148. ];
  149. }
  150. }
  151. }
  152. }
  153. $wordDataOutput[] = $wordData;
  154. $dictListOutput[] = $dictList;
  155. //TODO 加变格查询
  156. $case = new CaseMan();
  157. $parent = $case->WordToBase($word);
  158. foreach ($parent as $base => $case) {
  159. # code...
  160. if (!in_array($base, $searched)) {
  161. $word_base[$base] = $case;
  162. }
  163. }
  164. }
  165. $wordDataPass[] = ['pass' => $i + 1, 'words' => $wordDataOutput];
  166. if (count($word_base) === 0) {
  167. break;
  168. } else {
  169. $words = $word_base;
  170. }
  171. }
  172. if ($resultCount < 2) {
  173. //查询内文
  174. $wordDataOutput = [];
  175. $table = UserDict::select($indexCol)
  176. ->where('note', 'like', '%' . $word . '%')
  177. ->where('language', '<>', 'my')
  178. ->take(5)
  179. ->get();
  180. $resultCount += count($table);
  181. $wordData = [
  182. 'word' => $word,
  183. 'factors' => "",
  184. 'parents' => "",
  185. 'case' => [],
  186. 'grammar' => [],
  187. 'anchor' => $anchor,
  188. 'dict' => [],
  189. ];
  190. foreach ($table as $key => $value) {
  191. $dictInfo = DictInfo::find($value->dict_id);
  192. $dict_lang = explode('-', $dictInfo->dest_lang);
  193. $anchor = "{$word}-{$dictInfo->shortname}";
  194. $currData = [
  195. 'dictname' => $dictInfo->name,
  196. 'shortname' => $dictInfo->shortname,
  197. 'description' => $dictInfo->description,
  198. 'dict_id' => $value->dict_id,
  199. 'lang' => $dict_lang[0],
  200. 'word' => $word,
  201. 'note' => $this->GrmAbbr($value->note, 0),
  202. 'anchor' => $anchor,
  203. ];
  204. $wordData['dict'][] = $currData;
  205. }
  206. $wordDataOutput[] = $wordData;
  207. $wordDataPass[] = ['pass' => 0, 'words' => $wordDataOutput];
  208. }
  209. $output['words'] = $wordDataPass;
  210. $output['dictlist'] = $dictListOutput;
  211. $output['caselist'] = $caseListOutput;
  212. $output['time'] = microtime(true) - $startAt;
  213. $output['count'] = $resultCount;
  214. return $this->ok($output);
  215. }
  216. /**
  217. * Store a newly created resource in storage.
  218. *
  219. * @param \Illuminate\Http\Request $request
  220. * @return \Illuminate\Http\Response
  221. */
  222. public function store(Request $request)
  223. {
  224. //
  225. }
  226. /**
  227. * Display the specified resource.
  228. *
  229. * @param \App\Models\UserDict $userDict
  230. * @return \Illuminate\Http\Response
  231. */
  232. public function show(UserDict $userDict)
  233. {
  234. //
  235. }
  236. /**
  237. * Update the specified resource in storage.
  238. *
  239. * @param \Illuminate\Http\Request $request
  240. * @param \App\Models\UserDict $userDict
  241. * @return \Illuminate\Http\Response
  242. */
  243. public function update(Request $request, UserDict $userDict)
  244. {
  245. //
  246. }
  247. /**
  248. * Remove the specified resource from storage.
  249. *
  250. * @param \App\Models\UserDict $userDict
  251. * @return \Illuminate\Http\Response
  252. */
  253. public function destroy(UserDict $userDict)
  254. {
  255. //
  256. }
  257. private function GrmAbbr($input, $dictid)
  258. {
  259. $mean = $input;
  260. $replaced = array();
  261. foreach (GRM_ABBR as $key => $value) {
  262. if (in_array($value["abbr"], $replaced)) {
  263. continue;
  264. } else {
  265. $replaced[] = $value["abbr"];
  266. }
  267. if ($dictid !== 0) {
  268. if ($value["dictid"] === $dictid && strpos($input, $value["abbr"] . "|") == false) {
  269. $mean = str_ireplace($value["abbr"], "|@{$value["abbr"]}-{$value["replace"]}", $mean);
  270. }
  271. } else {
  272. if (strpos($mean, "|@" . $value["abbr"]) == false) {
  273. $props = base64_encode(\json_encode(['text' => $value["abbr"], 'gid' => $value["replace"]]));
  274. $tpl = "<MdTpl name='grammar-pop' tpl='grammar-pop' props='{$props}'></MdTpl>";
  275. $mean = str_ireplace($value["abbr"], $tpl, $mean);
  276. }
  277. }
  278. }
  279. return $mean;
  280. }
  281. }