DictController.php 9.9 KB

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