MHT_test.html 18.5 KB
Newer Older
Ford committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MHT测量页面</title>
    <style>
        #questionSection {
            display: none; /* Initially hide question section */
        }
    </style>
</head>
<body>
<!-- 用户个人信息部分 -->
<div id="userInfoSection">
    <form id="userInfoForm" onsubmit="submitUserInfo(event)">
        <!-- app_id 和 app_name 是隐藏的,不在页面上显示,但请求中需要 -->
       <!-- <input type="hidden" id="app_id" name="app_id" value="YOUR_APP_ID" required>
        <input type="hidden" id="app_name" name="app_name" value="YOUR_APP_NAME" required>-->
        <div>
            <label for="app_id">App ID:</label>
            <input type="text" id="app_id" name="app_id" required>
        </div>
        <div>
            <label for="app_name">App Name:</label>
            <input type="text" id="app_name" name="app_name" required>
        </div>
        <div>
            <label for="userName">姓名:</label>
            <input type="text" id="userName" name="userName" required>
        </div>
        <div>
            <label for="sex">性别:</label>
            <input type="text" id="sex" name="sex" required>
        </div>
  <!--      <div>
            <label for="phone">手机号:</label>
            <input type="text" id="phone" name="phone" required>
        </div>-->
        <div>
            <label for="birthday">生日:</label>
            <input type="date" id="birthday" name="birthday" required>
        </div>
        <div>
            <label for="height">身高(cm):</label>
            <input type="text" id="height" name="height" required>
        </div>
        <div>
            <label for="weight">体重(kg):</label>
            <input type="text" id="weight" name="weight" required>
        </div>
        <div>
            <label for="stuNo">学号:</label>
            <input type="text" id="stuNo" name="stuNo" required>
        </div>
        <div>
            <label for="SchoolName">学校:</label>
            <input type="text" id="SchoolName" name="SchoolName" required>
        </div>
        <div>
            <label for="IsUpdate">更新信息:</label>
            <select id="IsUpdate" name="IsUpdate" required>
                <option value="true"></option>
                <option value="false"></option>
            </select>
        </div>
        <button type="submit">提交个人信息</button>
    </form>
</div>


<!-- MHT问卷部分 -->
<div id="questionSection">
    <h2 id="question"></h2>
    <button onclick="choose('yes')"></button>
    <button onclick="choose('no')"></button>
    <button id="submitBtn" onclick="submitAnswersAndScores()">提交问卷</button> <!-- 提交按钮 -->
</div>

