TemplateRender.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. namespace App\Http\Api;
  3. use App\Models\DhammaTerm;
  4. use App\Models\PaliText;
  5. use App\Http\Controllers\CorpusController;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Facades\Log;
  8. use App\Http\Api\ChannelApi;
  9. class TemplateRender{
  10. protected $param = [];
  11. protected $mode = "read";
  12. protected $channel_id = [];
  13. /**
  14. * Create a new command instance.
  15. * int $mode 'read' | 'edit'
  16. * @return void
  17. */
  18. public function __construct($param, $channelInfo, $mode)
  19. {
  20. $this->param = $param;
  21. foreach ($channelInfo as $value) {
  22. $this->channel_id[] = $value->uid;
  23. }
  24. $this->channelInfo = $channelInfo;
  25. $this->mode = $mode;
  26. }
  27. public function render($tpl_name){
  28. switch ($tpl_name) {
  29. case 'term':
  30. # 术语
  31. $result = $this->render_term();
  32. break;
  33. case 'note':
  34. $result = $this->render_note();
  35. break;
  36. case 'sent':
  37. $result = $this->render_sent();
  38. break;
  39. case 'quote':
  40. $result = $this->render_quote();
  41. break;
  42. case 'exercise':
  43. $result = $this->render_exercise();
  44. break;
  45. case 'article':
  46. $result = $this->render_article();
  47. break;
  48. case 'nissaya':
  49. $result = $this->render_nissaya();
  50. break;
  51. case 'mermaid':
  52. $result = $this->render_mermaid();
  53. break;
  54. default:
  55. # code...
  56. $result = [
  57. 'props'=>base64_encode(\json_encode([])),
  58. 'html'=>'',
  59. 'tag'=>'span',
  60. 'tpl'=>'unknown',
  61. ];
  62. break;
  63. }
  64. return $result;
  65. }
  66. private function render_term(){
  67. $word = $this->get_param($this->param,"word",1);
  68. $channelId = $this->channel_id[0];
  69. $channelInfo = $this->channelInfo[0];
  70. $props = Cache::remember("/term/{$channelId}/{$word}",
  71. env('CACHE_EXPIRE',3600*24),
  72. function() use($word,$channelId,$channelInfo){
  73. //先查属于这个channel 的
  74. $tplParam = DhammaTerm::where("word",$word)->where('channal',$channelId)->first();
  75. if(!$tplParam){
  76. //没有,再查这个studio的
  77. $tplParam = DhammaTerm::where("word",$word)
  78. ->where('owner',$channelInfo->owner_uid)
  79. ->first();
  80. }
  81. if(!$tplParam){
  82. //没有,再查社区
  83. $community_channel = ChannelApi::getSysChannel("_community_term_zh-hans_");
  84. $tplParam = DhammaTerm::where("word",$word)
  85. ->where('channal',$community_channel)
  86. ->first();
  87. if($tplParam){
  88. $isCommunity = true;
  89. }
  90. }
  91. $output = [
  92. "word" => $word,
  93. "parentChannelId" => $channelId,
  94. "parentStudioId" => $channelInfo->owner_uid,
  95. ];
  96. $innerString = $output["word"];
  97. if($tplParam){
  98. $output["id"] = $tplParam->guid;
  99. $output["meaning"] = $tplParam->meaning;
  100. $output["channel"] = $tplParam->channal;
  101. if(isset($isCommunity)){
  102. $output["isCommunity"] = true;
  103. }
  104. $innerString = "{$output["meaning"]}({$output["word"]})";
  105. if(!empty($tplParam->other_meaning)){
  106. $output["meaning2"] = $tplParam->other_meaning;
  107. }
  108. /*
  109. if($tplParam->note){
  110. $output["summary"] = $tplParam->note;
  111. }else{
  112. //本人没有解释内容的。用社区数据。
  113. //TODO 由作者(读者)设置是否使用社区数据
  114. //获取channel 语言
  115. //使用社区note
  116. $community_channel = ChannelApi::getSysChannel("_community_term_zh-hans_");
  117. //查找社区解释
  118. $community_note = DhammaTerm::where("word",$word)
  119. ->where('channal',$community_channel)
  120. ->value('note');
  121. $output["summary"] = $tplParam->note;
  122. $output["community"] = true;
  123. }
  124. */
  125. }
  126. $output['innerHtml'] = $innerString;
  127. return $output;
  128. });
  129. return [
  130. 'props'=>base64_encode(\json_encode($props)),
  131. 'html'=>$props['innerHtml'],
  132. 'tag'=>'span',
  133. 'tpl'=>'term',
  134. ];
  135. }
  136. private function render_note(){
  137. $props = ["note" => $this->get_param($this->param,"text",1)];
  138. $trigger = $this->get_param($this->param,"trigger",2);
  139. $innerString = "";
  140. if(!empty($trigger)){
  141. $props["trigger"] = $trigger;
  142. $innerString = $props["trigger"];
  143. }
  144. return [
  145. 'props'=>base64_encode(\json_encode($props)),
  146. 'html'=>$innerString,
  147. 'tag'=>'span',
  148. 'tpl'=>'note',
  149. ];
  150. }
  151. private function render_nissaya(){
  152. $pali = $this->get_param($this->param,"pali",1);
  153. $meaning = $this->get_param($this->param,"meaning",2);
  154. $innerString = "";
  155. $props = [
  156. "pali" => $pali,
  157. "meaning" => $meaning,
  158. ];
  159. return [
  160. 'props'=>base64_encode(\json_encode($props)),
  161. 'html'=>$innerString,
  162. 'tag'=>'span',
  163. 'tpl'=>'nissaya',
  164. ];
  165. }
  166. private function render_exercise(){
  167. $id = $this->get_param($this->param,"id",1);
  168. $title = $this->get_param($this->param,"title",1);
  169. $props = [
  170. "id" => $id,
  171. "title" => $title,
  172. "channel" => $this->channel_id[0],
  173. ];
  174. return [
  175. 'props'=>base64_encode(\json_encode($props)),
  176. 'html'=>"",
  177. 'tag'=>'span',
  178. 'tpl'=>'exercise',
  179. ];
  180. }
  181. private function render_article(){
  182. $type = $this->get_param($this->param,"type",1);
  183. $id = $this->get_param($this->param,"id",2);
  184. $title = $this->get_param($this->param,"title",3);
  185. $channel = $this->get_param($this->param,"channel",4,$this->channel_id[0]);
  186. $style = $this->get_param($this->param,"style",5);
  187. $props = [
  188. "type" => $type,
  189. "id" => $id,
  190. "channel" => $channel,
  191. 'style' => $style,
  192. ];
  193. if(!empty($title)){
  194. $props['title'] = $title;
  195. }
  196. return [
  197. 'props'=>base64_encode(\json_encode($props)),
  198. 'html'=>"",
  199. 'tag'=>'span',
  200. 'tpl'=>'article',
  201. ];
  202. }
  203. private function render_quote(){
  204. $paraId = $this->get_param($this->param,"para",1);
  205. $channelId = $this->channel_id[0];
  206. $props = Cache::remember("/quote/{$channelId}/{$paraId}",
  207. env('CACHE_EXPIRE',3600*24),
  208. function() use($paraId,$channelId){
  209. $para = \explode('-',$paraId);
  210. $output = [
  211. "paraId" => $paraId,
  212. "channel" => $channelId,
  213. "innerString" => $paraId,
  214. ];
  215. if(count($para)<2){
  216. return $output;
  217. }
  218. $PaliText = PaliText::where("book",$para[0])
  219. ->where("paragraph",$para[1])
  220. ->select(['toc','path'])
  221. ->first();
  222. if($PaliText){
  223. $output["pali"] = $PaliText->toc;
  224. $output["paliPath"] = \json_decode($PaliText->path);
  225. $output["innerString"]= $PaliText->toc;
  226. }
  227. return $output;
  228. });
  229. return [
  230. 'props'=>base64_encode(\json_encode($props)),
  231. 'html'=>$props["innerString"],
  232. 'tag'=>'span',
  233. 'tpl'=>'quote',
  234. ];
  235. }
  236. private function render_sent(){
  237. $sid = $this->get_param($this->param,"sid",1);
  238. $channel = $this->get_param($this->param,"channel",2);
  239. if(!empty($channel)){
  240. $channels = explode(',',$channel);
  241. }else{
  242. $channels = $this->channel_id;
  243. }
  244. $sentInfo = explode('@',trim($sid));
  245. $sentId = $sentInfo[0];
  246. if(isset($sentInfo[1])){
  247. $channels = [$sentInfo[1]];
  248. }
  249. $Sent = new CorpusController();
  250. $props = $Sent->getSentTpl($sentId,$channels,$this->mode,true);
  251. if($props === false){
  252. $props['error']="句子模版渲染错误。句子参数个数不符。应该是四个。";
  253. }
  254. if($this->mode==='read'){
  255. $tpl = "sentread";
  256. }else{
  257. $tpl = "sentedit";
  258. }
  259. return [
  260. 'props'=>base64_encode(\json_encode($props)),
  261. 'html'=>"",
  262. 'tag'=>'span',
  263. 'tpl'=>$tpl,
  264. ];
  265. }
  266. private function render_mermaid(){
  267. $text = json_decode(base64_decode($this->get_param($this->param,"text",1)));
  268. $props = ["text" => implode("\n",$text)];
  269. return [
  270. 'props'=>base64_encode(\json_encode($props)),
  271. 'html'=>"mermaid",
  272. 'tag'=>'div',
  273. 'tpl'=>'mermaid',
  274. ];
  275. }
  276. private function get_param(array $param,string $name,int $id,string $default=''){
  277. if(isset($param[$name])){
  278. return trim($param[$name]);
  279. }else if(isset($param["{$id}"])){
  280. return trim($param["{$id}"]);
  281. }else{
  282. return $default;
  283. }
  284. }
  285. }