DictController.php 8.6 KB

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