|
@@ -193,19 +193,19 @@ class Paper extends Base
|
|
{
|
|
{
|
|
$request = Request::instance();
|
|
$request = Request::instance();
|
|
$user_id = $this->auth->id;
|
|
$user_id = $this->auth->id;
|
|
-
|
|
|
|
$grade_id = $request->post('grade_id');
|
|
$grade_id = $request->post('grade_id');
|
|
$paper_id = $request->post('paper_id/d', 0);
|
|
$paper_id = $request->post('paper_id/d', 0);
|
|
$questions = $request->post('questions/a', []);
|
|
$questions = $request->post('questions/a', []);
|
|
$start_time = $request->post('start_time/d', time());
|
|
$start_time = $request->post('start_time/d', time());
|
|
$room_id = 0;
|
|
$room_id = 0;
|
|
$room_grade_id = $request->post('room_grade_id/d', 0);
|
|
$room_grade_id = $request->post('room_grade_id/d', 0);
|
|
-
|
|
|
|
|
|
+
|
|
if (!$user_id || !$paper_id || !$questions) {
|
|
if (!$user_id || !$paper_id || !$questions) {
|
|
$this->error('提交数据有误');
|
|
$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();
|
|
$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){
|
|
if(!$check){
|
|
$this->error('交卷有误,或者您已交卷');
|
|
$this->error('交卷有误,或者您已交卷');
|
|
}
|
|
}
|
|
@@ -270,10 +270,10 @@ class Paper extends Base
|
|
$this->error('交卷失败');
|
|
$this->error('交卷失败');
|
|
}
|
|
}
|
|
|
|
|
|
- // 如果提交成功,推送积分到工行
|
|
|
|
|
|
+ // 异步推送积分到工行(写入队列)
|
|
$cephone = '15388010006';
|
|
$cephone = '15388010006';
|
|
//$cephone = $this->auth->mobile;
|
|
//$cephone = $this->auth->mobile;
|
|
- $this->update_villager_integral($cephone, $result['score'], '10938', [$this->auth->nickname]);
|
|
|
|
|
|
+ $this->addIcbcQueue($cephone, $result['score'], '10938', $this->auth->nickname);
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
@@ -434,6 +434,116 @@ class Paper extends Base
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * 添加工行积分任务到队列
|
|
|
|
+ * @param string $mobile_phone 手机号
|
|
|
|
+ * @param int $integral_value 积分值
|
|
|
|
+ * @param string $integral_type 积分类型
|
|
|
|
+ * @param string $nickname 用户昵称
|
|
|
|
+ */
|
|
|
|
+ private function addIcbcQueue($mobile_phone, $integral_value, $integral_type, $nickname)
|
|
|
|
+ {
|
|
|
|
+ try {
|
|
|
|
+ $data = [
|
|
|
|
+ 'mobile_phone' => $mobile_phone,
|
|
|
|
+ 'integral_value' => $integral_value,
|
|
|
|
+ 'integral_type' => $integral_type,
|
|
|
|
+ 'nickname' => $nickname,
|
|
|
|
+ 'status' => 0, // 0-待处理, 1-处理中, 2-成功, 3-失败
|
|
|
|
+ 'retry_count' => 0,
|
|
|
|
+ 'createtime' => time(),
|
|
|
|
+ 'updatetime' => time(),
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ Db::name('icbc_queue')->insert($data);
|
|
|
|
+
|
|
|
|
+ // 立即尝试异步处理(不阻塞当前请求)
|
|
|
|
+ $this->processIcbcQueueAsync();
|
|
|
|
+
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
+ // 队列写入失败,记录日志但不影响主流程
|
|
|
|
+ \think\Log::record('工行队列写入失败: ' . $e->getMessage(), 'error');
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 异步处理工行队列(不阻塞当前请求)
|
|
|
|
+ */
|
|
|
|
+ private function processIcbcQueueAsync()
|
|
|
|
+ {
|
|
|
|
+ // 使用fastcgi_finish_request()立即返回响应给用户
|
|
|
|
+ if (function_exists('fastcgi_finish_request')) {
|
|
|
|
+ fastcgi_finish_request();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 后台处理队列
|
|
|
|
+ $this->processIcbcQueue();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 处理工行队列任务
|
|
|
|
+ */
|
|
|
|
+ public function processIcbcQueue()
|
|
|
|
+ {
|
|
|
|
+ // 获取待处理的任务(限制每次处理10条)
|
|
|
|
+ $tasks = Db::name('icbc_queue')
|
|
|
|
+ ->where('status', 0)
|
|
|
|
+ ->where('retry_count', '<', 3) // 最多重试3次
|
|
|
|
+ ->limit(10)
|
|
|
|
+ ->select();
|
|
|
|
+
|
|
|
|
+ foreach ($tasks as $task) {
|
|
|
|
+ try {
|
|
|
|
+ // 更新状态为处理中
|
|
|
|
+ Db::name('icbc_queue')->where('id', $task['id'])->update([
|
|
|
|
+ 'status' => 1,
|
|
|
|
+ 'updatetime' => time()
|
|
|
|
+ ]);
|
|
|
|
+
|
|
|
|
+ // 调用工行接口
|
|
|
|
+ $result = $this->update_villager_integral(
|
|
|
|
+ $task['mobile_phone'],
|
|
|
|
+ $task['integral_value'],
|
|
|
|
+ $task['integral_type'],
|
|
|
|
+ [$task['nickname']]
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ // 根据结果更新状态
|
|
|
|
+ if (isset($result['return_code']) && $result['return_code'] == 0) {
|
|
|
|
+ // 成功
|
|
|
|
+ Db::name('icbc_queue')->where('id', $task['id'])->update([
|
|
|
|
+ 'status' => 2,
|
|
|
|
+ 'result' => json_encode($result, JSON_UNESCAPED_UNICODE),
|
|
|
|
+ 'updatetime' => time()
|
|
|
|
+ ]);
|
|
|
|
+ } else {
|
|
|
|
+ // 失败,增加重试次数
|
|
|
|
+ $retry_count = $task['retry_count'] + 1;
|
|
|
|
+ $status = $retry_count >= 3 ? 3 : 0; // 重试3次后标记为失败
|
|
|
|
+
|
|
|
|
+ Db::name('icbc_queue')->where('id', $task['id'])->update([
|
|
|
|
+ 'status' => $status,
|
|
|
|
+ 'retry_count' => $retry_count,
|
|
|
|
+ 'result' => json_encode($result, JSON_UNESCAPED_UNICODE),
|
|
|
|
+ 'error_msg' => isset($result['return_msg']) ? $result['return_msg'] : '未知错误',
|
|
|
|
+ 'updatetime' => time()
|
|
|
|
+ ]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
+ // 异常处理
|
|
|
|
+ Db::name('icbc_queue')->where('id', $task['id'])->update([
|
|
|
|
+ 'status' => 0,
|
|
|
|
+ 'retry_count' => $task['retry_count'] + 1,
|
|
|
|
+ 'error_msg' => $e->getMessage(),
|
|
|
|
+ 'updatetime' => time()
|
|
|
|
+ ]);
|
|
|
|
+
|
|
|
|
+ \think\Log::record('工行队列处理异常[ID:'.$task['id'].']: ' . $e->getMessage(), 'error');
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
* 写入工行接口日志
|
|
* 写入工行接口日志
|
|
* @param bool $is_success 是否成功
|
|
* @param bool $is_success 是否成功
|
|
* @param array $result 返回结果
|
|
* @param array $result 返回结果
|