function ($query) { $query->withField('id, name'); } ]) ->where('status', CommonStatus::NORMAL) ->where('is_only_room', 0)// 过滤仅考场使用的试卷 ->whereRaw("((start_time = 0 and end_time = 0) or (start_time < {$now} and end_time > {$now}))"); // 分类 if ($cate_id) { $child_cate_ids = CateModel::getChildId($cate_id); array_push($child_cate_ids, $cate_id); $query->whereIn('cate_id', $child_cate_ids); } // 排序 if ($sort && $sort != 'null') { $sort = explode('|', $sort); $field = $sort[0]; $order_by = $sort[1]; $field = in_array($field, ['join_count']) ? $field : 'join_count'; $order_by = $order_by == 'desc' ? 'desc' : 'asc'; $query->order("{$field} $order_by"); } $list = $query->paginate(); $this->success('', ['list' => $list]); } public function lists() { $list = Db::name('exam_paper')->field('id,image,title,updatetime')->where('status','normal') ->order('weigh desc, id desc')->autopage()->select(); $list = list_domain_image($list,['image']); $this->success('', $list); } public function detail(){ $id = input('id'); $info = Db::name('exam_paper')->field('id,image,title,content,updatetime,quantity')->where('status','normal') ->find($id); $info = info_domain_image($info,['image']); $this->success('', $info); } /** * 试卷取题接口 */ public function getExamQuestion() { $paper_id = input('paper_id/d', 0); $room_id = input('room_id/d', 0); // 验证是否需要绑定手机号 // UserModel::isMustBindMobile($this->auth->getUser()); // 预创建考场考试记录 // $room_grade_id = ExamService::preRoomGrade($room_id, $this->auth->id); // 获取试卷题目 $question_data = ExamService::getExamQuestion($paper_id, $room_id); // $this->success('', array_merge($question_data, ['room_grade_id' => $room_grade_id])); $this->success('', $question_data); } //开始考试接口 public function startpaper(){ $paper_id = input('paper_id/d', 0); $user_id = $this->auth->id; //检查考试状态 /*$check = Db::name('exam_grade')->where('user_id', $user_id)->where('status',1)->find(); if($check){ $this->success('您有其他考试正在进行中,即将继续考试',0);//直接给成功,数据返回0,前端跳转 }*/ //检查试卷 $paper = PaperModel::get($paper_id); switch (true) { case !$paper: $this->error('科普答题信息不存在'); case $paper->status != 'NORMAL': $this->error('科普答题信息不存在'); case $paper->mode == 'RANDOM' && !$paper->configs: $this->error('科普答题信息未配置'); } //时间限制 /*if ($paper['start_time'] > 0 && $paper['start_time'] > time()) { $this->error('该试卷未开始,不能参与考试'); } if ($paper['end_time'] > 0 && $paper['end_time'] < time()) { $this->error('该试卷已结束,不能参与考试'); }*/ //考试资格 /*if(!in_array($user_id,explode(',',$paper['user_ids']))){ $this->error('您不能参加该考试'); }*/ //次数限制 if ($paper['limit_count'] > 0){ $my_count = Db::name('exam_grade')->where('user_id', $user_id)->where('paper_id', $paper_id)->where('status',2)->count(); if($my_count >= $paper['limit_count']) { $this->error('在该科普中您的答题次数已达上限'); } } //记录为已开始,计划任务倒计时之后 自动结束 $data = [ 'cate_id' => $paper['cate_id'], 'user_id' => $this->auth->id, 'paper_id' => $paper_id, 'mode' => $paper['mode'], 'total_score' => $paper['total_score'], 'total_count' => $paper['quantity'], 'start_time' => time(), 'createtime' => time(), 'status' => 1, 'limit_time' => $paper['limit_time'], //限时N秒 'last_time' => $paper['limit_time'] > 0 ? (time() + $paper['limit_time']) : 0, //最后限制交卷时间,时间戳 ]; $grade_id = Db::name('exam_grade')->insertGetId($data); $this->success('',$grade_id); } //进行中考试 public function half_examing(){ $user_id = $this->auth->id; $check = Db::name('exam_grade')->where('user_id', $user_id)->where('status',1)->find(); if(empty($check)){ $this->error('您没有进行中的考试'); } $paper_id = $check['paper_id']; // 获取试卷题目 $question_data = ExamService::getExamQuestion($paper_id, 0); $question_data['paper']['limit_time'] = $check['last_time'] - time();//倒计时秒数 $this->success('', $question_data); } /** * 交卷 */ public function submit() { $request = Request::instance(); $user_id = $this->auth->id; $grade_id = $request->post('grade_id'); $paper_id = $request->post('paper_id/d', 0); $questions = $request->post('questions/a', []); $start_time = $request->post('start_time/d', time()); $room_id = 0; $room_grade_id = $request->post('room_grade_id/d', 0); if (!$user_id || !$paper_id || !$questions) { $this->error('提交数据有误'); } $check = Db::name('exam_grade')->where('status',1)->where('id',$grade_id)->where('user_id',$user_id)->where('paper_id',$paper_id)->find(); if(!$check){ $this->error('交卷有误,或者您已交卷'); } $start_time = $check['start_time']; // 考场考试 if ($room_id) { if (!$room_grade_id) { $this->error('提交数据不合法'); } // 考场考试 $result = ExamService::roomExam($user_id, $room_id, $room_grade_id, $questions, $start_time, $paper, $room, $is_makeup, $room_grade_log); // 记录考场考试成绩 $room_grade_log->allowField(true)->save( array_merge( $result, [ // 'cate_id' => $paper['cate_id'], 'user_id' => $user_id, 'paper_id' => $paper_id, 'is_makeup' => $is_makeup, 'is_pre' => 0, // 提交成绩后不再为预创建标记 ], [ 'exam_mode' => ExamMode::ROOM, ] ) ); } else { $result = ExamService::paperExam($user_id, $paper_id, $questions, $start_time, $paper); // 记录考试成绩 /*GradeModel::create(array_merge( $result, [ 'cate_id' => $paper['cate_id'], 'user_id' => $user_id, 'paper_id' => $paper_id, ], [ // 'exam_mode' => ExamMode::PAPER, 'date' => date('Y-m-d'), ]), true);*/ $update = array_merge( $result, [ 'updatetime' => time(), 'date' => date('Y-m-d'), 'status' => 2, 'finish_time' => time(), ]); unset($update['pass_score']); unset($update['start_time']); $rs = Db::name('exam_grade')->where('id',$grade_id)->update($update); if($rs === false){ $this->error('交卷失败'); } // 如果提交成功,推送积分到工行 $cephone = '15388010006'; //$cephone = $this->auth->mobile; $this->update_villager_integral($cephone, $result['score'], '10938', [$this->auth->nickname]); } $result['nickname'] = $this->auth->nickname; unset($result['question_ids']); unset($result['user_answers']); unset($result['configs']); $this->success('',$result); } /** * 积分维护接口 - 推送积分数据到工行 * @param string $mobile_phone 用户手机号 * @param int $integral_value 变动的积分值(整数) * @param string $integral_type 积分类型 * @param array $remarks 备用字段数组(可选,最多7个) * @return array 返回接口响应结果 */ public function update_villager_integral($mobile_phone = '', $integral_value = 0, $integral_type = '', $remarks = []) { // 如果没有传入手机号,尝试从登录用户获取 if (empty($mobile_phone) && isset($this->auth->mobile)) { $mobile_phone = $this->auth->mobile; } // 生成16位唯一序列号(时间戳10位+随机数6位) $fSeqNo = time() . str_pad(mt_rand(0, 999999), 6, '0', STR_PAD_LEFT); // 构建业务参数 $biz_content = [ 'fSeqNo' => $fSeqNo, 'corpCode' => 'xingfulishequ', 'mobilePhone' => $mobile_phone, 'integralValue' => (string)$integral_value, // 确保是字符串类型 'integralType' => $integral_type, ]; // 添加备用字段(如果有) for ($i = 1; $i <= 7; $i++) { if (isset($remarks[$i - 1]) && !empty($remarks[$i - 1])) { $biz_content['remark' . $i] = $remarks[$i - 1]; } } // 工行RSA密钥(字符串格式) $public_key = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwFgHD4kzEVPdOj03ctKM7KV+16bWZ5BMNgvEeuEQwfQYkRVwI9HFOGkwNTMn5hiJXHnlXYCX+zp5r6R52MY0O7BsTCLT7aHaxsANsvI9ABGx3OaTVlPB59M6GPbJh0uXvio0m1r/lTW3Z60RU6Q3oid/rNhP3CiNgg0W6O3AGqwIDAQAB'; $private_key = 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCR8/ZvKPAdZzsyvapySvztQm56s1N59ynKMOWpUbK4c5MNWMl+q3dvsp+UiSAx2TAeGkYfW3W6nO/3Y0hAZki99fmuzpPujEeHhs79HNwGZYQjN71Vck2JeflTq8jpL+9/up0Kz2nbwtZDMKTTEgbfNeO24gV1bmvD2kQ9j66RIyuXSDwQbVbQfl6LiqKoJm3rbtsbwX1Ytc0/Szslyor1VdPZWnHDMm3m78Hqu7X3vL6K7fAW/4FVbeKV7vWjvyiTQfETmuADqMdsqV5YeqUZWE/Dnzg+6JV/3L9nJ8f+/mPlh8k1h1oW035GrADKFKf1M2ujKKeHICnj7qKCdBeNAgMBAAECggEAHkh+U2YtHAy1Tbvox7ojbJ8iCTd4FJBiDV/D5zPaX0crtdM8S5oMOBLZ5ZnmIjGsODK/ZfY2ITg62huxfBs88J0+5zRZoV9d4BLqk74PMQyTNDN2h2omCGZUgzXbg/a8PMZdm0aZ8k0k4+AN8vWEk3+89c9Dzq/QkFyTWCqdz+Mp9NkcOjA255kj5/D1q9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msg_id' => createUniqueNo('msg', time()), 'format' => 'json', 'charset' => 'UTF-8', 'encrypt_type' => 'AES', 'sign_type' => 'RSA2', ]; // 记录请求日志(调试用) \think\Log::record('积分维护接口请求参数: ' . json_encode([ 'app_id' => $data['app_id'], 'msg_id' => $data['msg_id'], 'biz_content' => $biz_content ], JSON_UNESCAPED_UNICODE), 'info'); try { // 创建工行客户端 $client = new \DefaultIcbcClient( $data['app_id'], $private_key, $data['sign_type'], $data['charset'], $data['format'], $public_key, '', '', '', '' ); // 构建请求参数 $request = [ 'serviceUrl' => 'https://gw.dccnet.com.cn:8084/api/mybank/farm/farmplatf/updateVillagerIntegral/V1', 'method' => 'POST', 'isNeedEncrypt' => false, 'extraParams' => null, 'biz_content' => $biz_content, ]; // 发送请求 $response = $client->execute($request, $data['msg_id'], ''); // 记录原始响应(调试用) \think\Log::record('积分维护接口原始响应: ' . $response, 'info'); // 检查响应是否为空 if (empty($response)) { $resultData = [ 'return_code' => '-1', 'return_msg' => '接口无响应', 'raw_response' => $response ]; $this->writeIcbcLog(false, $resultData, $biz_content); return $resultData; } // 解析响应 $result = json_decode($response, true); // 检查JSON解析是否成功 if (json_last_error() !== JSON_ERROR_NONE) { $resultData = [ 'return_code' => '-2', 'return_msg' => 'JSON解析失败: ' . json_last_error_msg(), 'raw_response' => $response ]; $this->writeIcbcLog(false, $resultData, $biz_content); return $resultData; } // 检查是否有response_biz_content(按API文档,响应数据在这个字段里) if (isset($result['response_biz_content'])) { $biz_result = json_decode($result['response_biz_content'], true); if ($biz_result) { // 合并业务响应和原始响应 $resultData = array_merge($biz_result, ['raw_response' => $response]); $this->writeIcbcLog($resultData['return_code'] == 0, $resultData, $biz_content); return $resultData; } } // 如果直接有return_code,直接返回 if (isset($result['return_code'])) { $this->writeIcbcLog($result['return_code'] == 0, $result, $biz_content); return $result; } // 返回完整结果 $resultData = $result ? $result : [ 'return_code' => '-3', 'return_msg' => '未知响应格式', 'raw_response' => $response ]; $this->writeIcbcLog(false, $resultData, $biz_content); return $resultData; } catch (\Exception $e) { // 捕获异常 \think\Log::record('积分维护接口异常: ' . $e->getMessage(), 'error'); $resultData = [ 'return_code' => '-99', 'return_msg' => '接口调用异常: ' . $e->getMessage(), 'raw_response' => '' ]; $this->writeIcbcLog(false, $resultData, $biz_content); return $resultData; } } /** * 写入工行接口日志 * @param bool $is_success 是否成功 * @param array $result 返回结果 * @param array $request_data 请求数据 */ private function writeIcbcLog($is_success, $result, $request_data) { // 确定日志文件路径 $log_dir = RUNTIME_PATH . 'icbc_log/'; if (!is_dir($log_dir)) { mkdir($log_dir, 0755, true); } $filename = $is_success ? 'suc.txt' : 'failicbc.txt'; $filepath = $log_dir . $filename; // 构建日志内容 $log_content = [ '时间' => date('Y-m-d H:i:s'), '请求数据' => $request_data, '返回数据' => $result, '原始响应' => isset($result['raw_response']) ? $result['raw_response'] : '', '分隔线' => str_repeat('-', 80) ]; $log_text = "\n" . json_encode($log_content, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n"; // 追加写入文件 file_put_contents($filepath, $log_text, FILE_APPEND); } /* * 查看错题 * Robin * @param $ids * */ public function error_ids($ids) { $questions = QuestionModel::whereIn('id', ($ids))->select(); $this->success('', $questions); } }