SentenceController.php 13 KB

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