TemplateRender.php 8.5 KB

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