2
0

SentenceController.php 11 KB

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