SentPrController.php 8.8 KB

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