2
0

SentenceController.php 13 KB

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