SentenceController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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'])
  85. ->where('type',$type)
  86. ->get();
  87. //协作
  88. $channelShare = ShareApi::getResList($user['user_uid'],2);
  89. }
  90. $channelCanRead = [];
  91. foreach ($channelPub as $key => $value) {
  92. $channelCanRead[$value->uid] = [
  93. 'id' => $value->uid,
  94. 'role' => 'member',
  95. 'name' => $value->name,
  96. ];
  97. }
  98. foreach ($channelShare as $key => $value) {
  99. if($value['type'] === $type){
  100. $channelCanRead[$value['res_id']] = [
  101. 'id' => $value['res_id'],
  102. 'role' => 'member',
  103. 'name' => $value['res_title'],
  104. ];
  105. if($value['power']>=20){
  106. $channelCanRead[$value['res_id']]['role'] = "editor";
  107. }
  108. }
  109. }
  110. foreach ($channelMy as $key => $value) {
  111. $channelCanRead[$value->uid] = [
  112. 'id' => $value->uid,
  113. 'role' => 'owner',
  114. 'name' => $value->name,
  115. ];
  116. }
  117. $channels = [];
  118. foreach ($channelCanRead as $key => $value) {
  119. # code...
  120. $channels[] = $key;
  121. }
  122. $sent = explode('-',$request->get('sentence')) ;
  123. $table = Sentence::select($indexCol)
  124. ->whereIn('channel_uid', $channels)
  125. ->where('book_id',$sent[0])
  126. ->where('paragraph',$sent[1])
  127. ->where('word_start',$sent[2])
  128. ->where('word_end',$sent[3]);
  129. default:
  130. # code...
  131. break;
  132. }
  133. $count = $table->count();
  134. if($request->get('strlen',false)){
  135. $totalStrLen = $table->sum('strlen');
  136. }
  137. $table = $table->orderBy($request->get('order','updated_at'),$request->get('dir','desc'));
  138. $table = $table->skip($request->get("offset",0))
  139. ->take($request->get('limit',1000));
  140. $result = $table->get();
  141. if($result){
  142. if($request->get('view') === 'sent-can-read'){
  143. $output = ["rows"=>SentResource::collection($result),"count"=>$count];
  144. }else{
  145. $output = ["rows"=>$result,"count"=>$count];
  146. }
  147. if(isset($totalStrLen)){
  148. $output['total_strlen'] = $totalStrLen;
  149. }
  150. return $this->ok($output);
  151. }else{
  152. return $this->error("没有查询到数据");
  153. }
  154. }
  155. /**
  156. * 用channel 和句子编号列表查询句子
  157. */
  158. public function sent_in_channel(Request $request){
  159. $sent = $request->get('sentences') ;
  160. $query = [];
  161. foreach ($sent as $value) {
  162. # code...
  163. $ids = explode('-',$value);
  164. if(count($ids)===4){
  165. $query[] = $ids;
  166. }
  167. }
  168. $table = Sentence::select(['id','book_id','paragraph','word_start','word_end','content','channel_uid','updated_at'])
  169. ->where('channel_uid', $request->get('channel'))
  170. ->whereIns(['book_id','paragraph','word_start','word_end'],$query);
  171. $result = $table->get();
  172. if($result){
  173. return $this->ok(["rows"=>$result,"count"=>count($result)]);
  174. }else{
  175. return $this->error("没有查询到数据");
  176. }
  177. }
  178. /**
  179. * Show the form for creating a new resource.
  180. *
  181. * @return \Illuminate\Http\Response
  182. */
  183. public function create()
  184. {
  185. //
  186. }
  187. /**
  188. * 新建多个句子
  189. * 如果句子存在,修改
  190. * @param \Illuminate\Http\Request $request
  191. * @return \Illuminate\Http\Response
  192. */
  193. public function store(Request $request)
  194. {
  195. //鉴权
  196. $user = AuthApi::current($request);
  197. if(!$user ){
  198. //未登录用户
  199. return $this->error(__('auth.failed'),[],401);
  200. }
  201. $channel = Channel::where('uid',$request->get('channel'))->first();
  202. if(!$channel){
  203. return $this->error(__('auth.failed'));
  204. }
  205. if($channel->owner_uid !== $user["user_uid"]){
  206. //判断是否为协作
  207. $power = ShareApi::getResPower($user["user_uid"],$channel->uid,2);
  208. if($power < 20){
  209. return $this->error(__('auth.failed'),[],403);
  210. }
  211. }
  212. foreach ($request->get('sentences') as $key => $sent) {
  213. # code...
  214. $row = Sentence::firstOrNew([
  215. "book_id"=>$sent['book_id'],
  216. "paragraph"=>$sent['paragraph'],
  217. "word_start"=>$sent['word_start'],
  218. "word_end"=>$sent['word_end'],
  219. "channel_uid"=>$channel->uid,
  220. ],[
  221. "id"=>app('snowflake')->id(),
  222. "uid"=>Str::orderedUuid(),
  223. ]);
  224. $row->content = $sent['content'];
  225. $row->strlen = mb_strlen($sent['content'],"UTF-8");
  226. $row->language = $channel->lang;
  227. $row->status = $channel->status;
  228. $row->editor_uid = $user["user_uid"];
  229. $row->create_time = time()*1000;
  230. $row->modify_time = time()*1000;
  231. $row->save();
  232. //保存历史记录
  233. $this->saveHistory($row->uid,$user["user_uid"],$sent['content']);
  234. }
  235. return $this->ok(count($request->get('sentences')));
  236. }
  237. private function saveHistory($uid,$editor,$content){
  238. $newHis = new SentHistory();
  239. $newHis->id = app('snowflake')->id();
  240. $newHis->sent_uid = $uid;
  241. $newHis->user_uid = $editor;
  242. if(empty($content)){
  243. $newHis->content = "";
  244. }else{
  245. $newHis->content = $content;
  246. }
  247. $newHis->create_time = time()*1000;
  248. $newHis->save();
  249. }
  250. /**
  251. * Display the specified resource.
  252. *
  253. * @param \App\Models\Sentence $sentence
  254. * @return \Illuminate\Http\Response
  255. */
  256. public function show(Sentence $sentence)
  257. {
  258. //
  259. return $this->ok(new SentResource($sentence));
  260. }
  261. /**
  262. * 修改单个句子
  263. *
  264. * @param \Illuminate\Http\Request $request
  265. * @param string $id book_para_start_end_channel
  266. * @return \Illuminate\Http\Response
  267. */
  268. public function update(Request $request, $id)
  269. {
  270. //
  271. $param = \explode('_',$id);
  272. //鉴权
  273. $user = AuthApi::current($request);
  274. if(!$user){
  275. //未登录鉴权失败
  276. return $this->error(__('auth.failed'),[],403);
  277. }
  278. $channel = Channel::where('uid',$param[4])->first();
  279. if(!$channel){
  280. return $this->error("not found channel");
  281. }
  282. if($channel->owner_uid !== $user["user_uid"]){
  283. // 判断是否为协作
  284. $power = ShareApi::getResPower($user["user_uid"],$channel->uid,2);
  285. if($power < 20){
  286. return $this->error(__('auth.failed'),[],403);
  287. }
  288. }
  289. $sent = Sentence::firstOrNew([
  290. "book_id"=>$param[0],
  291. "paragraph"=>$param[1],
  292. "word_start"=>$param[2],
  293. "word_end"=>$param[3],
  294. "channel_uid"=>$param[4],
  295. ],[
  296. "id"=>app('snowflake')->id(),
  297. "uid"=>Str::orderedUuid(),
  298. "create_time"=>time()*1000,
  299. ]);
  300. $sent->content = $request->get('content');
  301. if($request->has('contentType')){
  302. $sent->content_type = $request->get('contentType');
  303. }
  304. $sent->language = $channel->lang;
  305. $sent->status = $channel->status;
  306. $sent->editor_uid = $user["user_uid"];
  307. $sent->strlen = mb_strlen($request->get('content'),"UTF-8");
  308. $sent->modify_time = time()*1000;
  309. if($request->has('prEditor')){
  310. $sent->acceptor_uid = $user["user_uid"];
  311. $sent->pr_edit_at = $request->get('prEditAt');
  312. $sent->editor_uid = $request->get('prEditor');
  313. $sent->pr_id = $request->get('prId');
  314. }
  315. $sent->save();
  316. //保存历史记录
  317. $this->saveHistory($sent->uid,$user["user_uid"],$request->get('content'));
  318. return $this->ok(new SentResource($sent));
  319. }
  320. /**
  321. * Remove the specified resource from storage.
  322. *
  323. * @param \App\Models\Sentence $sentence
  324. * @return \Illuminate\Http\Response
  325. */
  326. public function destroy(Sentence $sentence)
  327. {
  328. //
  329. }
  330. }