2
0

SentPrController.php 11 KB

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