From 480008777b139dfdcf95ae053540f1f876e8ec64 Mon Sep 17 00:00:00 2001 From: Haixing Hu Date: Sun, 23 Feb 2025 13:48:54 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=88=A0=E9=99=A4=E5=B7=A5?= =?UTF-8?q?=E4=BD=9C=E9=A2=86=E5=9F=9F=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../survey/controller/QuestionController.java | 8 +- .../java/ltd/qubit/survey/model/Question.java | 6 +- .../java/ltd/qubit/survey/model/User.java | 5 - .../resources/mybatis/mapper/OptionMapper.xml | 52 +-- .../mybatis/mapper/QuestionMapper.xml | 59 +-- .../mybatis/mapper/SurveyResponseMapper.xml | 48 +-- .../resources/mybatis/mapper/UserMapper.xml | 38 +- .../controller/QuestionController.class | Bin 3170 -> 3272 bytes doc/大模型应用需求调研问卷.md | 336 ++++++++++-------- frontend/.eslintrc.js | 6 +- frontend/src/stores/survey.js | 14 +- frontend/src/stores/user.js | 59 ++- frontend/src/utils/request.js | 27 +- frontend/src/views/RegisterView.vue | 59 +-- 14 files changed, 402 insertions(+), 315 deletions(-) diff --git a/backend/src/main/java/ltd/qubit/survey/controller/QuestionController.java b/backend/src/main/java/ltd/qubit/survey/controller/QuestionController.java index e983c75..f76a0e8 100644 --- a/backend/src/main/java/ltd/qubit/survey/controller/QuestionController.java +++ b/backend/src/main/java/ltd/qubit/survey/controller/QuestionController.java @@ -8,7 +8,7 @@ import ltd.qubit.survey.service.QuestionService; import ltd.qubit.survey.service.OptionService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** @@ -52,9 +52,9 @@ public class QuestionController { */ @GetMapping("/question/next") public Question getNextQuestion( - @PathVariable Long userId, - @PathVariable Integer currentQuestionNumber, - @PathVariable List selectedOptions) { + @RequestParam Long userId, + @RequestParam(defaultValue = "0") Integer currentQuestionNumber, + @RequestParam(required = false) List selectedOptions) { return questionService.getNextQuestion(userId, currentQuestionNumber, selectedOptions) .orElseThrow(() -> new IllegalArgumentException("没有更多问题了")); } diff --git a/backend/src/main/java/ltd/qubit/survey/model/Question.java b/backend/src/main/java/ltd/qubit/survey/model/Question.java index 3df9dbd..163c795 100644 --- a/backend/src/main/java/ltd/qubit/survey/model/Question.java +++ b/backend/src/main/java/ltd/qubit/survey/model/Question.java @@ -16,7 +16,7 @@ public class Question { /** * 问题序号 */ - private Integer questionNumber; + private Integer number; /** * 问题内容 @@ -26,7 +26,7 @@ public class Question { /** * 问题类型(单选、多选、文本) */ - private QuestionType questionType; + private QuestionType type; /** * 针对的工作领域(为null表示通用问题) @@ -41,7 +41,7 @@ public class Question { /** * 跳转逻辑(JSON格式) */ - private String nextQuestionLogic; + private String next; /** * 创建时间 diff --git a/backend/src/main/java/ltd/qubit/survey/model/User.java b/backend/src/main/java/ltd/qubit/survey/model/User.java index 6dca0fa..39fde69 100644 --- a/backend/src/main/java/ltd/qubit/survey/model/User.java +++ b/backend/src/main/java/ltd/qubit/survey/model/User.java @@ -23,11 +23,6 @@ public class User { */ private String phone; - /** - * 工作领域 - */ - private WorkArea workArea; - /** * 岗位性质 */ diff --git a/backend/src/main/resources/mybatis/mapper/OptionMapper.xml b/backend/src/main/resources/mybatis/mapper/OptionMapper.xml index a0533ce..da7f3c3 100644 --- a/backend/src/main/resources/mybatis/mapper/OptionMapper.xml +++ b/backend/src/main/resources/mybatis/mapper/OptionMapper.xml @@ -2,7 +2,7 @@ - + @@ -13,18 +13,18 @@ - id, question_id, option_code, content, requires_text, created_at + `id`, `question_id`, `option_code`, `content`, `requires_text`, `created_at` - INSERT INTO options (question_id, option_code, content, requires_text) + INSERT INTO `option` (`question_id`, `option_code`, `content`, `requires_text`) VALUES (#{questionId}, #{optionCode}, #{content}, #{requiresText}) - INSERT INTO options (question_id, option_code, content, requires_text) + INSERT INTO `option` (`question_id`, `option_code`, `content`, `requires_text`) VALUES (#{item.questionId}, #{item.optionCode}, #{item.content}, #{item.requiresText}) @@ -33,51 +33,51 @@ - UPDATE options - SET question_id = #{questionId}, - option_code = #{optionCode}, - content = #{content}, - requires_text = #{requiresText} - WHERE id = #{id} + UPDATE `option` + SET `question_id` = #{questionId}, + `option_code` = #{optionCode}, + `content` = #{content}, + `requires_text` = #{requiresText} + WHERE `id` = #{id} - DELETE FROM options WHERE id = #{id} + DELETE FROM `option` WHERE `id` = #{id} - DELETE FROM options WHERE question_id = #{questionId} + DELETE FROM `option` WHERE `question_id` = #{questionId} - SELECT - FROM options - WHERE id = #{id} + FROM `option` + WHERE `id` = #{id} - SELECT - FROM options - ORDER BY question_id, option_code + FROM `option` + ORDER BY `question_id`, `option_code` - SELECT - FROM options - WHERE question_id = #{questionId} - ORDER BY option_code + FROM `option` + WHERE `question_id` = #{questionId} + ORDER BY `option_code` - SELECT - FROM options - WHERE question_id = #{questionId} - AND option_code = #{optionCode} + FROM `option` + WHERE `question_id` = #{questionId} + AND `option_code` = #{optionCode} \ No newline at end of file diff --git a/backend/src/main/resources/mybatis/mapper/QuestionMapper.xml b/backend/src/main/resources/mybatis/mapper/QuestionMapper.xml index 4d3d902..79c07ed 100644 --- a/backend/src/main/resources/mybatis/mapper/QuestionMapper.xml +++ b/backend/src/main/resources/mybatis/mapper/QuestionMapper.xml @@ -4,83 +4,84 @@ - + - + - + - id, question_number, content, question_type, work_area, is_required, next_question_logic, created_at + `id`, `number`, `content`, `type`, `work_area`, `is_required`, `next`, `created_at` - INSERT INTO questions (question_number, content, question_type, work_area, is_required, next_question_logic) - VALUES (#{questionNumber}, #{content}, #{questionType}, #{workArea}, #{isRequired}, #{nextQuestionLogic}) + INSERT INTO `question` (`number`, `content`, `type`, `work_area`, `is_required`, `next`) + VALUES (#{number}, #{content}, #{type}, #{workArea}, #{isRequired}, + #{next,typeHandler=ltd.qubit.survey.common.mybatis.JsonTypeHandler}) - UPDATE questions - SET question_number = #{questionNumber}, - content = #{content}, - question_type = #{questionType}, - work_area = #{workArea}, - is_required = #{isRequired}, - next_question_logic = #{nextQuestionLogic} - WHERE id = #{id} + UPDATE `question` + SET `number` = #{number}, + `content` = #{content}, + `type` = #{type}, + `work_area` = #{workArea}, + `is_required` = #{isRequired}, + `next` = #{next,typeHandler=ltd.qubit.survey.common.mybatis.JsonTypeHandler} + WHERE `id` = #{id} - DELETE FROM questions WHERE id = #{id} + DELETE FROM `question` WHERE `id` = #{id} \ No newline at end of file diff --git a/backend/src/main/resources/mybatis/mapper/SurveyResponseMapper.xml b/backend/src/main/resources/mybatis/mapper/SurveyResponseMapper.xml index a3b78aa..c954675 100644 --- a/backend/src/main/resources/mybatis/mapper/SurveyResponseMapper.xml +++ b/backend/src/main/resources/mybatis/mapper/SurveyResponseMapper.xml @@ -13,18 +13,18 @@ - id, user_id, question_id, selected_options, text_answer, created_at + `id`, `user_id`, `question_id`, `selected_options`, `text_answer`, `created_at` - INSERT INTO survey_responses (user_id, question_id, selected_options, text_answer) + INSERT INTO `survey_response` (`user_id`, `question_id`, `selected_options`, `text_answer`) VALUES (#{userId}, #{questionId}, #{selectedOptions,typeHandler=ltd.qubit.survey.common.mybatis.JsonTypeHandler}, #{textAnswer}) - INSERT INTO survey_responses (user_id, question_id, selected_options, text_answer) + INSERT INTO `survey_response` (`user_id`, `question_id`, `selected_options`, `text_answer`) VALUES (#{item.userId}, #{item.questionId}, @@ -35,59 +35,59 @@ - UPDATE survey_responses - SET user_id = #{userId}, - question_id = #{questionId}, - selected_options = #{selectedOptions,typeHandler=ltd.qubit.survey.common.mybatis.JsonTypeHandler}, - text_answer = #{textAnswer} - WHERE id = #{id} + UPDATE `survey_response` + SET `user_id` = #{userId}, + `question_id` = #{questionId}, + `selected_options` = #{selectedOptions,typeHandler=ltd.qubit.survey.common.mybatis.JsonTypeHandler}, + `text_answer` = #{textAnswer} + WHERE `id` = #{id} - DELETE FROM survey_responses WHERE id = #{id} + DELETE FROM `survey_response` WHERE `id` = #{id} - DELETE FROM survey_responses WHERE user_id = #{userId} + DELETE FROM `survey_response` WHERE `user_id` = #{userId} \ No newline at end of file diff --git a/backend/src/main/resources/mybatis/mapper/UserMapper.xml b/backend/src/main/resources/mybatis/mapper/UserMapper.xml index da8010d..431e6fd 100644 --- a/backend/src/main/resources/mybatis/mapper/UserMapper.xml +++ b/backend/src/main/resources/mybatis/mapper/UserMapper.xml @@ -6,63 +6,61 @@ - - id, name, phone, work_area, position_type, created_at + `id`, `name`, `phone`, `position_type`, `created_at` - INSERT INTO users (name, phone, work_area, position_type) - VALUES (#{name}, #{phone}, #{workArea}, #{positionType}) + INSERT INTO `user` (`name`, `phone`, `position_type`) + VALUES (#{name}, #{phone}, #{positionType}) - UPDATE users - SET name = #{name}, - phone = #{phone}, - work_area = #{workArea}, - position_type = #{positionType} - WHERE id = #{id} + UPDATE `user` + SET `name` = #{name}, + `phone` = #{phone}, + `position_type` = #{positionType} + WHERE `id` = #{id} - DELETE FROM users WHERE id = #{id} + DELETE FROM `user` WHERE `id` = #{id} \ No newline at end of file diff --git a/backend/target/classes/ltd/qubit/survey/controller/QuestionController.class b/backend/target/classes/ltd/qubit/survey/controller/QuestionController.class index 616664eba3c6fe850ba785c02406ed00e56f9b71..a7134cc6cd7fcd4e17de45f618a86c860ff3aa59 100644 GIT binary patch delta 246 zcmXYry-EX75QV?VhRu3iP)K70V`G|tkj6s9cSvG>H2z-Qn;6|F*<@cN3xba!+gaGy zh>xL`K7%-d#ms!1IdhngwYQ3S|9O4^_PKvDUw-yltT)GEQ9N=_&cQUluuC1nZmUh5{EMJ^HKkSA4Dv5w65AWW;s88nGFHW?azo4=m0vvkF(m*1 delta 198 zcmX>h`ACB6)W2Q(7#J9A8L~EVJz--C)tDHoHMxxaDPzRs7!E1 { // 获取下一个问题 async function fetchNextQuestion(userId, selectedOptions) { try { - const response = await getNextQuestion(userId, currentQuestionNumber.value, selectedOptions); - if (response.data) { - currentQuestion.value = response.data; - currentQuestionNumber.value++; - await fetchQuestionOptions(response.data.id); + // 如果是初始化(currentQuestionNumber为0),则从第3题开始 + const questionNumber = currentQuestionNumber.value === 0 ? 3 : currentQuestionNumber.value; + const response = await getNextQuestion(userId, questionNumber, selectedOptions); + if (response) { + currentQuestion.value = response; + currentQuestionNumber.value = response.questionNumber; + await fetchQuestionOptions(response.id); } else { isCompleted.value = true; } - return response.data; + return response; } catch (error) { showToast('获取下一个问题失败:' + error.message); throw error; diff --git a/frontend/src/stores/user.js b/frontend/src/stores/user.js index 0370090..88d4ad1 100644 --- a/frontend/src/stores/user.js +++ b/frontend/src/stores/user.js @@ -10,12 +10,37 @@ export const useUserStore = defineStore('user', () => { // 注册用户 async function registerUser(data) { try { + console.log('发送注册请求,数据:', data); const response = await register(data); - userId.value = response.data.id; + console.log('注册请求响应:', response); + + // 检查响应数据结构 + if (!response) { + throw new Error('注册失败:服务器无响应'); + } + + // 后端直接返回 User 对象,response 就是 User 对象 + const userData = response; + console.log('用户数据:', userData); + + if (!userData || !userData.id) { + throw new Error('注册失败:无效的用户数据'); + } + + // 保存用户信息 + userId.value = String(userData.id); + userInfo.value = userData; localStorage.setItem('userId', userId.value); - return response; + + console.log('用户信息已保存,userId:', userId.value); + return userData; } catch (error) { - showToast('注册失败:' + error.message); + console.error('注册请求失败:', error); + if (error.response) { + console.error('错误响应:', error.response); + } + const message = error.response?.data?.message || error.message || '注册失败,请稍后重试'; + showToast(message); throw error; } } @@ -23,10 +48,18 @@ export const useUserStore = defineStore('user', () => { // 检查手机号是否已注册 async function checkPhoneNumber(phone) { try { + console.log('检查手机号:', phone); const response = await checkPhone(phone); - return response.data; + console.log('检查手机号响应:', response); + // 如果响应是包装过的,尝试获取实际数据 + return response.data ?? response; } catch (error) { - showToast('检查手机号失败:' + error.message); + console.error('检查手机号失败:', error); + if (error.response) { + console.error('错误响应:', error.response); + } + const message = error.response?.data?.message || error.message || '检查手机号失败,请稍后重试'; + showToast(message); throw error; } } @@ -34,11 +67,20 @@ export const useUserStore = defineStore('user', () => { // 获取用户信息 async function fetchUserInfo(id) { try { + console.log('获取用户信息:', id); const response = await getUserInfo(id); - userInfo.value = response.data; - return response.data; + console.log('获取用户信息响应:', response); + // 如果响应是包装过的,尝试获取实际数据 + const userData = response.data || response; + userInfo.value = userData; + return userData; } catch (error) { - showToast('获取用户信息失败:' + error.message); + console.error('获取用户信息失败:', error); + if (error.response) { + console.error('错误响应:', error.response); + } + const message = error.response?.data?.message || error.message || '获取用户信息失败,请稍后重试'; + showToast(message); throw error; } } @@ -48,6 +90,7 @@ export const useUserStore = defineStore('user', () => { userId.value = ''; userInfo.value = null; localStorage.removeItem('userId'); + console.log('用户已登出'); } return { diff --git a/frontend/src/utils/request.js b/frontend/src/utils/request.js index 30feb5f..cecafca 100644 --- a/frontend/src/utils/request.js +++ b/frontend/src/utils/request.js @@ -4,15 +4,24 @@ import { showToast } from 'vant'; const request = axios.create({ baseURL: '/api', timeout: 10000, + headers: { + 'Content-Type': 'application/json' + } }); // 请求拦截器 request.interceptors.request.use( (config) => { - // 可以在这里添加 token 等认证信息 + console.log('发送请求:', { + url: config.url, + method: config.method, + data: config.data, + headers: config.headers + }); return config; }, (error) => { + console.error('请求错误:', error); return Promise.reject(error); } ); @@ -20,9 +29,25 @@ request.interceptors.request.use( // 响应拦截器 request.interceptors.response.use( (response) => { + console.log('收到响应:', { + status: response.status, + statusText: response.statusText, + data: response.data, + headers: response.headers + }); return response.data; }, (error) => { + console.error('响应错误:', { + message: error.message, + config: error.config, + response: error.response ? { + status: error.response.status, + statusText: error.response.statusText, + data: error.response.data, + headers: error.response.headers + } : null + }); const message = error.response?.data?.message || '请求失败,请稍后重试'; showToast(message); return Promise.reject(error); diff --git a/frontend/src/views/RegisterView.vue b/frontend/src/views/RegisterView.vue index ad78ec6..c30e569 100644 --- a/frontend/src/views/RegisterView.vue +++ b/frontend/src/views/RegisterView.vue @@ -24,26 +24,6 @@ :rules="[{ required: true, message: '请填写姓名' }]" /> - - - - - { - router.replace('/survey'); + console.log('准备跳转到问卷页面'); + router.push({ + name: 'survey', + replace: true + }).catch(err => { + console.error('路由跳转失败:', err); + showToast('页面跳转失败,请刷新重试'); + }); }, 500); } catch (error) { console.error('注册失败:', error); + showToast(error.message || '注册失败,请重试'); } }