2
0

SentPrController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. require_once __DIR__.'/../../../public/app/ucenter/function.php';
  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'));
  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. #不存在,新建
  107. $new = new SentPr();
  108. $new->id = app('snowflake')->id();
  109. $new->book_id = $data['book'];
  110. $new->paragraph = $data['para'];
  111. $new->word_start = $data['begin'];
  112. $new->word_end = $data['end'];
  113. $new->channel_uid = $data['channel'];
  114. $new->editor_uid = $user_uid;
  115. $new->content = $data['text'];
  116. $new->language = Channel::where('uid',$data['channel'])->value('lang');
  117. $new->status = 1;//未处理状态
  118. $new->strlen = mb_strlen($data['text'],"UTF-8");
  119. $new->create_time = time()*1000;
  120. $new->modify_time = time()*1000;
  121. $new->save();
  122. Mq::publish('suggestion',new SentPrResource($new));
  123. }
  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. $userinfo = new \UserInfo();
  142. $username = $userinfo->getName($user_uid)['nickname'];
  143. $palitext = PaliSentence::where('book',$data['book'])
  144. ->where('paragraph',$data['para'])
  145. ->where('word_begin',$data['begin'])
  146. ->where('word_end',$data['end'])
  147. ->value('text');
  148. $sent_num = "{$data['book']}-{$data['para']}-{$data['begin']}-{$data['end']}";
  149. $palitext = mb_substr($palitext,0,20,"UTF-8");
  150. $prtext = mb_substr($data['text'],0,140,"UTF-8");
  151. $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";
  152. switch ($data['channel']) {
  153. //测试
  154. //case '3b0cb0aa-ea88-4ce5-b67d-00a3e76220cc':
  155. //正式
  156. case 'e5bc5c97-a6fb-4ccb-b7df-be6dcfee9c43':
  157. $strMessage = "{$username} 就文句`{$palitext}`提出了修改建议:
  158. >内容摘要:<font color=\"comment\">{$prtext}</font>,\n
  159. >句子编号:<font color=\"info\">{$sent_num}</font>\n
  160. 欢迎大家[点击链接]({$link}&channel=e5bc5c97-a6fb-4ccb-b7df-be6dcfee9c43,8622ad73-deef-4525-8e8e-ba3f1462724e,5ab653d7-1ae3-40b0-ae07-c3d530a2a8f8)查看并讨论。";
  161. break;
  162. case '8622ad73-deef-4525-8e8e-ba3f1462724e':
  163. $strMessage = "{$username} 就文句`{$palitext}`有疑问:\n
  164. >内容摘要:<font color=\"comment\">{$prtext}</font>,\n
  165. >句子编号:<font color=\"info\">{$sent_num}</font>\n
  166. 欢迎大家[点击链接]({$link}&channel=8622ad73-deef-4525-8e8e-ba3f1462724e,5ab653d7-1ae3-40b0-ae07-c3d530a2a8f8)查看并讨论。";
  167. break;
  168. case '5ab653d7-1ae3-40b0-ae07-c3d530a2a8f8':
  169. $strMessage = "{$username} 就文句`{$palitext}`中的疑问有这样的回复:\n
  170. >内容摘要:<font color=\"comment\">{$prtext}</font>,\n
  171. >句子编号:<font color=\"info\">{$sent_num}</font>\n
  172. 欢迎大家[点击链接]({$link}&channel=8622ad73-deef-4525-8e8e-ba3f1462724e,5ab653d7-1ae3-40b0-ae07-c3d530a2a8f8)查看并讨论。";
  173. break;
  174. default:
  175. $strMessage = "";
  176. break;
  177. }
  178. $url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=25dbd74f-c89c-40e5-8cbc-48b1ef7710b8";
  179. $param = [
  180. "msgtype"=>"markdown",
  181. "markdown"=> [
  182. "content"=> $strMessage,
  183. ],
  184. ];
  185. if(!empty($strMessage)){
  186. $response = Http::post($url, $param);
  187. if($response->successful()){
  188. $robotMessageOk = true;
  189. $webHookMessage = "消息发送成功";
  190. }else{
  191. $webHookMessage = "消息发送失败";
  192. $robotMessageOk = false;
  193. }
  194. }else{
  195. $webHookMessage = "channel不符";
  196. $robotMessageOk = false;
  197. }
  198. //}else{
  199. // $webHookMessage = "不在段落范围内";
  200. //}
  201. }
  202. #同时返回此句子pr数量
  203. $info['book_id'] = $data['book'];
  204. $info['paragraph'] = $data['para'];
  205. $info['word_start'] = $data['begin'];
  206. $info['word_end'] = $data['end'];
  207. $info['channel_uid'] = $data['channel'];
  208. $count = SentPr::where('book_id' , $data['book'])
  209. ->where('paragraph' , $data['para'])
  210. ->where('word_start' , $data['begin'])
  211. ->where('word_end' , $data['end'])
  212. ->where('channel_uid' , $data['channel'])
  213. ->count();
  214. return $this->ok(["new"=>$info,"count"=>$count,"webhook"=>["message"=>$webHookMessage,"ok"=>$robotMessageOk]]);
  215. }
  216. /**
  217. * Display the specified resource.
  218. *
  219. * @param \App\Models\SentPr $sentPr
  220. * @return \Illuminate\Http\Response
  221. */
  222. public function show(SentPr $sentPr)
  223. {
  224. //
  225. }
  226. /**
  227. * Update the specified resource in storage.
  228. *
  229. * @param \Illuminate\Http\Request $request
  230. * @param \App\Models\SentPr $sentPr
  231. * @return \Illuminate\Http\Response
  232. */
  233. public function update(Request $request, SentPr $sentPr)
  234. {
  235. $user = AuthApi::current($request);
  236. if(!$user){
  237. return $this->error(__('auth.failed'));
  238. }
  239. $user_uid = $user['user_uid'];
  240. $sentPr = SentPr::where('id',$request->get('id'));
  241. if($sentPr->value('editor_uid')==$user_uid){
  242. $update = $sentPr->update([
  243. "content"=>$request->get('text'),
  244. "modify_time"=>time()*1000,
  245. ]);
  246. if($update >= 0){
  247. $data = SentPr::where('id',$request->get('id'))->first();
  248. $data->id = sprintf("%d",$data->id);
  249. return $this->ok($data);
  250. }else{
  251. return $this->error('没有更新');
  252. }
  253. }else{
  254. return $this->error('not power');
  255. }
  256. }
  257. /**
  258. * Remove the specified resource from storage.
  259. *
  260. * @param int $id
  261. * @return \Illuminate\Http\Response
  262. */
  263. public function destroy($id)
  264. {
  265. //
  266. $user = AuthApi::current($request);
  267. if(!$user){
  268. return $this->error(__('auth.failed'));
  269. }
  270. $old = SentPr::where('id', $id)->first();
  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');
  285. }
  286. }
  287. }