SentPrController.php 6.8 KB

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