DictController.php 8.4 KB

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