SentPrController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\SentPr;
  4. use App\Models\Channel;
  5. use App\Models\PaliSentence;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Http;
  8. use Illuminate\Support\Facades\Log;
  9. class SentPrController extends Controller
  10. {
  11. /**
  12. * Display a listing of the resource.
  13. *
  14. * @return \Illuminate\Http\Response
  15. */
  16. public function index()
  17. {
  18. //
  19. }
  20. /**
  21. * Store a newly created resource in storage.
  22. *
  23. * @param \Illuminate\Http\Request $request
  24. * @return \Illuminate\Http\Response
  25. */
  26. public function store(Request $request)
  27. {
  28. //
  29. if(!isset($_COOKIE['user_uid'])){
  30. return $this->error('not login');
  31. }else{
  32. $user_uid = $_COOKIE['user_uid'];
  33. }
  34. $data = $request->all();
  35. #查询是否存在
  36. #同样的内容只能提交一次
  37. $exists = SentPr::where('book_id',$data['book'])
  38. ->where('paragraph',$data['para'])
  39. ->where('word_start',$data['begin'])
  40. ->where('word_end',$data['end'])
  41. ->where('content',$data['text'])
  42. ->where('channel_uid',$data['channel'])
  43. ->exists();
  44. if(!$exists){
  45. #不存在,新建
  46. $new = new SentPr();
  47. $new->id = app('snowflake')->id();
  48. $new->book_id = $data['book'];
  49. $new->paragraph = $data['para'];
  50. $new->word_start = $data['begin'];
  51. $new->word_end = $data['end'];
  52. $new->channel_uid = $data['channel'];
  53. $new->editor_uid = $user_uid;
  54. $new->content = $data['text'];
  55. $new->language = Channel::where('uid',$data['channel'])->value('lang');
  56. $new->status = 1;//未处理状态
  57. $new->strlen = mb_strlen($data['text'],"UTF-8");
  58. $new->create_time = time()*1000;
  59. $new->modify_time = time()*1000;
  60. $new->save();
  61. }
  62. $robotMessageOk=false;
  63. $webHookMessage="";
  64. if(app()->isLocal()==false)
  65. {
  66. /*
  67. 初译:e5bc5c97-a6fb-4ccb-b7df-be6dcfee9c43
  68. 模版:#用户名 就“##该句子巴利前20字符##”提出了这样的修改建议:“##PR内容前20字##”,欢迎大家[点击链接](句子/段落链接)前往查看并讨论。
  69. 问题集:8622ad73-deef-4525-8e8e-ba3f1462724e
  70. 模版:#用户名 就 “##该句子巴利前20字符##”有这样的疑问:“##PR内容前20字##”,欢迎大家[点击链接](句子/段落链接)参与讨论。
  71. 初步答疑:5ab653d7-1ae3-40b0-ae07-c3d530a2a8f8
  72. 模版:#用户名 就“##该句子巴利前20字符##”中的问题做了这样的回复:“##PR内容前20字##”,欢迎大家[点击链接](句子/段落链接)前往查看并讨论。
  73. 机器人地址:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=25dbd74f-c89c-40e5-8cbc-48b1ef7710b8
  74. 项目范围:
  75. book65 par:829-1306
  76. book67 par:759-1152
  77. */
  78. $username = '';
  79. $palitext = PaliSentence::where('book',$data['book'])
  80. ->where('paragraph',$data['para'])
  81. ->where('word_begin',$data['begin'])
  82. ->where('word_end',$data['end'])
  83. ->value('text');
  84. $palitext = mb_substr($palitext,0,20,"UTF-8");
  85. $prtext = mb_substr($data['text'],0,20,"UTF-8");
  86. $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";
  87. Log::info("palitext:{$palitext} prtext = {$prtext} link={$link}");
  88. if(($data['book']==65 && $data['para']>=829 && $data['para']<=1306) || ($data['book']== 67 && $data['para'] >= 759 && $data['para'] <= 1152)){
  89. switch ($data['channel']) {
  90. case 'e5bc5c97-a6fb-4ccb-b7df-be6dcfee9c43':
  91. $strMessage = "wikipali: {$username} 就“{$palitext}”提出了这样的修改建议:“{$prtext}”,欢迎大家[点击链接]({$link})前往查看并讨论。";
  92. break;
  93. case '8622ad73-deef-4525-8e8e-ba3f1462724e':
  94. $strMessage = "wikipali: {$username} 就“{$palitext}”有这样的疑问:“{$prtext}”,欢迎大家[点击链接]({$link})前往查看并讨论。";
  95. break;
  96. case '5ab653d7-1ae3-40b0-ae07-c3d530a2a8f8':
  97. $strMessage = "wikipali: {$username} 就“{$palitext}”中的问题做了这样的回复:“{$prtext}”,欢迎大家[点击链接]({$link})前往查看并讨论。";
  98. break;
  99. default:
  100. $strMessage = "";
  101. break;
  102. }
  103. $url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=25dbd74f-c89c-40e5-8cbc-48b1ef7710b8";
  104. /*
  105. $param = [
  106. "actionCard"=> [
  107. "title"=> "修改建议",
  108. "text"=> " wikipali: 来自{$_COOKIE['user_uid']}的修改建议:{$data['text']}",
  109. "btnOrientation"=> "0",
  110. "singleTitle" => "详情",
  111. "singleURL"=>"https://staging.wikipali.org/app/article/index.php?view=para&book={$data['book']}&par={$data['para']}&channal={$data['channel']}&display=sent&mode=edit"
  112. ],
  113. "msgtype"=>"actionCard"
  114. ];
  115. */
  116. $param = [
  117. "msgtype"=>"markdown",
  118. "markdown"=> [
  119. "content"=> $strMessage,
  120. ],
  121. ];
  122. Log::info("message:{$strMessage}");
  123. if(!empty($strMessage)){
  124. $response = Http::post($url, $param);
  125. $webHookMessage = $response->body;
  126. if($response->successful()){
  127. $robotMessageOk = true;
  128. }else{
  129. $webHookMessage = "消息发送失败";
  130. $robotMessageOk = false;
  131. }
  132. }else{
  133. $webHookMessage = "channel不符";
  134. $robotMessageOk = false;
  135. }
  136. }else{
  137. $webHookMessage = "不在段落范围内";
  138. }
  139. }
  140. #同时返回此句子pr数量
  141. $info['book_id'] = $data['book'];
  142. $info['paragraph'] = $data['para'];
  143. $info['word_start'] = $data['begin'];
  144. $info['word_end'] = $data['end'];
  145. $info['channel_uid'] = $data['channel'];
  146. $count = SentPr::where('book_id' , $data['book'])
  147. ->where('paragraph' , $data['para'])
  148. ->where('word_start' , $data['begin'])
  149. ->where('word_end' , $data['end'])
  150. ->where('channel_uid' , $data['channel'])
  151. ->count();
  152. Log::info("count:{$count} webhook-ok={$robotMessageOk}");
  153. return $this->ok(["new"=>$info,"count"=>$count,"webhook"=>["message"=>$webHookMessage,"ok"=>$robotMessageOk]]);
  154. }
  155. /**
  156. * Display the specified resource.
  157. *
  158. * @param \App\Models\SentPr $sentPr
  159. * @return \Illuminate\Http\Response
  160. */
  161. public function show(SentPr $sentPr)
  162. {
  163. //
  164. }
  165. /**
  166. * Update the specified resource in storage.
  167. *
  168. * @param \Illuminate\Http\Request $request
  169. * @param \App\Models\SentPr $sentPr
  170. * @return \Illuminate\Http\Response
  171. */
  172. public function update(Request $request, SentPr $sentPr)
  173. {
  174. //
  175. if(!isset($_COOKIE['user_uid'])){
  176. return $this->error('not login');
  177. }else{
  178. $user_uid = $_COOKIE['user_uid'];
  179. }
  180. $sentPr = SentPr::where('id',$request->get('id'));
  181. if($sentPr->value('editor_uid')==$user_uid){
  182. $update = $sentPr->update([
  183. "content"=>$request->get('text'),
  184. "modify_time"=>time()*1000,
  185. ]);
  186. if($update >= 0){
  187. $data = SentPr::where('id',$request->get('id'))->first();
  188. $data->id = sprintf("%d",$data->id);
  189. return $this->ok($data);
  190. }else{
  191. return $this->error('没有更新');
  192. }
  193. }else{
  194. return $this->error('not power');
  195. }
  196. }
  197. /**
  198. * Remove the specified resource from storage.
  199. *
  200. * @param int $id
  201. * @return \Illuminate\Http\Response
  202. */
  203. public function destroy($id)
  204. {
  205. //
  206. Log::info("user_uid=" .$_COOKIE['user_uid']);
  207. $old = SentPr::where('id', $id)->first();
  208. $result = SentPr::where('id', $id)
  209. ->where('editor_uid', $_COOKIE["user_uid"])
  210. ->delete();
  211. Log::info("delete=" .$result);
  212. if($result>0){
  213. #同时返回此句子pr数量
  214. $count = SentPr::where('book_id' , $old->book_id)
  215. ->where('paragraph' , $old->paragraph)
  216. ->where('word_start' , $old->word_start)
  217. ->where('word_end' , $old->word_end)
  218. ->where('channel_uid' , $old->channel_uid)
  219. ->count();
  220. return $this->ok($count);
  221. }else{
  222. return $this->error('not power');
  223. }
  224. }
  225. }