SentenceController.php 12 KB

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