<script>
    function login() {
        // 用户名和密码设置为 "admin"
        let username = 'admin';
        let password = 'admin';

        // 创建 URLSearchParams 对象并添加参数
        let params = new URLSearchParams();
        params.append('username', username);
        params.append('password', password);

        // 设置请求头
        let headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        };

        // 发送 POST 请求
        fetch('https://localhost:8089/login', {
            method: 'POST',
            headers: headers,
            body: params.toString()  // 将 URLSearchParams 对象转换为字符串
        })
            .then(response => {
                if (response.ok) {
                    return response.json();  // 如果响应状态码为 2xx,解析响应体为 JSON
                } else {
                    throw new Error('Network response was not ok');
                }
            })
            .then(data => {
                console.log('登录成功:', data);  // 处理响应数据
            })
            .catch(error => {
                console.error('登录失败:', error);  // 处理错误
            });
    }
    console.log("========== login(); ==========")
    // 调用 login 函数
    login();

    // 题目数组
    let questions = [
        "你晚上要睡觉时,是否总想着明天的功课?",
        "老师向全班提问时,你是否会觉得是在问自己而感到不安?",
        "你是否一听说“要考试”心里就紧张。",
        "你考试成绩不好时,心里是否感到很不快?",
        "你学习成绩不好时,是否总是提心吊胆?",
        "你考试时,想不起原先掌握的知识时,是否会感到紧张不安?",
        "你考试后,在没有知道成绩之前,是否总是放心不下?",
        "你是否一遇到考试,就担心会考坏?",
        "你是否希望每次考试都能顺利?",
        "你在没有完成任务之前,是否总担心完不成任务?",
        "你当着大家面朗读课文时,是否总是怕读错?",
        "你是否认为学校里得到的学习成绩总是不大可靠?",
        "你是否认为你比别人更担心学习?",
        "你是否做过考试考坏了的梦?",
        "你是否做过学习成绩不好时,受到爸爸妈妈或老师训斥的梦?",
        "你是否经常觉得有同学在背后说你的坏话?",
        "你受到父母批评后,是否总是想不开,放在心上?",
        "你在游戏或与别人的竞争中输给了对方,是否就不想再干了?",
        "人家在背后议论你,你是否感到讨厌?",
        "你在大家面前或被老师提问时,是否会脸红?",
        "你是否很担心叫你担任班级工作?",
        "你是否总是觉得好像有人在注意你?",
        "你在工作或学习时,如果有人在注意你,你心里是否会紧张?",
        "你受到批评时,心情是否不愉快?",
        "你受到老师批评时,心里是否总是不安?",
        "同学们在笑时,你是否也不大会笑?",
        "你是否觉得到同学家里去玩时不如在自己家里玩?",
        "你和大家在一起时,是否也觉得自己是孤单的一个人?",
        "你是否觉得和同学一起玩,不如自己—个人玩?",
        "同学们在交谈时,你是否不想加入?",
        "你和大家在—起时,是否觉得自己是多余的人?",
        "你是否讨厌参加运动会和文艺演出?",
        "你的朋友是否很少?",
        "你是否不喜欢同别人谈话?",
        "在人多的地方,你是否觉得很怕?",
        "你在参加排球、篮球等集体比赛输了时,心里是否一直 认为自己没做好?",
        "你受到批评后,是否总认为是自己不好?",
        "别人笑你的时候,你是否会认为是自己做错了什么事?",
        "你学习成绩不好时,是否总是认为是自己不用功的缘故?",
        "你做事失败的时候,是否总是认为是自己的责任?",
        "大家受到责备时,你是否认为主要是自己的过错?",
        "你参加乒乓球、羽毛球、广播操等体育比赛时, 是否一出错就特别留神?",
        "碰到为难的事情时,你是否认为自己难以应付?",
        "你是否有时会后悔:“那件事不做就好了”?",
        "你和同学吵架以后,是否总是认为是自己的错?",
        "你心里是否总想为班级做点好事?",
        "你学习的时候,思想是否经常开小差?",
        "你把东西借给别人时,是否担心别人会把东西弄坏?",
        "碰到不顺利的事情时,你心里是否很烦躁?",
        "你是否非常担心家里有人生病或死去?",
        "你是否在梦里见到过死去的人?",
        "你对收音机和汽车的声音是否特别敏感?",
        "你心里是否总觉得好像有什么事没有做好?",
        "你是否总担心会发生什么意外的事?",
        "你在决定要做什么事时,是否总是犹豫不决?",
        "你手上是否经常出汗?",
        "你害羞时是否会脸红?",
        "你是否经常头痛?",
        "你被老师提问时,心里是否总是很紧张?",
        "你没有参加运动,心脏是否经常扑腾扑腾地跳?",
        "你是否很容易疲劳?",
        "你是否很不愿吃药?",
        "夜里你是否很难入睡?",
        "你是否总觉得身体好像有什么毛病?",
        "你是否经常认为自己的体型和面孔比别人难看?",
        "你是否经常觉得肠胃不好?",
        "你是否经常咬指甲?",
        "你是否经常舔手指头?",
        "你是否经常感到呼吸困难?",
        "你去厕所的次数是否比别人多?",
        "你是否很怕到高的地方去?",
        "你是否害怕很多东西?",
        "你是否经常做噩梦?",
        "你胆子是否很小?",
        "夜里,你是否很怕一个人在房间里睡觉?",
        "你乘车穿过隧道或路过高桥时,是否很怕?",
        "你是否喜欢整夜开着灯睡觉?",
        "你听到打雷声是否非常害怕?",
        "你是否非常害怕黑暗?",
        "你是否经常感到后面有人跟着你?",
        "你是否经常生气?",
        "你是否不想得到好的成绩?",
        "你是否经常会突然想哭?",
        "你以前是否说过谎话?",
        "你有时是否会觉得,还是死了好?",
        "你是否一次也没有失约过?",
        "你是否经常想大声喊叫?",
        "你是否能保密别人不让说的事?",
        "你有时是否想过自己一个人到远的地方去?",
        "你是否总是很有礼貌?",
        "你被人说了坏话,是否想立即采取报复行动?",
        "老师或父母说的话,你是否都照办?",
        "你心里不开心,是否会乱丢、乱砸东西?",
        "你是否发过怒?",
        "你想要的东西,是否就—定要拿到手?",
        "你不喜欢的功课老师提前下课,你是否会感到特别高兴?",
        "你是否经常想从高的地方跳下来?",
        "你是否无论对谁都很亲热?",
        "你是否会经常急躁得坐立不安?",
        "对不认识的人,你是否会都喜欢?"
    ];// 填入题目
    // 存储用户答案的数组,yes对应1,no对应0
    var answers = [];
    var currentQuestion = 0;
    var scores = { "学习焦虑": 0, "对人焦虑": 0, "孤独倾向": 0, "自责倾向": 0, "过敏倾向": 0, "身体症状": 0, "恐怖倾向": 0, "冲动倾向": 0 };
    var scoring = {
        "学习焦虑": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
        "对人焦虑": [16, 17, 18, 19, 20, 21, 22, 23, 24, 25],
        "孤独倾向": [26, 27, 28, 29, 30, 31, 32, 33, 34, 35],
        "自责倾向": [36, 37, 38, 39, 40, 41, 42, 43, 44, 45],
        "过敏倾向": [46, 47, 48, 49, 50, 51, 52, 53, 54, 55],
        "身体症状": [56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70],
        "恐怖倾向": [71, 72, 73, 74, 75, 76, 77, 78, 79, 80],
        "冲动倾向": [81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
    };
    // 初始化显示第一个问题
    displayQuestion();

    function displayQuestion() {
        document.getElementById("question").innerText = questions[currentQuestion];
        document.getElementById("submitBtn").style.display = 'none'; // Hide the submit button
    }

    function choose(answer) {
        // 根据答案将1或0保存到answers数组中
        answers[currentQuestion] = answer === 'yes' ? 1 : 0;

        // 更新分数,这里仅当答案为'yes'时增加分数
        for (var category in scoring) {
            if (scoring[category].includes(currentQuestion + 1)) {
                scores[category] += answers[currentQuestion];
            }
        }

        currentQuestion++;
        if (currentQuestion < questions.length) {
            displayQuestion();
        } else {
            // 若是最后一道题,显示提交按钮
            document.getElementById("submitBtn").style.display = 'block';
        }
    }


    //提交个人信息部分
    function submitUserInfo(event) {
        event.preventDefault(); // Prevent default form submission behavior
        let userInfoForm = document.getElementById('userInfoForm');
        let userInfo = new FormData(userInfoForm);

        // 请求URL改成用户信息录入的接口
        let submitUrl = 'https://localhost:8089/MentalClinic/quickMHTMentalClinic';

        // 使用 AJAX 方式发送用户信息数据
        fetch(submitUrl, {
            method: 'POST',
            body: userInfo
        })
            .then(response => response.json())
            .then(responseData => {
                // 根据响应处理结果
                if (responseData.code === 1) {
                    // 存储返回的UUID和IDs
                    MHT_UUID = responseData.MHT_UUID;
                    console.log("MHT_UUID: ",MHT_UUID)
                    UserId = responseData.UserId;
                    AppId = responseData.appId;
                    console.log("UserId: ",UserId)
                    console.log("AppId: ",AppId)

                    // 隐藏个人信息的div
                    document.getElementById('userInfoSection').style.display = 'none';

                    // 显示问题部分
                    document.getElementById('questionSection').style.display = 'block';
                    displayQuestion(); // 显示第一个问题
                } else {
                    alert("用户信息提交失败:" + responseData.message);
                }
            })
            .catch((error) => {
                console.error('Error:', error);
            });
    }


    //提交答案和评分
  /*  function submitAnswersAndScores() {
        //收集答案评分
        console.log("提交答案和评分 MHT_UUID: ",MHT_UUID)
        let answerData = {
            // 通过上文函数保存的UUID
            MHT_UUID: MHT_UUID,
            AnswerArr: answers.join(','),
            // 各维度的得分
            LearningAnxiety: scores["学习焦虑"].toString(),
            AnxietyPeople: scores["对人焦虑"].toString(),
            LonelinessTendency: scores["孤独倾向"].toString(),
            SelfBlameTendency: scores["自责倾向"].toString(),
            AllergicTendency: scores["过敏倾向"].toString(),
            PhysicalSign: scores["身体症状"].toString(),
            PhobicTendency: scores["恐怖倾向"].toString(),
            ImpulsiveTendency: scores["冲动倾向"].toString()
        };
        // 定义后端,提交问卷结果的接口
        let submitUrl = 'https://localhost:8089/MentalClinic/evaluateMHTMentalClinic';

        // 使用 AJAX 方式发送问卷结果
        fetch(submitUrl, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(answerData)
        })
            .then(response => response.json())
            .then(responseData => {
                if (responseData.code === 1) {
                    // 进行聊天连接等后续操作
                    console.log("问卷评分数据提交成功: ", responseData);
                    // 连接聊天WebSocket
                    let chatUrl = `wss://localhost:8089/chat/ws?id=${UserId}&DpId=25&app_id=${AppId}&uuid=${MHT_UUID}`;
                    openWebSocket(chatUrl);
                } else {
                    alert("问卷评分数据提交失败:" + responseData.message);
                }
            })
            .catch((error) => {
                console.error('Error:', error);
            });
    }*/

    function submitAnswersAndScores() {
        // 创建 FormData 对象收集答案和评分
        let formData = new FormData();
        formData.append('MHT_UUID', MHT_UUID);
        formData.append('AnswerArr', answers.join(','));
        formData.append('LearningAnxiety', scores["学习焦虑"]);
        formData.append('AnxietyPeople', scores["对人焦虑"]);
        formData.append('LonelinessTendency', scores["孤独倾向"]);
        formData.append('SelfBlameTendency', scores["自责倾向"]);
        formData.append('AllergicTendency', scores["过敏倾向"]);
        formData.append('PhysicalSign', scores["身体症状"]);
        formData.append('PhobicTendency', scores["恐怖倾向"]);
        formData.append('ImpulsiveTendency', scores["冲动倾向"]);

        // 定义后端接口 URL
        let submitUrl = 'https://localhost:8089/MentalClinic/evaluateMHTMentalClinic';

        // 使用 AJAX 方式发送问卷结果
        // 注意:使用 FormData 时,不需要设置 Content-Type 头,浏览器会自动设置
        fetch(submitUrl, {
            method: 'POST',
            body: formData
        })
            .then(response => response.json())
            .then(responseData => {
                if (responseData.code === 1) {
                    // 问卷评分数据提交成功
                    console.log("问卷评分数据提交成功: ", responseData);
                    // 连接聊天WebSocket
                    let chatUrl = `wss://localhost:8089/chat/ws?id=${UserId}&DpId=25&app_id=${AppId}&uuid=${MHT_UUID}`;
                    openWebSocket(chatUrl);
                } else {
                    // 问卷评分数据提交失败
                    alert("问卷评分数据提交失败:" + responseData.message);
                }
            })
            .catch((error) => {
                console.error('Error:', error);
            });
    }

    function openWebSocket(url) {
        // 连接 WebSocket 的逻辑
        let ws = new WebSocket(url);
        ws.onopen = function (event) {
            console.log("Connected to WebSocket");

            // 构建要发送的消息对象
            let message = {
                type: 0,
                content: "你知道我的名字,身高和体重,以及我的测量结果怎么样?"
            };

            // 发送消息到服务器
            ws.send(JSON.stringify(message));
        };

        // 其他事件处理,例如接收消息
        ws.onmessage = function (event) {
            console.log("Received message: " + event.data);
        };

        // 错误处理
        ws.onerror = function (event) {
            console.error("WebSocket error observed:", event);
        };

        // 连接关闭处理
        ws.onclose = function (event) {
            if (event.wasClean) {
                console.log(`Closed cleanly, code=${event.code}, reason=${event.reason}`);
            } else {
                // 例如,由服务器进程杀死或因网络问题无法到达等
                console.log('WebSocket connection closed unexpectedly');
            }
        };
    }

</script>
</body>
</html>