SentenceController.php 15 KB

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