SentenceController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Sentence;
  4. use App\Models\Channel;
  5. use App\Models\SentHistory;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Str;
  8. use App\Http\Resources\SentResource;
  9. use App\Http\Api\AuthApi;
  10. use App\Http\Api\ShareApi;
  11. use App\Http\Api\ChannelApi;
  12. use App\Http\Api\PaliTextApi;
  13. use Illuminate\Support\Facades\Log;
  14. use Illuminate\Support\Facades\Cache;
  15. use App\Http\Api\Mq;
  16. class SentenceController extends Controller
  17. {
  18. /**
  19. * Display a listing of the resource.
  20. *
  21. * @return \Illuminate\Http\Response
  22. */
  23. public function index(Request $request)
  24. {
  25. $result=false;
  26. $indexCol = ['id','uid','book_id','paragraph',
  27. 'word_start','word_end','content','content_type',
  28. 'channel_uid','editor_uid','acceptor_uid','pr_edit_at','updated_at'];
  29. switch ($request->get('view')) {
  30. case 'public':
  31. //获取全部公开的译文
  32. //首先获取某个类型的 channel 列表
  33. $channels = [];
  34. $channel_type = $request->get('channel_type','translation');
  35. if($channel_type === "original"){
  36. $pali_channel = ChannelApi::getSysChannel("_System_Pali_VRI_");
  37. if($pali_channel !== false){
  38. $channels[] = $pali_channel;
  39. }
  40. }else{
  41. $channelList = Channel::where('type',$channel_type)
  42. ->where('status',30)
  43. ->select('uid')->get();
  44. foreach ($channelList as $channel) {
  45. # code...
  46. $channels[] = $channel->uid;
  47. }
  48. }
  49. $table = Sentence::select($indexCol)
  50. ->whereIn('channel_uid',$channels)
  51. ->where('updated_at','>',$request->get('updated_after','1970-1-1'));
  52. break;
  53. case 'fulltext':
  54. if(isset($_COOKIE['user_uid'])){
  55. $userUid = $_COOKIE['user_uid'];
  56. }
  57. $key = $request->get('key');
  58. if(empty($key)){
  59. return $this->error("没有关键词");
  60. }
  61. $table = Sentence::select($indexCol)
  62. ->where('content','like', '%'.$key.'%')
  63. ->where('editor_uid',$userUid);
  64. break;
  65. case 'channel':
  66. //句子编号列表在某个channel下的全部内容
  67. $sent = explode(',',$request->get('sentence')) ;
  68. $query = [];
  69. foreach ($sent as $value) {
  70. # code...
  71. $ids = explode('-',$value);
  72. $query[] = $ids;
  73. }
  74. $table = Sentence::select($indexCol)
  75. ->where('channel_uid', $request->get('channel'))
  76. ->whereIns(['book_id','paragraph','word_start','word_end'],$query);
  77. break;
  78. case 'sent-can-read':
  79. /**
  80. * 某句的全部译文
  81. */
  82. //获取用户有阅读权限的所有channel
  83. //全网公开
  84. $type = $request->get('type','translation');
  85. $channelTable = Channel::where("type",$type)->select(['uid','name']);
  86. $channelPub = $channelTable->where('status',30)->get();
  87. $user = AuthApi::current($request);
  88. $channelShare=array();
  89. $channelMy=array();
  90. if($user){
  91. //自己的
  92. $channelMy = Channel::where('owner_uid',$user['user_uid'])
  93. ->where('type',$type)
  94. ->get();
  95. //协作
  96. $channelShare = ShareApi::getResList($user['user_uid'],2);
  97. }
  98. $channelCanRead = [];
  99. foreach ($channelPub as $key => $value) {
  100. $channelCanRead[$value->uid] = [
  101. 'id' => $value->uid,
  102. 'role' => 'member',
  103. 'name' => $value->name,
  104. ];
  105. }
  106. foreach ($channelShare as $key => $value) {
  107. if($value['type'] === $type){
  108. $channelCanRead[$value['res_id']] = [
  109. 'id' => $value['res_id'],
  110. 'role' => 'member',
  111. 'name' => $value['res_title'],
  112. ];
  113. if($value['power']>=20){
  114. $channelCanRead[$value['res_id']]['role'] = "editor";
  115. }
  116. }
  117. }
  118. foreach ($channelMy as $key => $value) {
  119. $channelCanRead[$value->uid] = [
  120. 'id' => $value->uid,
  121. 'role' => 'owner',
  122. 'name' => $value->name,
  123. ];
  124. }
  125. $channels = [];
  126. $excludeChannels = explode(',',$request->get('channels')) ;
  127. foreach ($channelCanRead as $key => $value) {
  128. # code...
  129. if(!in_array($key,$excludeChannels)){
  130. $channels[] = $key;
  131. }
  132. }
  133. $sent = explode('-',$request->get('sentence')) ;
  134. $table = Sentence::select($indexCol)
  135. ->whereIn('channel_uid', $channels)
  136. ->where('book_id',$sent[0])
  137. ->where('paragraph',$sent[1])
  138. ->where('word_start',$sent[2])
  139. ->where('word_end',$sent[3]);
  140. break;
  141. case 'chapter':
  142. $chapter = PaliTextApi::getChapterStartEnd($request->get('book'),$request->get('para'));
  143. $table = Sentence::where('book_id',$request->get('book'))
  144. ->whereBetween('paragraph',$chapter)
  145. ->whereIn('channel_uid',explode(',',$request->get('channels')));
  146. break;
  147. default:
  148. # code...
  149. break;
  150. }
  151. $count = $table->count();
  152. if($request->get('strlen',false)){
  153. $totalStrLen = $table->sum('strlen');
  154. }
  155. $table = $table->orderBy($request->get('order','updated_at'),$request->get('dir','desc'));
  156. $table = $table->skip($request->get("offset",0))
  157. ->take($request->get('limit',1000));
  158. $result = $table->get();
  159. if($result){
  160. $output = ["count"=>$count];
  161. if($request->get('view') === 'sent-can-read' ||
  162. $request->get('view') === 'chapter'){
  163. $output["rows"] = SentResource::collection($result);
  164. }else{
  165. $output["rows"] = $result;
  166. }
  167. if(isset($totalStrLen)){
  168. $output['total_strlen'] = $totalStrLen;
  169. }
  170. return $this->ok($output);
  171. }else{
  172. return $this->error("没有查询到数据");
  173. }
  174. }
  175. /**
  176. * 用channel 和句子编号列表查询句子
  177. */
  178. public function sent_in_channel(Request $request){
  179. $sent = $request->get('sentences') ;
  180. $query = [];
  181. foreach ($sent as $value) {
  182. # code...
  183. $ids = explode('-',$value);
  184. if(count($ids)===4){
  185. $query[] = $ids;
  186. }
  187. }
  188. $table = Sentence::select(['id','book_id','paragraph','word_start','word_end','content','channel_uid','updated_at'])
  189. ->where('channel_uid', $request->get('channel'))
  190. ->whereIns(['book_id','paragraph','word_start','word_end'],$query);
  191. $result = $table->get();
  192. if($result){
  193. return $this->ok(["rows"=>$result,"count"=>count($result)]);
  194. }else{
  195. return $this->error("没有查询到数据");
  196. }
  197. }
  198. /**
  199. * Show the form for creating a new resource.
  200. *
  201. * @return \Illuminate\Http\Response
  202. */
  203. public function create()
  204. {
  205. //
  206. }
  207. /**
  208. * 新建多个句子
  209. * 如果句子存在,修改
  210. * @param \Illuminate\Http\Request $request
  211. * @return \Illuminate\Http\Response
  212. */
  213. public function store(Request $request)
  214. {
  215. //鉴权
  216. $user = AuthApi::current($request);
  217. if(!$user ){
  218. //未登录用户
  219. return $this->error(__('auth.failed'),[],401);
  220. }
  221. $channel = Channel::where('uid',$request->get('channel'))->first();
  222. if(!$channel){
  223. return $this->error(__('auth.failed'));
  224. }
  225. if($channel->owner_uid !== $user["user_uid"]){
  226. //判断是否为协作
  227. $power = ShareApi::getResPower($user["user_uid"],$channel->uid,2);
  228. if($power < 20){
  229. return $this->error(__('auth.failed'),[],403);
  230. }
  231. }
  232. $sentFirst=null;
  233. foreach ($request->get('sentences') as $key => $sent) {
  234. # code...
  235. if($sentFirst === null){
  236. $sentFirst = $sent;
  237. }
  238. $row = Sentence::firstOrNew([
  239. "book_id"=>$sent['book_id'],
  240. "paragraph"=>$sent['paragraph'],
  241. "word_start"=>$sent['word_start'],
  242. "word_end"=>$sent['word_end'],
  243. "channel_uid"=>$channel->uid,
  244. ],[
  245. "id"=>app('snowflake')->id(),
  246. "uid"=>Str::uuid(),
  247. ]);
  248. $row->content = $sent['content'];
  249. $row->strlen = mb_strlen($sent['content'],"UTF-8");
  250. $row->language = $channel->lang;
  251. $row->status = $channel->status;
  252. if($request->has('copy')){
  253. //复制句子,保留原作者信息
  254. $row->editor_uid = $sent["editor_uid"];
  255. $row->acceptor_uid = $user["user_uid"];
  256. $row->pr_edit_at = $sent["updated_at"];
  257. }else{
  258. $row->editor_uid = $user["user_uid"];
  259. $row->acceptor_uid = null;
  260. $row->pr_edit_at = null;
  261. }
  262. $row->create_time = time()*1000;
  263. $row->modify_time = time()*1000;
  264. $row->save();
  265. //保存历史记录
  266. if($request->has('copy')){
  267. $this->saveHistory($row->uid,$sent["editor_uid"],$sent['content']);
  268. }else{
  269. $this->saveHistory($row->uid,$user["user_uid"],$sent['content']);
  270. }
  271. }
  272. if($sentFirst !== null){
  273. Mq::publish('progress',['book'=>$sentFirst['book_id'],
  274. 'para'=>$sentFirst['paragraph'],
  275. 'channel'=>$channel->uid,
  276. ]);
  277. }
  278. return $this->ok(count($request->get('sentences')));
  279. }
  280. private function saveHistory($uid,$editor,$content){
  281. $newHis = new SentHistory();
  282. $newHis->id = app('snowflake')->id();
  283. $newHis->sent_uid = $uid;
  284. $newHis->user_uid = $editor;
  285. if(empty($content)){
  286. $newHis->content = "";
  287. }else{
  288. $newHis->content = $content;
  289. }
  290. $newHis->create_time = time()*1000;
  291. $newHis->save();
  292. }
  293. /**
  294. * Display the specified resource.
  295. *
  296. * @param \App\Models\Sentence $sentence
  297. * @return \Illuminate\Http\Response
  298. */
  299. public function show(Sentence $sentence)
  300. {
  301. //
  302. return $this->ok(new SentResource($sentence));
  303. }
  304. /**
  305. * 修改单个句子
  306. *
  307. * @param \Illuminate\Http\Request $request
  308. * @param string $id book_para_start_end_channel
  309. * @return \Illuminate\Http\Response
  310. */
  311. public function update(Request $request, $id)
  312. {
  313. //
  314. $param = \explode('_',$id);
  315. //鉴权
  316. $user = AuthApi::current($request);
  317. if(!$user){
  318. //未登录鉴权失败
  319. return $this->error(__('auth.failed'),[],403);
  320. }
  321. $channel = Channel::where('uid',$param[4])->first();
  322. if(!$channel){
  323. return $this->error("not found channel");
  324. }
  325. if($channel->owner_uid !== $user["user_uid"]){
  326. // 判断是否为协作
  327. $power = ShareApi::getResPower($user["user_uid"],$channel->uid,2);
  328. if($power < 20){
  329. return $this->error(__('auth.failed'),[],403);
  330. }
  331. }
  332. $sent = Sentence::firstOrNew([
  333. "book_id"=>$param[0],
  334. "paragraph"=>$param[1],
  335. "word_start"=>$param[2],
  336. "word_end"=>$param[3],
  337. "channel_uid"=>$param[4],
  338. ],[
  339. "id"=>app('snowflake')->id(),
  340. "uid"=>Str::orderedUuid(),
  341. "create_time"=>time()*1000,
  342. ]);
  343. $sent->content = $request->get('content');
  344. if($request->has('contentType')){
  345. $sent->content_type = $request->get('contentType');
  346. }
  347. $sent->language = $channel->lang;
  348. $sent->status = $channel->status;
  349. $sent->strlen = mb_strlen($request->get('content'),"UTF-8");
  350. $sent->modify_time = time()*1000;
  351. if($request->has('prEditor')){
  352. $sent->editor_uid = $request->get('prEditor');
  353. $sent->acceptor_uid = $user["user_uid"];
  354. $sent->pr_edit_at = $request->get('prEditAt');
  355. $sent->pr_id = $request->get('prId');
  356. }else{
  357. $sent->editor_uid = $user["user_uid"];
  358. $sent->acceptor_uid = null;
  359. $sent->pr_edit_at = null;
  360. $sent->pr_id = null;
  361. }
  362. $sent->save();
  363. //清除cache
  364. $channelId = $param[4];
  365. $currSentId = "{$param[0]}-{$param[1]}-{$param[2]}-{$param[3]}";
  366. Cache::forget("/sent/{$channelId}/{$currSentId}");
  367. //保存历史记录
  368. $this->saveHistory($sent->uid,$user["user_uid"],$request->get('content'));
  369. Mq::publish('progress',['book'=>$param[0],
  370. 'para'=>$param[1],
  371. 'channel'=>$channelId,
  372. ]);
  373. Mq::publish('content',new SentResource($sent));
  374. return $this->ok(new SentResource($sent));
  375. }
  376. /**
  377. * Remove the specified resource from storage.
  378. *
  379. * @param \App\Models\Sentence $sentence
  380. * @return \Illuminate\Http\Response
  381. */
  382. public function destroy(Sentence $sentence)
  383. {
  384. //
  385. }
  386. }