IndexController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. namespace App\Home\Controllers;
  3. use Storage;
  4. use App\Http\Controllers\Controller;
  5. use App\Model\Question;
  6. use App\Model\Option;
  7. use App\Jobs\Robot;
  8. use GuzzleHttp\Client;
  9. use GuzzleHttp\Cookie\CookieJar;
  10. use Illuminate\Http\Request;
  11. class IndexController extends Controller
  12. {
  13. public function importTimu1()
  14. {
  15. set_time_limit(0);
  16. $txt = Storage::get('1.txt');
  17. $txt_arr = explode("\n", $txt);
  18. foreach ($txt_arr as $k => $val) {
  19. $q_a = explode('?', $val);
  20. $str1 = preg_replace('/^\d/i','',$q_a[0], 1);
  21. $str2 = preg_replace('/^\d/i','',$str1, 1);
  22. $title = preg_replace('/^\d/i','',$str2, 1);
  23. $question_id = Question::insertGetId([
  24. "title" => $title . "?",
  25. "is_released" => 1,
  26. "created_at" => date('Y-m-d H:i:s'),
  27. "updated_at" => date('Y-m-d H:i:s'),
  28. ]);
  29. $optiion = mb_substr($q_a[1], 0, -4);
  30. //答案
  31. $answer = mb_substr($q_a[1], -4);
  32. $answer = mb_substr($answer, 1,1);
  33. // echo $optiion;
  34. // echo mb_strpos($optiion, 'B');
  35. $o_answer = 0;
  36. if(mb_strpos($optiion, 'A')){
  37. $optiion_A = mb_substr($optiion, mb_strpos($optiion, 'A') + 1, mb_strpos($optiion, 'B') - mb_strpos($optiion, 'A') - 1);
  38. }else{
  39. $optiion_A = mb_substr($optiion, 0 ,mb_strpos($optiion, 'B') - 1);
  40. }
  41. if($answer == 'A'){
  42. $o_answer = 1;
  43. }
  44. Option::insert([
  45. "title" => $optiion_A,
  46. "is_answer" => $o_answer,
  47. "question_id" => $question_id,
  48. "created_at" => date('Y-m-d H:i:s'),
  49. "updated_at" => date('Y-m-d H:i:s'),
  50. ]);
  51. $o_answer = 0;
  52. if(mb_strpos($optiion, 'C')){
  53. $option_B = mb_substr($optiion, mb_strpos($optiion, 'B') + 1, mb_strpos($optiion, 'C') - mb_strpos($optiion, 'B') - 1);
  54. if($answer == 'B'){
  55. $o_answer = 1;
  56. }
  57. Option::insert([
  58. "title" => $option_B,
  59. "is_answer" => $o_answer,
  60. "question_id" => $question_id,
  61. "created_at" => date('Y-m-d H:i:s'),
  62. "updated_at" => date('Y-m-d H:i:s'),
  63. ]);
  64. $o_answer = 0;
  65. }elseif(mb_strpos($optiion, 'B')){
  66. echo $option_B = mb_substr($optiion, mb_strpos($optiion, 'B') + 1);
  67. if($answer == 'B'){
  68. $o_answer = 1;
  69. }
  70. Option::insert([
  71. "title" => $option_B,
  72. "is_answer" => $o_answer,
  73. "question_id" => $question_id,
  74. "created_at" => date('Y-m-d H:i:s'),
  75. "updated_at" => date('Y-m-d H:i:s'),
  76. ]);
  77. $o_answer = 0;
  78. }
  79. if(mb_strpos($optiion, 'D')){
  80. $option_C = mb_substr($optiion, mb_strpos($optiion, 'C') + 1, mb_strpos($optiion, 'D') - mb_strpos($optiion, 'C') - 1);
  81. if($answer == 'C'){
  82. $o_answer = 1;
  83. }
  84. Option::insert([
  85. "title" => $option_C,
  86. "is_answer" => $o_answer,
  87. "question_id" => $question_id,
  88. "created_at" => date('Y-m-d H:i:s'),
  89. "updated_at" => date('Y-m-d H:i:s'),
  90. ]);
  91. $o_answer = 0;
  92. }elseif(mb_strpos($optiion, 'C')){
  93. echo $option_C = mb_substr($optiion, mb_strpos($optiion, 'C') + 1);
  94. if($answer == 'C'){
  95. $o_answer = 1;
  96. }
  97. Option::insert([
  98. "title" => $option_C,
  99. "is_answer" => $o_answer,
  100. "question_id" => $question_id,
  101. "created_at" => date('Y-m-d H:i:s'),
  102. "updated_at" => date('Y-m-d H:i:s'),
  103. ]);
  104. $o_answer = 0;
  105. }
  106. if(mb_strpos($optiion, 'E')){
  107. $option_D = mb_substr($optiion, mb_strpos($optiion, 'D') + 1, mb_strpos($optiion, 'E') - mb_strpos($optiion, 'D') - 1);
  108. if($answer == 'B'){
  109. $o_answer = 1;
  110. }
  111. Option::insert([
  112. "title" => $option_D,
  113. "is_answer" => $o_answer,
  114. "question_id" => $question_id,
  115. "created_at" => date('Y-m-d H:i:s'),
  116. "updated_at" => date('Y-m-d H:i:s'),
  117. ]);
  118. $o_answer = 0;
  119. }elseif(mb_strpos($optiion, 'D')){
  120. $option_D = mb_substr($optiion, mb_strpos($optiion, 'D') + 1);
  121. if($answer == 'D'){
  122. $o_answer = 1;
  123. }
  124. Option::insert([
  125. "title" => $option_D,
  126. "is_answer" => $o_answer,
  127. "question_id" => $question_id,
  128. "created_at" => date('Y-m-d H:i:s'),
  129. "updated_at" => date('Y-m-d H:i:s'),
  130. ]);
  131. $o_answer = 0;
  132. }
  133. }
  134. }
  135. public function importTimu()
  136. {
  137. set_time_limit(0);
  138. $text = Storage::get('timu.json');
  139. $questions = json_decode($text);
  140. dump($questions);
  141. $answer_ex = [
  142. "A" => 1,
  143. "B" => 2,
  144. "C" => 3,
  145. "D" => 4,
  146. "E" => 5,
  147. "F" => 6,
  148. "G" => 7,
  149. ];
  150. foreach ($questions as $k => $ques) {
  151. $question_id = Question::insertGetId([
  152. "title" => $ques->title,
  153. "is_released" => 1,
  154. "created_at" => date('Y-m-d H:i:s'),
  155. "updated_at" => date('Y-m-d H:i:s'),
  156. ]);
  157. //判断答案
  158. foreach ($ques->options as $sk => $op) {
  159. if($answer_ex[$ques->answer] - 1 == $sk){
  160. $is_answer = 1;
  161. }else{
  162. $is_answer = 0;
  163. }
  164. Option::insert([
  165. "title" => $op,
  166. "is_answer" => $is_answer,
  167. "question_id" => $question_id,
  168. "created_at" => date('Y-m-d H:i:s'),
  169. "updated_at" => date('Y-m-d H:i:s'),
  170. ]);
  171. }
  172. echo "{$ques->title} 已入库";
  173. }
  174. }
  175. /**
  176. * 测试加入队列
  177. */
  178. public function putJob(Request $request)
  179. {
  180. $cmcc_id = $request->input('user_id');
  181. $mt = $request->input('mt');
  182. //请求用户信息
  183. $client = new Client([
  184. 'verify' => false,
  185. ]);
  186. $ip = '211.139.191.175';
  187. $port = '38083';
  188. $req_time = date('YmdHis');
  189. $sign = md5($cmcc_id . $req_time . $mt);
  190. $params['req_param'] = [
  191. "pub_info" => [
  192. "req_src" => "1",
  193. "ver" => "1.0",
  194. "mt" => $mt,
  195. "user_id" => $cmcc_id,
  196. "req_time" => $req_time,
  197. "sign" => $sign,
  198. ],
  199. "page_info" => [
  200. // "current_page" => 1,
  201. // "page_size" => 10,
  202. ],
  203. "busi_info" => [
  204. "pk_result" => [
  205. [
  206. "robot_flag" => 0,
  207. "user_id" => $cmcc_id,
  208. "score" => 230,
  209. ],
  210. [
  211. "robot_flag" => 1,
  212. "user_id" => "",
  213. "score" => 230,
  214. ],
  215. ]
  216. ],
  217. ];
  218. $result = $client->request('POST', "http://{$ip}:{$port}/hgs/uc/pktaskresult",[
  219. 'json' => $params
  220. ]);
  221. $cmcc_user = json_decode($result->getBody()->getContents());
  222. dump($cmcc_user);exit;
  223. if($cmcc_user->rsp_param->pub_info->code != "0000"){
  224. $response = [];
  225. $response['code'] = 400;
  226. $response['msg'] = '请求接口返回信息错误';
  227. return response()->json($response);
  228. }
  229. dump($cmcc_user);
  230. // dump(Robot::dispatch()->onQueue('robot'));
  231. }
  232. /**
  233. * 测试机器人
  234. */
  235. // public function robot()
  236. // {
  237. // $client = new Client([
  238. // 'base_uri' => 'http://183.234.61.252:8090',
  239. // 'timeout' => 2.0,
  240. // 'cookies' => true,
  241. // 'verify' => false,
  242. // ]);
  243. // $worker = new Worker('websocket://183.234.61.252:8282');
  244. // $worker->onMessage = function($ws_connection, $message)
  245. // {
  246. // //$client->request('POST');
  247. // // 与远程task服务建立异步连接,ip为远程task服务的ip,如果是本机就是127.0.0.1,如果是集群就是lvs的ip
  248. // $task_connection = new AsyncTcpConnection('Text://127.0.0.1:12345');
  249. // // 任务及参数数据
  250. // $task_data = array(
  251. // 'function' => 'send_mail',
  252. // 'args' => array('from'=>'xxx', 'to'=>'xxx', 'contents'=>'xxx'),
  253. // );
  254. // // 发送数据
  255. // $task_connection->send(json_encode($task_data));
  256. // // 异步获得结果
  257. // $task_connection->onMessage = function($task_connection, $task_result)use($ws_connection)
  258. // {
  259. // // 结果
  260. // var_dump($task_result);
  261. // // 获得结果后记得关闭异步连接
  262. // $task_connection->close();
  263. // // 通知对应的websocket客户端任务完成
  264. // $ws_connection->send('task complete');
  265. // };
  266. // // 执行异步连接
  267. // $task_connection->connect();
  268. // }
  269. // Worker::runAll();
  270. // }
  271. }