SentPrController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Api\AuthApi;
  4. use App\Models\SentPr;
  5. use App\Models\Channel;
  6. use App\Models\PaliSentence;
  7. use App\Models\Sentence;
  8. use App\Http\Resources\SentPrResource;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Http;
  11. use Illuminate\Support\Facades\Log;
  12. use App\Http\Api\Mq;
  13. class SentPrController 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. //
  23. switch ($request->get('view')) {
  24. case 'sent-info':
  25. $table = SentPr::where('book_id',$request->get('book'))
  26. ->where('paragraph',$request->get('para'))
  27. ->where('word_start',$request->get('start'))
  28. ->where('word_end',$request->get('end'))
  29. ->where('channel_uid',$request->get('channel'));
  30. $all_count = $table->count();
  31. $chapters = $table->orderBy('created_at','desc')->get();
  32. break;
  33. }
  34. if($chapters){
  35. return $this->ok(["rows"=>SentPrResource::collection($chapters),"count"=>$all_count]);
  36. }else{
  37. return $this->error("no data");
  38. }
  39. }
  40. public function pr_tree(Request $request){
  41. $output = [];
  42. $sentences = $request->get("data");
  43. foreach ($sentences as $key => $sentence) {
  44. # 先查句子信息
  45. $sentInfo = Sentence::where('book_id',$sentence['book'])
  46. ->where('paragraph',$sentence['paragraph'])
  47. ->where('word_start',$sentence['word_start'])
  48. ->where('word_end',$sentence['word_end'])
  49. ->where('channel_uid',$sentence['channel_id'])
  50. ->first();
  51. $sentPr = SentPr::where('book_id',$sentence['book'])
  52. ->where('paragraph',$sentence['paragraph'])
  53. ->where('word_start',$sentence['word_start'])
  54. ->where('word_end',$sentence['word_end'])
  55. ->where('channel_uid',$sentence['channel_id'])
  56. ->select('content','editor_uid')
  57. ->orderBy('created_at','desc')->get();
  58. if(count($sentPr)>0){
  59. if($sentInfo){
  60. $content = $sentInfo->content;
  61. }else{
  62. $content = "null";
  63. }
  64. $output[] = [
  65. 'sentence' => [
  66. 'book' => $sentence['book'],
  67. 'paragraph' => $sentence['paragraph'],
  68. 'word_start' => $sentence['word_start'],
  69. 'word_end' => $sentence['word_end'],
  70. 'channel_id' => $sentence['channel_id'],
  71. 'content' => $content,
  72. 'pr_count' => count($sentPr),
  73. ],
  74. 'pr' => $sentPr,
  75. ];
  76. }
  77. }
  78. return $this->ok(['rows'=>$output,'count'=>count($output)]);
  79. }
  80. /**
  81. * Store a newly created resource in storage.
  82. *
  83. * @param \Illuminate\Http\Request $request
  84. * @return \Illuminate\Http\Response
  85. */
  86. public function store(Request $request)
  87. {
  88. //
  89. $user = AuthApi::current($request);
  90. if(!$user){
  91. return $this->error(__('auth.failed'),401,401);
  92. }
  93. $user_uid = $user['user_uid'];
  94. $data = $request->all();
  95. #查询是否存在
  96. #同样的内容只能提交一次
  97. $exists = SentPr::where('book_id',$data['book'])
  98. ->where('paragraph',$data['para'])
  99. ->where('word_start',$data['begin'])
  100. ->where('word_end',$data['end'])
  101. ->where('content',$data['text'])
  102. ->where('channel_uid',$data['channel'])
  103. ->exists();
  104. if($exists){
  105. return $this->error("已经存在同样的修改建议",200,200);
  106. }
  107. #不存在,新建
  108. $new = new SentPr();
  109. $new->id = app('snowflake')->id();
  110. $new->book_id = $data['book'];
  111. $new->paragraph = $data['para'];
  112. $new->word_start = $data['begin'];
  113. $new->word_end = $data['end'];
  114. $new->channel_uid = $data['channel'];
  115. $new->editor_uid = $user_uid;
  116. $new->content = $data['text'];
  117. $new->language = Channel::where('uid',$data['channel'])->value('lang');
  118. $new->status = 1;//未处理状态
  119. $new->strlen = mb_strlen($data['text'],"UTF-8");
  120. $new->create_time = time()*1000;
  121. $new->modify_time = time()*1000;
  122. $new->save();
  123. Mq::publish('suggestion',new SentPrResource($new));
  124. $robotMessageOk=false;
  125. $webHookMessage="";
  126. if(app()->isLocal()==false)
  127. {
  128. /*
  129. 初译:e5bc5c97-a6fb-4ccb-b7df-be6dcfee9c43
  130. 模版:#用户名 就“##该句子巴利前20字符##”提出了这样的修改建议:“##PR内容前20字##”,欢迎大家[点击链接](句子/段落链接)前往查看并讨论。
  131. 问题集:8622ad73-deef-4525-8e8e-ba3f1462724e
  132. 模版:#用户名 就 “##该句子巴利前20字符##”有这样的疑问:“##PR内容前20字##”,欢迎大家[点击链接](句子/段落链接)参与讨论。
  133. 初步答疑:5ab653d7-1ae3-40b0-ae07-c3d530a2a8f8
  134. 模版:#用户名 就“##该句子巴利前20字符##”中的问题做了这样的回复:“##PR内容前20字##”,欢迎大家[点击链接](句子/段落链接)前往查看并讨论。
  135. 机器人地址:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=25dbd74f-c89c-40e5-8cbc-48b1ef7710b8
  136. 项目范围:
  137. book65 par:829-1306
  138. book67 par:759-1152
  139. */
  140. //if(($data['book']==65 && $data['para']>=829 && $data['para']<=1306) || ($data['book']== 67 && $data['para'] >= 759 && $data['para'] <= 1152)){
  141. $username = 'nickname';
  142. $palitext = PaliSentence::where('book',$data['book'])
  143. ->where('paragraph',$data['para'])
  144. ->where('word_begin',$data['begin'])
  145. ->where('word_end',$data['end'])
  146. ->value('text');
  147. $sent_num = "{$data['book']}-{$data['para']}-{$data['begin']}-{$data['end']}";
  148. $palitext = mb_substr($palitext,0,20,"UTF-8");
  149. $prtext = mb_substr($data['text'],0,140,"UTF-8");
  150. $link = "https://www-hk.wikipali.org/app/article/index.php?view=para&book={$data['book']}&par={$data['para']}&begin={$data['begin']}&end={$data['end']}&channel={$data['channel']}&mode=edit";
  151. switch ($data['channel']) {
  152. //测试
  153. //case '3b0cb0aa-ea88-4ce5-b67d-00a3e76220cc':
  154. //正式
  155. case 'e5bc5c97-a6fb-4ccb-b7df-be6dcfee9c43':
  156. $strMessage = "{$username} 就文句`{$palitext}`提出了修改建议:
  157. >内容摘要:<font color=\"comment\">{$prtext}</font>,\n
  158. >句子编号:<font color=\"info\">{$sent_num}</font>\n
  159. 欢迎大家[点击链接]({$link}&channel=e5bc5c97-a6fb-4ccb-b7df-be6dcfee9c43,8622ad73-deef-4525-8e8e-ba3f1462724e,5ab653d7-1ae3-40b0-ae07-c3d530a2a8f8)查看并讨论。";
  160. break;
  161. case '8622ad73-deef-4525-8e8e-ba3f1462724e':
  162. $strMessage = "{$username} 就文句`{$palitext}`有疑问:\n
  163. >内容摘要:<font color=\"comment\">{$prtext}</font>,\n
  164. >句子编号:<font color=\"info\">{$sent_num}</font>\n
  165. 欢迎大家[点击链接]({$link}&channel=8622ad73-deef-4525-8e8e-ba3f1462724e,5ab653d7-1ae3-40b0-ae07-c3d530a2a8f8)查看并讨论。";
  166. break;
  167. case '5ab653d7-1ae3-40b0-ae07-c3d530a2a8f8':
  168. $strMessage = "{$username} 就文句`{$palitext}`中的疑问有这样的回复:\n
  169. >内容摘要:<font color=\"comment\">{$prtext}</font>,\n
  170. >句子编号:<font color=\"info\">{$sent_num}</font>\n
  171. 欢迎大家[点击链接]({$link}&channel=8622ad73-deef-4525-8e8e-ba3f1462724e,5ab653d7-1ae3-40b0-ae07-c3d530a2a8f8)查看并讨论。";
  172. break;
  173. default:
  174. $strMessage = "";
  175. break;
  176. }
  177. $url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=25dbd74f-c89c-40e5-8cbc-48b1ef7710b8";
  178. $param = [
  179. "msgtype"=>"markdown",
  180. "markdown"=> [
  181. "content"=> $strMessage,
  182. ],
  183. ];
  184. if(!empty($strMessage)){
  185. $response = Http::post($url, $param);
  186. if($response->successful()){
  187. $robotMessageOk = true;
  188. $webHookMessage = "消息发送成功";
  189. }else{
  190. $webHookMessage = "消息发送失败";
  191. $robotMessageOk = false;
  192. }
  193. }else{
  194. $webHookMessage = "channel不符";
  195. $robotMessageOk = false;
  196. }
  197. //}else{
  198. // $webHookMessage = "不在段落范围内";
  199. //}
  200. }
  201. #同时返回此句子pr数量
  202. $info['book_id'] = $data['book'];
  203. $info['paragraph'] = $data['para'];
  204. $info['word_start'] = $data['begin'];
  205. $info['word_end'] = $data['end'];
  206. $info['channel_uid'] = $data['channel'];
  207. $count = SentPr::where('book_id' , $data['book'])
  208. ->where('paragraph' , $data['para'])
  209. ->where('word_start' , $data['begin'])
  210. ->where('word_end' , $data['end'])
  211. ->where('channel_uid' , $data['channel'])
  212. ->count();
  213. return $this->ok(["new"=>$info,"count"=>$count,"webhook"=>["message"=>$webHookMessage,"ok"=>$robotMessageOk]]);
  214. }
  215. /**
  216. * Display the specified resource.
  217. *
  218. * @param \App\Models\SentPr $sentPr
  219. * @return \Illuminate\Http\Response
  220. */
  221. public function show(SentPr $sentPr)
  222. {
  223. //
  224. }
  225. /**
  226. * Update the specified resource in storage.
  227. *
  228. * @param \Illuminate\Http\Request $request
  229. * @param \App\Models\SentPr $sentPr
  230. * @return \Illuminate\Http\Response
  231. */
  232. public function update(Request $request, string $id)
  233. {
  234. $user = AuthApi::current($request);
  235. if(!$user){
  236. return $this->error(__('auth.failed'),401,401);
  237. }
  238. $sentPr = SentPr::find($id);
  239. if(!$sentPr){
  240. return $this->error('no res');
  241. }
  242. if($sentPr->editor_uid !== $user['user_uid']){
  243. return $this->error('not power',403,403);
  244. }
  245. $sentPr->content = $request->get('text');
  246. $sentPr->modify_time = time()*1000;
  247. $sentPr->save();
  248. return $this->ok($sentPr);
  249. }
  250. /**
  251. * Remove the specified resource from storage.
  252. *
  253. * @param string $id
  254. * @return \Illuminate\Http\Response
  255. */
  256. public function destroy(Request $request, string $id)
  257. {
  258. //
  259. $user = AuthApi::current($request);
  260. if(!$user){
  261. return $this->error(__('auth.failed'),401,401);
  262. }
  263. $old = SentPr::where('id', $id)->first();
  264. if(!$old){
  265. return $this->error('no res');
  266. }
  267. //鉴权
  268. if($old->editor_uid !== $user["user_uid"]){
  269. return $this->error(__('auth.failed'),403,403);
  270. }
  271. $result = SentPr::where('id', $id)
  272. ->where('editor_uid', $user["user_uid"])
  273. ->delete();
  274. if($result>0){
  275. #同时返回此句子pr数量
  276. $count = SentPr::where('book_id' , $old->book_id)
  277. ->where('paragraph' , $old->paragraph)
  278. ->where('word_start' , $old->word_start)
  279. ->where('word_end' , $old->word_end)
  280. ->where('channel_uid' , $old->channel_uid)
  281. ->count();
  282. return $this->ok($count);
  283. }else{
  284. return $this->error('not power',403,403);
  285. }
  286. }
  287. }