SentenceController.php 15 KB

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