SentenceController.php 7.1 KB

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