SentPrController.php 9.1 KB

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