欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 资讯 > OJ在线评测系统 前端 完善题目提交服务 细讲异步前端请求与后端接口交互

OJ在线评测系统 前端 完善题目提交服务 细讲异步前端请求与后端接口交互

2024/12/26 22:43:19 来源:https://blog.csdn.net/qq_30500575/article/details/142578627  浏览:    关键词:OJ在线评测系统 前端 完善题目提交服务 细讲异步前端请求与后端接口交互

题目提交服务完善

这则笔记是我们来梳理一下前后端逻辑

主要是我们的提交逻辑

先是看前端

按钮绑定的是这个异步请求

async 关键字表示这个函数内部会使用 await 来等待异步操作完成。

异步提交表单数据

const doSubmit = async () => {// message.error("刷题机架构尚未完成");console.log("尚未完善");if (!question.value?.id) {return;}const res = await QuestionControllerService.doQuestionSubmitUsingPost({...form.value,questionId: question.value.id,});// console.log(res.code);if (res.code === 0) {message.success("提交成功");} else {message.error("提交失败," + res.message);}
};

找一下请求

使用 await 关键字等待 QuestionControllerService.doQuestionSubmitUsingPost 异步请求的结果。这个请求将 form.value 对象的所有属性(使用了对象展开运算符 ...)以及 questionId 属性一起发送到服务器。questionIdquestion.value.id 中提取。

请求在这

  public static doQuestionSubmitUsingPost(questionSubmitAddRequest: QuestionSubmitAddRequest,): CancelablePromise<BaseResponse_long_ | any> {return __request(OpenAPI, {method: 'POST',url: '/question/question_submit/do',body: questionSubmitAddRequest,errors: {401: `Unauthorized`,403: `Forbidden`,404: `Not Found`,},});}

我们的请求找到了后端接口

首先我们看一下controller层 表现层接口的的情况

@PostMapping("/question_submit/do")
public BaseResponse<Long> doQuestionSubmit(@RequestBody QuestionSubmitAddRequest questionSubmitAddRequest,HttpServletRequest request) {if (questionSubmitAddRequest == null || questionSubmitAddRequest.getQuestionId() <= 0) {throw new BusinessException(ErrorCode.PARAMS_ERROR);}final User loginUser = userService.getLoginUser(request);long questionSubmitId = questionSubmitService.doQuestionSubmit(questionSubmitAddRequest, loginUser);return ResultUtils.success(questionSubmitId);
}

在看我们的核心代码逻辑主要在service层里

业务逻辑层我们看一下实现类里面是怎么书写具体逻辑的

@Override
public long doQuestionSubmit(QuestionSubmitAddRequest questionSubmitAddRequest, User loginUser) {// 校验编程语言是否合法String language = questionSubmitAddRequest.getLanguage();QuestionSubmitLanguageEnum languageEnum = QuestionSubmitLanguageEnum.getEnumByValue(language);if (languageEnum == null) {throw new BusinessException(ErrorCode.PARAMS_ERROR, "编程语言错误");}long questionId = questionSubmitAddRequest.getQuestionId();// 判断实体是否存在,根据类别获取实体Question question = questionService.getById(questionId);if (question == null) {throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);}// 是否已提交题目long userId = loginUser.getId();// 每个用户串行提交题目QuestionSubmit questionSubmit = new QuestionSubmit();questionSubmit.setUserId(userId);questionSubmit.setQuestionId(questionId);questionSubmit.setCode(questionSubmitAddRequest.getCode());questionSubmit.setLanguage(language);// 设置初始状态questionSubmit.setStatus(QuestionSubmitStatusEnum.WAITING.getValue());questionSubmit.setJudgeInfo("{}");boolean save = this.save(questionSubmit);if (!save){throw new BusinessException(ErrorCode.SYSTEM_ERROR, "数据插入失败");}// todo 执行判题服务Long questionSubmitId = questionSubmit.getId();// 执行判题服务CompletableFuture.runAsync(() -> {judgeService.doJudge(questionSubmitId);});return questionSubmitId;
}

前端代码分析

JS部分

TypeScript部分

后端代码分析

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com