DictController.php 10.0 KB

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