SentenceController.php 12 KB

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