123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 |
- <script>
- //获取ticket
- function getTicket() {
- let url = window.location.href;
- let ticket = url.split("ticket=")[1];
- console.log(ticket, "获取ticket");
- return ticket;
- }
- //获取用户信息
- function get_userInfo() {
- return new Promise((resolve, reject) => {
- let data = {}; //输出的参数
- //设置参数
- console.log(getTicket(), "调用方法ticket");
- let secret = "EC224D5F-B37C-4768-A39E-0F736361815C";
- let appid = "FA176490-C0C2-4BB3-8C06-FBC513C24ECF";
- let ticket = getTicket();
- var params = {
- ticket: ticket,
- appid: appid,
- signature: md5(ticket + appid + secret),
- };
- // 将参数转换为查询字符串
- var queryString = Object.keys(params)
- .map(
- (key) => encodeURIComponent(key) + "=" + encodeURIComponent(params[key])
- )
- .join("&");
- console.log(queryString, "queryString");
- // 创建XMLHttpRequest对象
- var xhr = new XMLHttpRequest();
- // 设置请求的方法和URL
- var method = "GET"; // 或者 'POST'、'PUT'、'DELETE'等
- var url =
- "http://computer.web.ximengnaikang.com/pap/api/open/token?" + queryString; // 替换为您要请求的API的URL
- xhr.open(method, url, true);
- // 监听请求状态变化
- xhr.onreadystatechange = function () {
- if (xhr.readyState === XMLHttpRequest.DONE) {
- // 请求完成
- if (xhr.status === 200) {
- // 请求成功,处理响应数据
- var responseData = xhr.response;
- let data = JSON.parse(responseData);
- localStorage.setItem("access_token", data.access_token);
- localStorage.setItem("username", data.un);
- console.log(data, "用户数据--接口请求成功");
- } else {
- // 请求失败,处理错误
- console.error("请求失败:" + xhr.status + " " + xhr.statusText);
- }
- }
- };
- // 发送请求
- xhr.send();
- resolve(xhr.response);
- });
- }
- // 方法一
- // title:实验名称
- // step:当前步骤
- // total_steps:总步骤
- // total_sorce:实验总分
- // per_sorce:每步分分值
- // start_time: 开始时间,时间戳13位
- // end_time:结束时间,时间戳13位
- function postMethod_1(
- title,
- step,
- total_steps,
- step_sorce,
- per_sorce,
- start_time,
- end_time
- ) {
- return new Promise((resolve, reject) => {
- var access_token = localStorage.getItem("access_token");
- const xhr = new XMLHttpRequest();
- let steps = [];
- let stepsTime = (end_time - start_time) / 1000;
- stepsTime = Math.round(stepsTime);
- //接下来每次调用该方法直接取缓存的步骤数据
- let stepStore = localStorage.getItem("stepsList");
- stepStore = JSON.parse(stepStore);
- console.log(stepStore);
- if (stepStore) {
- //如果含有步骤缓存则直接修改步骤数据
- let index = Number(step - 1);
- console.log(stepStore[index].startTime, "---", start_time);
- stepStore[index].startTime = start_time;
- stepStore[index].endTime = end_time;
- stepStore[index].stepsTime = stepsTime;
- steps = stepStore;
- } else {
- //第一次请求该接口时循环出全部步骤
- for (let i = 0; i < Number(total_steps); i++) {
- const obj = {
- sep: i + 1, //实验步骤序号
- title: `实验步骤${i + 1}`, //步骤名称
- startTime: start_time, //实验每个步骤开始时间
- endTime: end_time, //实验每个步骤结束时间 这里每步设置10秒
- timeUsed: stepsTime, //实验步骤用时:非零整数,单位秒
- expectTime: stepsTime, // 实验步骤合理用时:单位秒
- maxScore: per_sorce, //实验步骤满分:0 ~100,百分制
- score: per_sorce, //实验步骤得分:0 ~100,百分制
- repeatCount: 1, //实验步骤操作次数
- evaluation: " 优 ", //步骤评价:200字以内
- scoringModel: "赋分模型", //赋分模型:200字以内
- done: i < Number(step) ? 1 : 0, //已做未做 1已做 0未做 默认值1
- };
- steps.push(obj);
- }
- }
- let step_index = Number(total_steps) - 1; //获取步骤数组的最后一个索引
- let time_cha =
- (steps[step_index].end_time - steps[step_index].startTime) / 1000; //开始时间结束时间时间差
- time_cha = Math.round(time_cha);
- console.log(steps, "steps");
- //请求接口的全部数据
- const data = {
- username: localStorage.getItem("username"),
- title: title,
- status: steps[step_index].done == 1 ? 1 : 2, //判断最后一步是否完成如果最后一步完成代表全部完成,否则为未完成
- score: step_sorce,
- startTime: steps[step_index].startTime,
- endTime: steps[step_index].end_time,
- timeUsed: time_cha,
- steps,
- };
- //将每次请求的数据存为缓存
- localStorage.setItem("stepsList", JSON.stringify(steps));
- xhr.onreadystatechange = function () {
- if (xhr.readyState === 4) {
- if (xhr.status === 200) {
- console.log(xhr.response, "实验进度接口更新成功", xhr);
- if (step == total_steps) {
- localStorage.removeItem(steps);
- }
- resolve(xhr.responseText);
- } else {
- reject(xhr.statusText);
- }
- }
- };
- xhr.open(
- "POST",
- `http://computer.web.ximengnaikang.com/pap/api/open/dataupload?access_token=${access_token}`,
- true
- );
- xhr.setRequestHeader("Content-Type", "application/json");
- let jsonData = JSON.stringify(data);
- xhr.send(jsonData);
- });
- }
- //方法二
- // 方法二,考试完成时调用此方法,仅在完成时只调用一次
- // title:实验名称
- // status:当前实验是否完成1,完成,2未完成
- // total_steps:总步骤
- // total_sorce:实验总分
- // per_sorce:每步分分值
- // start_time: 开始时间,时间戳13位
- // end_time:结束时间,时间戳13位
- // time_used:实验用时
- // steps:[],实验步骤数组,有几不数组有几个值
- // steps示例
- // const steps=[{
- // seq: 1,//实验步骤序号
- // title: "实验步骤 1",//步骤名称
- // startTime: 1522646936000,//验步骤开始时间:13位时间戳
- // endTime: 1522647936000,//实验步骤结束时间:13位时间戳
- // timeUsed: 900,//实验步骤用时:非零整数,单位秒
- // expectTime: 2,//实验步骤合理用时:单位秒
- // maxScore: 10,//实验步骤满分:0 ~100,百分制
- // score: 10,// 实验步骤得分:0 ~100,百分制
- // repeatCount: 1,//实验步骤操作次数
- // evaluation: " 优 ",//步骤评价
- // scoringModel: "无",//赋分模型
- // remarks: "无",// 备注
- // done: 1,//步骤状态
- // }]
- function postMethod_2(
- title,
- status,
- total_sorce,
- start_time,
- end_time,
- time_used,
- steps
- ) {
- return new Promise((resolve, reject) => {
- var access_token = localStorage.getItem("access_token");
- let apiUrl = "http://computer.web.ximengnaikang.com/pap";
- const xhr = new XMLHttpRequest();
- //请求接口的全部数据
- const data = {
- username: localStorage.getItem("username"),
- title: title,
- status: status, //判断最后一步是否完成如果最后一步完成代表全部完成,否则为未完成
- score: total_sorce,
- startTime: start_time,
- endTime: end_time,
- timeUsed: time_used,
- steps,
- };
- xhr.onreadystatechange = function () {
- if (xhr.readyState === 4) {
- if (xhr.status === 200) {
- console.log();
- resolve(xhr.responseText);
- } else {
- reject(xhr.statusText);
- }
- }
- };
- xhr.open(
- "POST",
- `${apiUrl}/api/open/dataupload?access_token=${access_token}`,
- true
- );
- xhr.setRequestHeader("Content-Type", "application/json");
- let jsonData = JSON.stringify(data);
- xhr.send(jsonData);
- });
- }
- //md5 加密
- function md5(string) {
- function md5_RotateLeft(lValue, iShiftBits) {
- return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));
- }
- function md5_AddUnsigned(lX, lY) {
- var lX4, lY4, lX8, lY8, lResult;
- lX8 = lX & 0x80000000;
- lY8 = lY & 0x80000000;
- lX4 = lX & 0x40000000;
- lY4 = lY & 0x40000000;
- lResult = (lX & 0x3fffffff) + (lY & 0x3fffffff);
- if (lX4 & lY4) {
- return lResult ^ 0x80000000 ^ lX8 ^ lY8;
- }
- if (lX4 | lY4) {
- if (lResult & 0x40000000) {
- return lResult ^ 0xc0000000 ^ lX8 ^ lY8;
- } else {
- return lResult ^ 0x40000000 ^ lX8 ^ lY8;
- }
- } else {
- return lResult ^ lX8 ^ lY8;
- }
- }
- function md5_F(x, y, z) {
- return (x & y) | (~x & z);
- }
- function md5_G(x, y, z) {
- return (x & z) | (y & ~z);
- }
- function md5_H(x, y, z) {
- return x ^ y ^ z;
- }
- function md5_I(x, y, z) {
- return y ^ (x | ~z);
- }
- function md5_FF(a, b, c, d, x, s, ac) {
- a = md5_AddUnsigned(
- a,
- md5_AddUnsigned(md5_AddUnsigned(md5_F(b, c, d), x), ac)
- );
- return md5_AddUnsigned(md5_RotateLeft(a, s), b);
- }
- function md5_GG(a, b, c, d, x, s, ac) {
- a = md5_AddUnsigned(
- a,
- md5_AddUnsigned(md5_AddUnsigned(md5_G(b, c, d), x), ac)
- );
- return md5_AddUnsigned(md5_RotateLeft(a, s), b);
- }
- function md5_HH(a, b, c, d, x, s, ac) {
- a = md5_AddUnsigned(
- a,
- md5_AddUnsigned(md5_AddUnsigned(md5_H(b, c, d), x), ac)
- );
- return md5_AddUnsigned(md5_RotateLeft(a, s), b);
- }
- function md5_II(a, b, c, d, x, s, ac) {
- a = md5_AddUnsigned(
- a,
- md5_AddUnsigned(md5_AddUnsigned(md5_I(b, c, d), x), ac)
- );
- return md5_AddUnsigned(md5_RotateLeft(a, s), b);
- }
- function md5_ConvertToWordArray(string) {
- var lWordCount;
- var lMessageLength = string.length;
- var lNumberOfWords_temp1 = lMessageLength + 8;
- var lNumberOfWords_temp2 =
- (lNumberOfWords_temp1 - (lNumberOfWords_temp1 % 64)) / 64;
- var lNumberOfWords = (lNumberOfWords_temp2 + 1) * 16;
- var lWordArray = Array(lNumberOfWords - 1);
- var lBytePosition = 0;
- var lByteCount = 0;
- while (lByteCount < lMessageLength) {
- lWordCount = (lByteCount - (lByteCount % 4)) / 4;
- lBytePosition = (lByteCount % 4) * 8;
- lWordArray[lWordCount] =
- lWordArray[lWordCount] |
- (string.charCodeAt(lByteCount) << lBytePosition);
- lByteCount++;
- }
- lWordCount = (lByteCount - (lByteCount % 4)) / 4;
- lBytePosition = (lByteCount % 4) * 8;
- lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80 << lBytePosition);
- lWordArray[lNumberOfWords - 2] = lMessageLength << 3;
- lWordArray[lNumberOfWords - 1] = lMessageLength >>> 29;
- return lWordArray;
- }
- function md5_WordToHex(lValue) {
- var WordToHexValue = "",
- WordToHexValue_temp = "",
- lByte,
- lCount;
- for (lCount = 0; lCount <= 3; lCount++) {
- lByte = (lValue >>> (lCount * 8)) & 255;
- WordToHexValue_temp = "0" + lByte.toString(16);
- WordToHexValue =
- WordToHexValue +
- WordToHexValue_temp.substr(WordToHexValue_temp.length - 2, 2);
- }
- return WordToHexValue;
- }
- function md5_Utf8Encode(string) {
- string = string.replace(/\r\n/g, "\n");
- var utftext = "";
- for (var n = 0; n < string.length; n++) {
- var c = string.charCodeAt(n);
- if (c < 128) {
- utftext += String.fromCharCode(c);
- } else if (c > 127 && c < 2048) {
- utftext += String.fromCharCode((c >> 6) | 192);
- utftext += String.fromCharCode((c & 63) | 128);
- } else {
- utftext += String.fromCharCode((c >> 12) | 224);
- utftext += String.fromCharCode(((c >> 6) & 63) | 128);
- utftext += String.fromCharCode((c & 63) | 128);
- }
- }
- return utftext;
- }
- var x = Array();
- var k, AA, BB, CC, DD, a, b, c, d;
- var S11 = 7,
- S12 = 12,
- S13 = 17,
- S14 = 22;
- var S21 = 5,
- S22 = 9,
- S23 = 14,
- S24 = 20;
- var S31 = 4,
- S32 = 11,
- S33 = 16,
- S34 = 23;
- var S41 = 6,
- S42 = 10,
- S43 = 15,
- S44 = 21;
- string = md5_Utf8Encode(string);
- x = md5_ConvertToWordArray(string);
- a = 0x67452301;
- b = 0xefcdab89;
- c = 0x98badcfe;
- d = 0x10325476;
- for (k = 0; k < x.length; k += 16) {
- AA = a;
- BB = b;
- CC = c;
- DD = d;
- a = md5_FF(a, b, c, d, x[k + 0], S11, 0xd76aa478);
- d = md5_FF(d, a, b, c, x[k + 1], S12, 0xe8c7b756);
- c = md5_FF(c, d, a, b, x[k + 2], S13, 0x242070db);
- b = md5_FF(b, c, d, a, x[k + 3], S14, 0xc1bdceee);
- a = md5_FF(a, b, c, d, x[k + 4], S11, 0xf57c0faf);
- d = md5_FF(d, a, b, c, x[k + 5], S12, 0x4787c62a);
- c = md5_FF(c, d, a, b, x[k + 6], S13, 0xa8304613);
- b = md5_FF(b, c, d, a, x[k + 7], S14, 0xfd469501);
- a = md5_FF(a, b, c, d, x[k + 8], S11, 0x698098d8);
- d = md5_FF(d, a, b, c, x[k + 9], S12, 0x8b44f7af);
- c = md5_FF(c, d, a, b, x[k + 10], S13, 0xffff5bb1);
- b = md5_FF(b, c, d, a, x[k + 11], S14, 0x895cd7be);
- a = md5_FF(a, b, c, d, x[k + 12], S11, 0x6b901122);
- d = md5_FF(d, a, b, c, x[k + 13], S12, 0xfd987193);
- c = md5_FF(c, d, a, b, x[k + 14], S13, 0xa679438e);
- b = md5_FF(b, c, d, a, x[k + 15], S14, 0x49b40821);
- a = md5_GG(a, b, c, d, x[k + 1], S21, 0xf61e2562);
- d = md5_GG(d, a, b, c, x[k + 6], S22, 0xc040b340);
- c = md5_GG(c, d, a, b, x[k + 11], S23, 0x265e5a51);
- b = md5_GG(b, c, d, a, x[k + 0], S24, 0xe9b6c7aa);
- a = md5_GG(a, b, c, d, x[k + 5], S21, 0xd62f105d);
- d = md5_GG(d, a, b, c, x[k + 10], S22, 0x2441453);
- c = md5_GG(c, d, a, b, x[k + 15], S23, 0xd8a1e681);
- b = md5_GG(b, c, d, a, x[k + 4], S24, 0xe7d3fbc8);
- a = md5_GG(a, b, c, d, x[k + 9], S21, 0x21e1cde6);
- d = md5_GG(d, a, b, c, x[k + 14], S22, 0xc33707d6);
- c = md5_GG(c, d, a, b, x[k + 3], S23, 0xf4d50d87);
- b = md5_GG(b, c, d, a, x[k + 8], S24, 0x455a14ed);
- a = md5_GG(a, b, c, d, x[k + 13], S21, 0xa9e3e905);
- d = md5_GG(d, a, b, c, x[k + 2], S22, 0xfcefa3f8);
- c = md5_GG(c, d, a, b, x[k + 7], S23, 0x676f02d9);
- b = md5_GG(b, c, d, a, x[k + 12], S24, 0x8d2a4c8a);
- a = md5_HH(a, b, c, d, x[k + 5], S31, 0xfffa3942);
- d = md5_HH(d, a, b, c, x[k + 8], S32, 0x8771f681);
- c = md5_HH(c, d, a, b, x[k + 11], S33, 0x6d9d6122);
- b = md5_HH(b, c, d, a, x[k + 14], S34, 0xfde5380c);
- a = md5_HH(a, b, c, d, x[k + 1], S31, 0xa4beea44);
- d = md5_HH(d, a, b, c, x[k + 4], S32, 0x4bdecfa9);
- c = md5_HH(c, d, a, b, x[k + 7], S33, 0xf6bb4b60);
- b = md5_HH(b, c, d, a, x[k + 10], S34, 0xbebfbc70);
- a = md5_HH(a, b, c, d, x[k + 13], S31, 0x289b7ec6);
- d = md5_HH(d, a, b, c, x[k + 0], S32, 0xeaa127fa);
- c = md5_HH(c, d, a, b, x[k + 3], S33, 0xd4ef3085);
- b = md5_HH(b, c, d, a, x[k + 6], S34, 0x4881d05);
- a = md5_HH(a, b, c, d, x[k + 9], S31, 0xd9d4d039);
- d = md5_HH(d, a, b, c, x[k + 12], S32, 0xe6db99e5);
- c = md5_HH(c, d, a, b, x[k + 15], S33, 0x1fa27cf8);
- b = md5_HH(b, c, d, a, x[k + 2], S34, 0xc4ac5665);
- a = md5_II(a, b, c, d, x[k + 0], S41, 0xf4292244);
- d = md5_II(d, a, b, c, x[k + 7], S42, 0x432aff97);
- c = md5_II(c, d, a, b, x[k + 14], S43, 0xab9423a7);
- b = md5_II(b, c, d, a, x[k + 5], S44, 0xfc93a039);
- a = md5_II(a, b, c, d, x[k + 12], S41, 0x655b59c3);
- d = md5_II(d, a, b, c, x[k + 3], S42, 0x8f0ccc92);
- c = md5_II(c, d, a, b, x[k + 10], S43, 0xffeff47d);
- b = md5_II(b, c, d, a, x[k + 1], S44, 0x85845dd1);
- a = md5_II(a, b, c, d, x[k + 8], S41, 0x6fa87e4f);
- d = md5_II(d, a, b, c, x[k + 15], S42, 0xfe2ce6e0);
- c = md5_II(c, d, a, b, x[k + 6], S43, 0xa3014314);
- b = md5_II(b, c, d, a, x[k + 13], S44, 0x4e0811a1);
- a = md5_II(a, b, c, d, x[k + 4], S41, 0xf7537e82);
- d = md5_II(d, a, b, c, x[k + 11], S42, 0xbd3af235);
- c = md5_II(c, d, a, b, x[k + 2], S43, 0x2ad7d2bb);
- b = md5_II(b, c, d, a, x[k + 9], S44, 0xeb86d391);
- a = md5_AddUnsigned(a, AA);
- b = md5_AddUnsigned(b, BB);
- c = md5_AddUnsigned(c, CC);
- d = md5_AddUnsigned(d, DD);
- }
- return (
- md5_WordToHex(a) +
- md5_WordToHex(b) +
- md5_WordToHex(c) +
- md5_WordToHex(d)
- ).toLowerCase();
- }
- async function pubilcMethod(
- title,
- step,
- total_steps,
- step_sorce,
- per_sorce,
- start_time,
- end_time
- ) {
- let access_token = localStorage.getItem("access_token");
- if (!access_token) {
- await get_userInfo();
- }
- await postMethod_1(
- title,
- step,
- total_steps,
- step_sorce,
- per_sorce,
- start_time,
- end_time
- );
- }
- //调用
- pubilcMethod('虚拟仿真实验', 2, 5, 100, 25, 0, 0, 0)
- </script>
|