TemplateRender.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. "parentChannelId" => $channelId,
  78. "parentStudioId" => $channelInfo->owner_uid,
  79. ];
  80. $innerString = $output["word"];
  81. if($tplParam){
  82. $output["id"] = $tplParam->guid;
  83. $output["meaning"] = $tplParam->meaning;
  84. $output["channel"] = $tplParam->channal;
  85. $innerString = "{$output["meaning"]}({$output["word"]})";
  86. if(!empty($tplParam->other_meaning)){
  87. $output["meaning2"] = $tplParam->other_meaning;
  88. }
  89. if($tplParam->note){
  90. $output["summary"] = $tplParam->note;
  91. }else{
  92. //使用社区note
  93. //获取channel 语言
  94. //查找社区解释
  95. }
  96. }
  97. $output['innerHtml'] = $innerString;
  98. return $output;
  99. });
  100. return [
  101. 'props'=>base64_encode(\json_encode($props)),
  102. 'html'=>$props['innerHtml'],
  103. 'tag'=>'span',
  104. 'tpl'=>'term',
  105. ];
  106. }
  107. private function render_note(){
  108. $props = ["note" => $this->get_param($this->param,"text",1)];
  109. $trigger = $this->get_param($this->param,"trigger",2);
  110. $innerString = "";
  111. if(!empty($trigger)){
  112. $props["trigger"] = $trigger;
  113. $innerString = $props["trigger"];
  114. }
  115. return [
  116. 'props'=>base64_encode(\json_encode($props)),
  117. 'html'=>$innerString,
  118. 'tag'=>'span',
  119. 'tpl'=>'note',
  120. ];
  121. }
  122. private function render_nissaya(){
  123. $pali = $this->get_param($this->param,"pali",1);
  124. $meaning = $this->get_param($this->param,"meaning",2);
  125. $innerString = "";
  126. $props = [
  127. "pali" => $pali,
  128. "meaning" => $meaning,
  129. ];
  130. return [
  131. 'props'=>base64_encode(\json_encode($props)),
  132. 'html'=>$innerString,
  133. 'tag'=>'span',
  134. 'tpl'=>'nissaya',
  135. ];
  136. }
  137. private function render_exercise(){
  138. $id = $this->get_param($this->param,"id",1);
  139. $title = $this->get_param($this->param,"title",1);
  140. $props = [
  141. "id" => $id,
  142. "title" => $title,
  143. "channel" => $this->channel_id,
  144. ];
  145. return [
  146. 'props'=>base64_encode(\json_encode($props)),
  147. 'html'=>"",
  148. 'tag'=>'span',
  149. 'tpl'=>'exercise',
  150. ];
  151. }
  152. private function render_article(){
  153. $type = $this->get_param($this->param,"type",1);
  154. $id = $this->get_param($this->param,"id",2);
  155. $title = $this->get_param($this->param,"title",3);
  156. $channel = $this->get_param($this->param,"channel",4);
  157. $props = [
  158. "type" => $type,
  159. "id" => $id,
  160. "channel" => $channel,
  161. ];
  162. if(empty($channel)){
  163. $props['channel'] = $this->channel_id;
  164. }
  165. if(!empty($title)){
  166. $props['title'] = $title;
  167. }
  168. return [
  169. 'props'=>base64_encode(\json_encode($props)),
  170. 'html'=>"",
  171. 'tag'=>'span',
  172. 'tpl'=>'article',
  173. ];
  174. }
  175. private function render_quote(){
  176. $paraId = $this->get_param($this->param,"para",1);
  177. $channelId = $this->channel_id;
  178. $props = Cache::remember("/quote/{$channelId}/{$paraId}",
  179. 60,
  180. function() use($paraId,$channelId){
  181. $para = \explode('-',$paraId);
  182. $output = [
  183. "paraId" => $paraId,
  184. "channel" => $channelId,
  185. "innerString" => $paraId,
  186. ];
  187. if(count($para)<2){
  188. return $output;
  189. }
  190. $PaliText = PaliText::where("book",$para[0])
  191. ->where("paragraph",$para[1])
  192. ->select(['toc','path'])
  193. ->first();
  194. if($PaliText){
  195. $output["pali"] = $PaliText->toc;
  196. $output["paliPath"] = \json_decode($PaliText->path);
  197. $output["innerString"]= $PaliText->toc;
  198. }
  199. return $output;
  200. });
  201. return [
  202. 'props'=>base64_encode(\json_encode($props)),
  203. 'html'=>$props["innerString"],
  204. 'tag'=>'span',
  205. 'tpl'=>'quote',
  206. ];
  207. }
  208. private function render_sent(){
  209. $sid = $this->get_param($this->param,"sid",1);
  210. $channel = $this->get_param($this->param,"channel",2);
  211. if(!empty($channel)){
  212. $mChannel = $channel;
  213. }else{
  214. $mChannel = $this->channel_id;
  215. }
  216. $sentInfo = explode('@',trim($sid));
  217. $sentId = $sentInfo[0];
  218. if(empty($mChannel)){
  219. $channels = [];
  220. }else{
  221. $channels = [$mChannel];
  222. }
  223. if(isset($sentInfo[1])){
  224. $channels = [$sentInfo[1]];
  225. }
  226. $Sent = new CorpusController();
  227. $props = $Sent->getSentTpl($sentId,$channels,$this->mode,true);
  228. if($props === false){
  229. $props['error']="句子模版渲染错误。句子参数个数不符。应该是四个。";
  230. }
  231. if($this->mode==='read'){
  232. $tpl = "sentread";
  233. }else{
  234. $tpl = "sentedit";
  235. }
  236. return [
  237. 'props'=>base64_encode(\json_encode($props)),
  238. 'html'=>"",
  239. 'tag'=>'span',
  240. 'tpl'=>$tpl,
  241. ];
  242. }
  243. private function get_param(array $param,string $name,int $id,string $default=''){
  244. if(isset($param[$name])){
  245. return trim($param[$name]);
  246. }else if(isset($param["{$id}"])){
  247. return trim($param["{$id}"]);
  248. }else{
  249. return $default;
  250. }
  251. }
  252. }