SentenceController.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. $query[] = $ids;
  139. }
  140. $table = Sentence::select(['id','book_id','paragraph','word_start','word_end','content','channel_uid','updated_at'])
  141. ->where('channel_uid', $request->get('channel'))
  142. ->whereIns(['book_id','paragraph','word_start','word_end'],$query);
  143. $result = $table->get();
  144. if($result){
  145. return $this->ok(["rows"=>$result,"count"=>count($result)]);
  146. }else{
  147. return $this->error("没有查询到数据");
  148. }
  149. }
  150. /**
  151. * Show the form for creating a new resource.
  152. *
  153. * @return \Illuminate\Http\Response
  154. */
  155. public function create()
  156. {
  157. //
  158. }
  159. /**
  160. * 新建多个句子
  161. * 如果句子存在,修改
  162. * @param \Illuminate\Http\Request $request
  163. * @return \Illuminate\Http\Response
  164. */
  165. public function store(Request $request)
  166. {
  167. //鉴权
  168. $user = AuthApi::current($request);
  169. if(!$user ){
  170. //未登录用户
  171. return $this->error(__('auth.failed'));
  172. }
  173. $channel = Channel::where('uid',$request->get('channel'))->first();
  174. if(!$channel){
  175. return $this->error(__('auth.failed'));
  176. }
  177. if($channel->owner_uid !== $user["user_uid"]){
  178. //判断是否为协作
  179. $power = ShareApi::getResPower($user["user_uid"],$channel->uid);
  180. if($power<30){
  181. return $this->error(__('auth.failed'));
  182. }
  183. }
  184. foreach ($request->get('sentences') as $key => $sent) {
  185. # code...
  186. $row = Sentence::firstOrNew([
  187. "book_id"=>$sent['book_id'],
  188. "paragraph"=>$sent['paragraph'],
  189. "word_start"=>$sent['word_start'],
  190. "word_end"=>$sent['word_end'],
  191. "channel_uid"=>$channel->uid,
  192. ],[
  193. "id"=>app('snowflake')->id(),
  194. "uid"=>Str::orderedUuid(),
  195. ]);
  196. $row->content = $sent['content'];
  197. $row->strlen = mb_strlen($sent['content'],"UTF-8");
  198. $row->language = $channel->lang;
  199. $row->status = $channel->status;
  200. $row->editor_uid = $user["user_uid"];
  201. $row->create_time = time()*1000;
  202. $row->modify_time = time()*1000;
  203. $row->save();
  204. }
  205. return $this->ok(count($request->get('sentences')));
  206. }
  207. /**
  208. * Display the specified resource.
  209. *
  210. * @param \App\Models\Sentence $sentence
  211. * @return \Illuminate\Http\Response
  212. */
  213. public function show(Sentence $sentence)
  214. {
  215. //
  216. }
  217. /**
  218. * 修改单个句子
  219. *
  220. * @param \Illuminate\Http\Request $request
  221. * @param string $id book_para_start_end_channel
  222. * @return \Illuminate\Http\Response
  223. */
  224. public function update(Request $request, $id)
  225. {
  226. //
  227. $param = \explode('_',$id);
  228. //鉴权
  229. $user = AuthApi::current($request);
  230. if(!$user){
  231. //未登录鉴权失败
  232. return $this->error(__('auth.failed'));
  233. }
  234. $channel = Channel::where('uid',$param[4])->first();
  235. if(!$channel){
  236. return $this->error("not found channel");
  237. }
  238. if($channel->owner_uid !== $user["user_uid"]){
  239. //TODO 判断是否为协作
  240. return $this->error(__('auth.failed'));
  241. }
  242. $sent = Sentence::firstOrNew([
  243. "book_id"=>$param[0],
  244. "paragraph"=>$param[1],
  245. "word_start"=>$param[2],
  246. "word_end"=>$param[3],
  247. "channel_uid"=>$param[4],
  248. ],[
  249. "id"=>app('snowflake')->id(),
  250. "uid"=>Str::orderedUuid(),
  251. "create_time"=>time()*1000,
  252. ]);
  253. $sent->content = $request->get('content');
  254. $sent->language = $channel->lang;
  255. $sent->status = $channel->status;
  256. $sent->editor_uid = $user["user_uid"];
  257. $sent->strlen = mb_strlen($request->get('content'),"UTF-8");
  258. $sent->modify_time = time()*1000;
  259. if($request->has('prEditor')){
  260. $sent->acceptor_uid = $user["user_uid"];
  261. $sent->pr_edit_at = $request->get('prEditAt');
  262. $sent->editor_uid = $request->get('prEditor');
  263. $sent->pr_id = $request->get('prId');
  264. }
  265. $sent->save();
  266. return $this->ok(new SentResource($sent));
  267. }
  268. /**
  269. * Remove the specified resource from storage.
  270. *
  271. * @param \App\Models\Sentence $sentence
  272. * @return \Illuminate\Http\Response
  273. */
  274. public function destroy(Sentence $sentence)
  275. {
  276. //
  277. }
  278. }