SentenceController.php 13 KB

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