Index.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\AdminLog;
  4. use app\common\controller\Backend;
  5. use think\Config;
  6. use think\Hook;
  7. use think\Db;
  8. use think\Session;
  9. use think\Validate;
  10. /**
  11. * 后台首页
  12. * @internal
  13. */
  14. class Index extends Backend
  15. {
  16. protected $noNeedLogin = ['login','timeout'];
  17. protected $noNeedRight = ['index', 'logout'];
  18. protected $layout = '';
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. //移除HTML标签
  23. $this->request->filter('trim,strip_tags,htmlspecialchars');
  24. }
  25. /**
  26. * 后台首页
  27. */
  28. public function index()
  29. {
  30. $cookieArr = ['adminskin' => "/^skin\-([a-z\-]+)\$/i", 'multiplenav' => "/^(0|1)\$/", 'multipletab' => "/^(0|1)\$/", 'show_submenu' => "/^(0|1)\$/"];
  31. foreach ($cookieArr as $key => $regex) {
  32. $cookieValue = $this->request->cookie($key);
  33. if (!is_null($cookieValue) && preg_match($regex, $cookieValue)) {
  34. config('fastadmin.' . $key, $cookieValue);
  35. }
  36. }
  37. //左侧菜单
  38. list($menulist, $navlist, $fixedmenu, $referermenu) = $this->auth->getSidebar([
  39. 'dashboard' => 'hot',
  40. 'addon' => ['new', 'red', 'badge'],
  41. 'auth/rule' => __('Menu'),
  42. ], $this->view->site['fixedpage']);
  43. $action = $this->request->request('action');
  44. if ($this->request->isPost()) {
  45. if ($action == 'refreshmenu') {
  46. $this->success('', null, ['menulist' => $menulist, 'navlist' => $navlist]);
  47. }
  48. }
  49. $this->assignconfig('cookie', ['prefix' => config('cookie.prefix')]);
  50. $this->view->assign('menulist', $menulist);
  51. $this->view->assign('navlist', $navlist);
  52. $this->view->assign('fixedmenu', $fixedmenu);
  53. $this->view->assign('referermenu', $referermenu);
  54. $this->view->assign('title', __('Home'));
  55. return $this->view->fetch();
  56. }
  57. /**
  58. * 管理员登录
  59. */
  60. public function login()
  61. {
  62. $url = $this->request->get('url', '', 'url_clean');
  63. $url = $url ?: 'index/index';
  64. if ($this->auth->isLogin()) {
  65. $this->success(__("You've logged in, do not login again"), $url);
  66. }
  67. //保持会话有效时长,单位:小时
  68. $keeyloginhours = 24;
  69. if ($this->request->isPost()) {
  70. $username = $this->request->post('username');
  71. $password = $this->request->post('password', '', null);
  72. $keeplogin = $this->request->post('keeplogin');
  73. $token = $this->request->post('__token__');
  74. $rule = [
  75. 'username' => 'require|length:3,30',
  76. 'password' => 'require|length:3,30',
  77. '__token__' => 'require|token',
  78. ];
  79. $data = [
  80. 'username' => $username,
  81. 'password' => $password,
  82. '__token__' => $token,
  83. ];
  84. if (Config::get('fastadmin.login_captcha')) {
  85. $rule['captcha'] = 'require|captcha';
  86. $data['captcha'] = $this->request->post('captcha');
  87. }
  88. $validate = new Validate($rule, [], ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]);
  89. $result = $validate->check($data);
  90. if (!$result) {
  91. $this->error($validate->getError(), $url, ['token' => $this->request->token()]);
  92. }
  93. AdminLog::setTitle(__('Login'));
  94. $result = $this->auth->login($username, $password, $keeplogin ? $keeyloginhours * 3600 : 0);
  95. if ($result === true) {
  96. Hook::listen("admin_login_after", $this->request);
  97. $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar]);
  98. } else {
  99. $msg = $this->auth->getError();
  100. $msg = $msg ? $msg : __('Username or password is incorrect');
  101. $this->error($msg, $url, ['token' => $this->request->token()]);
  102. }
  103. }
  104. // 根据客户端的cookie,判断是否可以自动登录
  105. if ($this->auth->autologin()) {
  106. Session::delete("referer");
  107. $this->redirect($url);
  108. }
  109. $background = Config::get('fastadmin.login_background');
  110. $background = $background ? (stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background) : '';
  111. $this->view->assign('keeyloginhours', $keeyloginhours);
  112. $this->view->assign('background', $background);
  113. $this->view->assign('title', __('Login'));
  114. Hook::listen("admin_login_init", $this->request);
  115. return $this->view->fetch();
  116. }
  117. /**
  118. * 退出登录
  119. */
  120. public function logout()
  121. {
  122. if ($this->request->isPost()) {
  123. $this->auth->logout();
  124. Hook::listen("admin_logout_after", $this->request);
  125. $this->success(__('Logout successful'), 'index/login');
  126. }
  127. $html = "<form id='logout_submit' name='logout_submit' action='' method='post'>" . token() . "<input type='submit' value='ok' style='display:none;'></form>";
  128. $html .= "<script>document.forms['logout_submit'].submit();</script>";
  129. return $html;
  130. }
  131. /**
  132. * 超时异常数据处理
  133. * 已开始考试,且 未主动交卷。。。。 关闭浏览器,断电时间超过考试时间。
  134. * @return void
  135. */
  136. public function timeout()
  137. {
  138. $where = ['exam_status'=>4,'countdown_time'=>['>',time()],'exam_collection_type'=>3,'endtime'=>0];
  139. $list = Db::name('real_exam')->where($where)->select();
  140. foreach ($list as $k => $v) {
  141. $score = 100;
  142. $fault_one_score = 25;
  143. $fault_two_score = 25;
  144. $fault_three_score = 25;
  145. $overtime_score = 10;//超时
  146. //故障现象
  147. $xianxian_score = 15;
  148. $other_jielun = [];
  149. $other_replace = '[{"fault_id":"","request_status":"0"}]';
  150. //故障表查找得分
  151. //特殊判断结果 故障部位
  152. // 002型 薄膜开关FPC排线 蜂鸣器出声口 检测剂 干燥管 维护管
  153. // 003型 检测剂 干燥管 维护管
  154. $question_arr = ['0002GZBW0001','0002GZBW0003','0002GZBW0005','0002GZBW0009','0002GZBW0010','0003GZBW0006','0003GZBW0007','0003GZBW0008'];
  155. //更新故障是否正确
  156. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$v['exam_id'],'flag'=>1])->select();
  157. if(!empty($fault_list)){
  158. foreach ($fault_list as $k1 =>$t){
  159. $answer_right = 2;
  160. //真实故障id 故障部位在 特殊故障部位里面
  161. if(in_array($t['fault_id'],$question_arr)){
  162. if(!empty($t['sim_fault_answer_value']) && substr($t['sim_fault_answer_value'], -1,1)==0){
  163. $answer_right=1;
  164. }
  165. }else{
  166. if(!empty($t['sim_fault_question_value']) && !empty($t['sim_fault_answer_value'])){
  167. if($t['sim_fault_question_value']!=$t['sim_fault_answer_value'] ){
  168. $answer_right=1;
  169. }
  170. }
  171. }
  172. Db::name('real_exam_fault')->where(['ref_id'=>$t['ref_id']])->update(['answer_right'=>$answer_right]);
  173. }
  174. }
  175. //计算得分,故障是否有扣分 扣分制
  176. $fault_right_list = Db::name('real_exam_fault')->where(['exam_id'=>$v['exam_id'],'flag'=>1,'answer_right'=>['>',0]])->select();
  177. if(!empty($fault_right_list)){
  178. if($fault_right_list[0]['answer_right']==1){
  179. $fault_one_score = 0;
  180. }
  181. if(!empty($fault_right_list[1]['answer_right']) && $fault_right_list[1]['answer_right']==1){
  182. $fault_two_score = 0;
  183. }
  184. if(!empty($fault_right_list[2]['answer_right']) && $fault_right_list[2]['answer_right']==1){
  185. $fault_three_score = 0;
  186. }
  187. }
  188. $total = $score-$fault_one_score-$fault_two_score-$fault_three_score-$xianxian_score-$overtime_score;
  189. //追加考试记录数据
  190. $add = [
  191. 'exam_id' => $v['exam_id'],
  192. 'total' => $total,
  193. 'fault_one_score' => $fault_one_score,
  194. 'fault_two_score' => $fault_two_score,
  195. 'fault_three_score' => $fault_three_score,
  196. 'xianxian_score' => $xianxian_score,
  197. 'other_replace' => $other_replace,
  198. 'other_jielun' => $other_jielun,
  199. 'overtime_score' => $overtime_score,
  200. ];
  201. Db::name('real_exam_score')->insert($add);
  202. //计算用时
  203. $endtime = time();
  204. $timediff = $endtime-$v['starttime'];
  205. $remain = $timediff%86400;
  206. //计算分钟数
  207. $remain = $remain%3600;
  208. $mins = intval($remain/60);
  209. //计算秒数
  210. $secs = $remain%60;
  211. $diffInSeconds = $endtime-$v['starttime']; // 两个时间戳之间的差异(秒)
  212. $minutes = floor($diffInSeconds / 60); // 计算分钟数
  213. $seconds = $diffInSeconds % 60; // 计算剩余的秒数
  214. $exam_duration = $mins*60+$secs;
  215. $exam_duration_text = $minutes.'分'.$seconds.'秒';
  216. $endtime = $endtime;
  217. $end_time = date('Y-m-d H:i:s',$endtime);
  218. $update = [
  219. 'total_score'=>$total,
  220. 'exam_duration'=>$exam_duration,
  221. 'exam_duration_text'=>$exam_duration_text,
  222. 'exam_status'=>5,
  223. 'endtime'=>$endtime,
  224. 'end_time'=>$end_time,
  225. ];
  226. //同步更新成绩
  227. Db::name('real_exam')->where(['exam_id'=>$v['exam_id']])->update($update);
  228. }
  229. unset($v);
  230. die('完成');
  231. }
  232. }