SentPrController.php 11 KB

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