SentenceController.php 10.0 KB

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