WbwController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Wbw;
  4. use App\Models\WbwBlock;
  5. use App\Models\Channel;
  6. use App\Models\PaliSentence;
  7. use App\Models\Sentence;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Str;
  10. use App\Tools\Tools;
  11. use App\Http\Api\AuthApi;
  12. use App\Http\Api\ShareApi;
  13. use App\Http\Api\ChannelApi;
  14. class WbwController extends Controller
  15. {
  16. /**
  17. * Display a listing of the resource.
  18. *
  19. * @return \Illuminate\Http\Response
  20. */
  21. public function index()
  22. {
  23. //
  24. }
  25. /**
  26. * Store a newly created resource in storage.
  27. * 新建多个
  28. * 如果存在,修改
  29. * @param \Illuminate\Http\Request $request
  30. * @return \Illuminate\Http\Response
  31. */
  32. public function store(Request $request)
  33. {
  34. //
  35. //鉴权
  36. $user = AuthApi::current($request);
  37. if(!$user ){
  38. //未登录用户
  39. return $this->error(__('auth.failed'),[],401);
  40. }
  41. $channel = Channel::where('uid',$request->get('channel_id'))->first();
  42. if(!$channel){
  43. return $this->error(__('auth.failed'));
  44. }
  45. if($channel->owner_uid !== $user["user_uid"]){
  46. //判断是否为协作
  47. $power = ShareApi::getResPower($user["user_uid"],$channel->uid);
  48. if($power<30){
  49. return $this->error(__('auth.failed'));
  50. }
  51. }
  52. //查看WbwBlock是否已经建立
  53. $wbwBlockId = WbwBlock::where('book_id',$request->get('book'))
  54. ->where('paragraph',$request->get('para'))
  55. ->where('channel_uid',$request->get('channel_id'))
  56. ->value('uid');
  57. if(!Str::isUuid($wbwBlockId)){
  58. $wbwBlock = new WbwBlock();
  59. $wbwBlockId = Str::uuid();
  60. $wbwBlock->id = app('snowflake')->id();
  61. $wbwBlock->uid = $wbwBlockId;
  62. $wbwBlock->creator_uid = $user["user_uid"];
  63. $wbwBlock->editor_id = $user["user_id"];
  64. $wbwBlock->book_id = $request->get('book');
  65. $wbwBlock->paragraph = $request->get('para');
  66. $wbwBlock->channel_uid = $request->get('channel_id');
  67. $wbwBlock->lang = $channel->lang;
  68. $wbwBlock->status = $channel->status;
  69. $wbwBlock->create_time = time()*1000;
  70. $wbwBlock->modify_time = time()*1000;
  71. $wbwBlock->save();
  72. }
  73. $wbw = Wbw::where('block_uid',$wbwBlockId)
  74. ->where('wid',$request->get('sn'))
  75. ->first();
  76. if(!$wbw){
  77. //建立一个句子的逐词解析数据
  78. //找到句子
  79. $sent = PaliSentence::where('book',$request->get('book'))
  80. ->where('paragraph',$request->get('para'))
  81. ->where('word_begin',"<=",$request->get('sn'))
  82. ->where('word_end',">=",$request->get('sn'))
  83. ->first();
  84. $channelId = ChannelApi::getSysChannel('_System_Wbw_VRI_');
  85. $wbwContent = Sentence::where('book_id',$sent->book)
  86. ->where('paragraph',$sent->paragraph)
  87. ->where('word_start',$sent->word_begin)
  88. ->where('word_end',$sent->word_end)
  89. ->where('channel_uid',$channelId)
  90. ->value('content');
  91. $words = json_decode($wbwContent);
  92. foreach ($words as $word) {
  93. # code...
  94. $xmlObj = simplexml_load_string("<word></word>");
  95. $xmlObj->addChild('id',"{$sent->book}-{$sent->paragraph}-{$word->sn[0]}");
  96. $xmlObj->addChild('pali',$word->word->value)->addAttribute('status',0);
  97. $xmlObj->addChild('real',$word->real->value)->addAttribute('status',0);
  98. $xmlObj->addChild('type',$word->type->value)->addAttribute('status',0);
  99. $xmlObj->addChild('gramma',$word->grammar->value)->addAttribute('status',0);
  100. $xmlObj->addChild('case',implode('$', $word->case->value))->addAttribute('status',0);
  101. $xmlObj->addChild('style',$word->style->value)->addAttribute('status',0);
  102. $xmlObj->addChild('org',$word->factors->value)->addAttribute('status',0);
  103. $xmlObj->addChild('om',$word->factorMeaning->value)->addAttribute('status',0);
  104. $xmlObj->addChild('status',1);
  105. $xml = $xmlObj->asXml();
  106. $xml = str_replace('<?xml version="1.0"?>','',$xml);
  107. $newWbw = new Wbw();
  108. $newWbw->id = app('snowflake')->id();
  109. $newWbw->uid = Str::uuid();
  110. $newWbw->creator_uid = $channel->owner_uid;
  111. $newWbw->editor_id = $user["user_id"];
  112. $newWbw->book_id = $request->get('book');
  113. $newWbw->paragraph = $request->get('para');
  114. $newWbw->wid = $word->sn[0];
  115. $newWbw->block_uid = $wbwBlockId;
  116. $newWbw->data = $xml;
  117. $newWbw->word = $word->real->value;
  118. $newWbw->status = 0;
  119. $newWbw->create_time = time()*1000;
  120. $newWbw->modify_time = time()*1000;
  121. $newWbw->save();
  122. if($word->sn[0] === $request->get('sn')){
  123. $wbw = $newWbw;
  124. }
  125. }
  126. }
  127. $wbwData = "";
  128. foreach ($request->get('data') as $key => $word) {
  129. $xml = Tools::JsonToXml($word);
  130. $xml = str_replace('<?xml version="1.0"?>','',$xml);
  131. $wbwData .= $xml;
  132. }
  133. $wbw->data = $wbwData;
  134. $wbw->status = 5;
  135. $wbw->save();
  136. return $this->ok(1);
  137. }
  138. /**
  139. * Display the specified resource.
  140. *
  141. * @param \App\Models\Wbw $wbw
  142. * @return \Illuminate\Http\Response
  143. */
  144. public function show(Wbw $wbw)
  145. {
  146. //
  147. }
  148. /**
  149. * Update the specified resource in storage.
  150. *
  151. * @param \Illuminate\Http\Request $request
  152. * @param \App\Models\Wbw $wbw
  153. * @return \Illuminate\Http\Response
  154. */
  155. public function update(Request $request, Wbw $wbw)
  156. {
  157. //
  158. }
  159. /**
  160. * Remove the specified resource from storage.
  161. *
  162. * @param \App\Models\Wbw $wbw
  163. * @return \Illuminate\Http\Response
  164. */
  165. public function destroy(Wbw $wbw)
  166. {
  167. //
  168. }
  169. }