SentenceController.php 15 KB

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