SentenceController.php 7.4 KB

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