index_1.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * @FilePath: index_1.js
  3. * @Author: 旭颖
  4. * @Date: 2023-07-20 15:40:41
  5. * @LastEditors: Please set LastEditors
  6. * @LastEditTime: 2023-07-21 10:10:51
  7. */
  8. // 封装 AJAX 请求为类
  9. class AjaxRequest {
  10. constructor() {
  11. this.xhr = new XMLHttpRequest();
  12. }
  13. // 发送 GET 请求
  14. get(url, callback) {
  15. this.xhr.open("GET", url, true);
  16. this.xhr.onload = function () {
  17. if (this.xhr.status === 200) {
  18. callback(null, JSON.parse(this.xhr.responseText));
  19. } else {
  20. callback("Error: " + this.xhr.status);
  21. }
  22. }.bind(this);
  23. this.xhr.onerror = function () {
  24. callback("Network Error");
  25. }.bind(this);
  26. this.xhr.send();
  27. }
  28. // 发送 POST 请求
  29. post(url, data, callback) {
  30. this.xhr.open("POST", url, true);
  31. this.xhr.setRequestHeader("Content-Type", "application/json");
  32. this.xhr.onload = function () {
  33. if (this.xhr.status === 200) {
  34. callback(null, JSON.parse(this.xhr.responseText));
  35. } else {
  36. callback("Error: " + this.xhr.status);
  37. }
  38. }.bind(this);
  39. this.xhr.onerror = function () {
  40. callback("Network Error");
  41. }.bind(this);
  42. this.xhr.send(JSON.stringify(data));
  43. }
  44. }
  45. // 实例化 AJAX 请求类
  46. const ajaxRequest = new AjaxRequest();
  47. const apiUrl = "http://47.103.39.163:8080";
  48. // 对接步骤
  49. // 步骤一:从url中获取ticket
  50. // 步骤二:获取当前用户的个人信息
  51. // 步骤三:调用进度记录方法
  52. function getTicket() {
  53. let url = window.location.href;
  54. // let url =
  55. // "http://computer.web.ximengnaikang.com/?tokenid=2*b30c8537e6b2497aacfab88821d1d19f*1022022800064457_hactcm-xs_0&progressId=bd1799ab-0675-4a94-91d2-e3f9b3a989f9&ticket=c646fff11e55447ea66498568495ddfe";
  56. let ticket = url.split("ticket=")[1];
  57. console.log(ticket, "获取ticket");
  58. return ticket;
  59. }
  60. //获取用户信息
  61. function getUserInfo() {
  62. let data = {}; //输出的参数
  63. //设置参数
  64. console.log(getTicket(), "调用方法ticket");
  65. let secret = "EC224D5F-B37C-4768-A39E-0F736361815C";
  66. let appid = "FA176490-C0C2-4BB3-8C06-FBC513C24ECF";
  67. let ticket = getTicket();
  68. var params = {
  69. ticket: ticket,
  70. appid: appid,
  71. signature: md5(ticket + appid + secret),
  72. };
  73. // 将参数转换为查询字符串
  74. var queryString = Object.keys(params)
  75. .map(
  76. (key) => encodeURIComponent(key) + "=" + encodeURIComponent(params[key])
  77. )
  78. .join("&");
  79. console.log(queryString, "queryString");
  80. ajaxRequest.get(
  81. apiUrl + "/api/open/token?" + queryString,
  82. function (error, responseData) {
  83. if (error) {
  84. console.error("请求失败:" + error);
  85. } else {
  86. data = {
  87. access_token: responseData.access_token,
  88. username: responseData.un,
  89. };
  90. }
  91. }
  92. );
  93. return data;
  94. }
  95. // title:实验名称
  96. // step:当前步骤
  97. // total_steps:总步骤
  98. // total_sorce:实验总分
  99. // per_sorce:每步分分值
  100. // start_time: 开始时间,时间戳13位
  101. // end_time:结束时间,时间戳13位
  102. //方法一
  103. function dataupload(
  104. title,
  105. step,
  106. total_steps,
  107. total_sorce,
  108. per_sorce,
  109. start_time,
  110. end_time
  111. ) {
  112. return new Promise((resolve, reject) => {
  113. let user_info = getUserInfo();
  114. console.log(user_info, "获取到的用户信息");
  115. let steps = [];
  116. let stepsTime = (end_time - start_time) / 1000;
  117. stepsTime = Math.round(stepsTime);
  118. //接下来每次调用该方法直接取缓存的步骤数据
  119. let stepStore = localStorage.getItem("stepsList");
  120. stepStore = JSON.parse(stepStore);
  121. console.log(stepStore);
  122. if (stepStore) {
  123. //如果含有步骤缓存则直接修改步骤数据
  124. let index = Number(step - 1);
  125. stepStore[index].startTime = start_time;
  126. stepStore[index].endTime = end_time;
  127. stepStore[index].stepsTime = stepsTime;
  128. steps = stepStore;
  129. } else {
  130. //第一次请求该接口时循环出全部步骤
  131. for (let i = 0; i < Number(total_steps); i++) {
  132. const obj = {
  133. sep: i + 1, //实验步骤序号
  134. title: `实验步骤${i + 1}`, //步骤名称
  135. startTime: start_time, //实验每个步骤开始时间
  136. endTime: end_time, //实验每个步骤结束时间 这里每步设置10秒
  137. timeUsed: stepsTime, //实验步骤用时:非零整数,单位秒
  138. expectTime: stepsTime, // 实验步骤合理用时:单位秒
  139. maxScore: per_sorce, //实验步骤满分:0 ~100,百分制
  140. score: per_sorce, //实验步骤得分:0 ~100,百分制
  141. repeatCount: 1, //实验步骤操作次数
  142. evaluation: " 优 ", //步骤评价:200字以内
  143. scoringModel: "赋分模型", //赋分模型:200字以内
  144. done: i < Number(total_steps) ? 1 : 0, //已做未做 1已做 0未做 默认值1
  145. };
  146. steps.push(obj);
  147. }
  148. }
  149. let step_index = Number(total_steps) - 1; //获取步骤数组的最后一个索引
  150. let time_cha =
  151. (steps[step_index].end_time - steps[step_index].startTime) / 1000; //开始时间结束时间时间差
  152. time_cha = Math.round(time_cha);
  153. console.log(steps, "steps");
  154. //请求接口的全部数据
  155. const data = {
  156. username: user_info.username,
  157. title: title,
  158. status: steps[step_index].done == 1 ? 1 : 2, //判断最后一步是否完成如果最后一步完成代表全部完成,否则为未完成
  159. score: total_sorce,
  160. startTime: steps[step_index].startTime,
  161. endTime: steps[step_index].end_time,
  162. timeUsed: time_cha,
  163. steps,
  164. };
  165. //将每次请求的数据存为缓存
  166. localStorage.setItem("stepsList", JSON.stringify(steps));
  167. ajaxRequest.post(
  168. apiUrl + "/api/open/dataupload?access_token=" + user_info.access_token,
  169. data,
  170. function (error, responseData) {
  171. if (error) {
  172. console.error(error);
  173. } else {
  174. console.log(responseData, "发送数据请求成功");
  175. }
  176. }
  177. );
  178. });
  179. }
  180. // 方法二,考试完成时调用此方法,仅在完成时只调用一次
  181. // title:实验名称
  182. // status:当前实验是否完成1,完成,2未完成
  183. // total_steps:总步骤
  184. // total_sorce:实验总分
  185. // per_sorce:每步分分值
  186. // start_time: 开始时间,时间戳13位
  187. // end_time:结束时间,时间戳13位
  188. // time_used:实验用时
  189. // steps:[],实验步骤数组,有几不数组有几个值
  190. // steps示例
  191. // const steps=[{
  192. // seq: 1,//实验步骤序号
  193. // title: "实验步骤 1",//步骤名称
  194. // startTime: 1522646936000,//验步骤开始时间:13位时间戳
  195. // endTime: 1522647936000,//实验步骤结束时间:13位时间戳
  196. // timeUsed: 900,//实验步骤用时:非零整数,单位秒
  197. // expectTime: 2,//实验步骤合理用时:单位秒
  198. // maxScore: 10,//实验步骤满分:0 ~100,百分制
  199. // score: 10,// 实验步骤得分:0 ~100,百分制
  200. // repeatCount: 1,//实验步骤操作次数
  201. // evaluation: " 优 ",//步骤评价
  202. // scoringModel: "无",//赋分模型
  203. // remarks: "无",// 备注
  204. // done: 1,//步骤状态
  205. // }]
  206. function sendResult(
  207. title,
  208. status,
  209. total_sorce,
  210. start_time,
  211. end_time,
  212. time_used,
  213. steps
  214. ) {
  215. return new Promise((resolve, reject) => {
  216. let user_info = getUserInfo();
  217. console.log(user_info, "获取到的用户信息");
  218. const xhr = new XMLHttpRequest();
  219. //请求接口的全部数据
  220. const data = {
  221. username: user_info.username,
  222. title: title,
  223. status: status, //判断最后一步是否完成如果最后一步完成代表全部完成,否则为未完成
  224. score: total_sorce,
  225. startTime: start_time,
  226. endTime: end_time,
  227. timeUsed: time_used,
  228. steps,
  229. };
  230. ajaxRequest.post(
  231. apiUrl + "/api/open/dataupload?access_token=" + user_info.access_token,
  232. data,
  233. function (error, responseData) {
  234. if (error) {
  235. console.error(error);
  236. } else {
  237. console.log(responseData, "发送数据请求成功");
  238. }
  239. }
  240. );
  241. });
  242. }
  243. // MD5加密方法
  244. function md5(string) {
  245. function md5_RotateLeft(lValue, iShiftBits) {
  246. return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));
  247. }
  248. function md5_AddUnsigned(lX, lY) {
  249. var lX4, lY4, lX8, lY8, lResult;
  250. lX8 = lX & 0x80000000;
  251. lY8 = lY & 0x80000000;
  252. lX4 = lX & 0x40000000;
  253. lY4 = lY & 0x40000000;
  254. lResult = (lX & 0x3fffffff) + (lY & 0x3fffffff);
  255. if (lX4 & lY4) {
  256. return lResult ^ 0x80000000 ^ lX8 ^ lY8;
  257. }
  258. if (lX4 | lY4) {
  259. if (lResult & 0x40000000) {
  260. return lResult ^ 0xc0000000 ^ lX8 ^ lY8;
  261. } else {
  262. return lResult ^ 0x40000000 ^ lX8 ^ lY8;
  263. }
  264. } else {
  265. return lResult ^ lX8 ^ lY8;
  266. }
  267. }
  268. function md5_F(x, y, z) {
  269. return (x & y) | (~x & z);
  270. }
  271. function md5_G(x, y, z) {
  272. return (x & z) | (y & ~z);
  273. }
  274. function md5_H(x, y, z) {
  275. return x ^ y ^ z;
  276. }
  277. function md5_I(x, y, z) {
  278. return y ^ (x | ~z);
  279. }
  280. function md5_FF(a, b, c, d, x, s, ac) {
  281. a = md5_AddUnsigned(
  282. a,
  283. md5_AddUnsigned(md5_AddUnsigned(md5_F(b, c, d), x), ac)
  284. );
  285. return md5_AddUnsigned(md5_RotateLeft(a, s), b);
  286. }
  287. function md5_GG(a, b, c, d, x, s, ac) {
  288. a = md5_AddUnsigned(
  289. a,
  290. md5_AddUnsigned(md5_AddUnsigned(md5_G(b, c, d), x), ac)
  291. );
  292. return md5_AddUnsigned(md5_RotateLeft(a, s), b);
  293. }
  294. function md5_HH(a, b, c, d, x, s, ac) {
  295. a = md5_AddUnsigned(
  296. a,
  297. md5_AddUnsigned(md5_AddUnsigned(md5_H(b, c, d), x), ac)
  298. );
  299. return md5_AddUnsigned(md5_RotateLeft(a, s), b);
  300. }
  301. function md5_II(a, b, c, d, x, s, ac) {
  302. a = md5_AddUnsigned(
  303. a,
  304. md5_AddUnsigned(md5_AddUnsigned(md5_I(b, c, d), x), ac)
  305. );
  306. return md5_AddUnsigned(md5_RotateLeft(a, s), b);
  307. }
  308. function md5_ConvertToWordArray(string) {
  309. var lWordCount;
  310. var lMessageLength = string.length;
  311. var lNumberOfWords_temp1 = lMessageLength + 8;
  312. var lNumberOfWords_temp2 =
  313. (lNumberOfWords_temp1 - (lNumberOfWords_temp1 % 64)) / 64;
  314. var lNumberOfWords = (lNumberOfWords_temp2 + 1) * 16;
  315. var lWordArray = Array(lNumberOfWords - 1);
  316. var lBytePosition = 0;
  317. var lByteCount = 0;
  318. while (lByteCount < lMessageLength) {
  319. lWordCount = (lByteCount - (lByteCount % 4)) / 4;
  320. lBytePosition = (lByteCount % 4) * 8;
  321. lWordArray[lWordCount] =
  322. lWordArray[lWordCount] |
  323. (string.charCodeAt(lByteCount) << lBytePosition);
  324. lByteCount++;
  325. }
  326. lWordCount = (lByteCount - (lByteCount % 4)) / 4;
  327. lBytePosition = (lByteCount % 4) * 8;
  328. lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80 << lBytePosition);
  329. lWordArray[lNumberOfWords - 2] = lMessageLength << 3;
  330. lWordArray[lNumberOfWords - 1] = lMessageLength >>> 29;
  331. return lWordArray;
  332. }
  333. function md5_WordToHex(lValue) {
  334. var WordToHexValue = "",
  335. WordToHexValue_temp = "",
  336. lByte,
  337. lCount;
  338. for (lCount = 0; lCount <= 3; lCount++) {
  339. lByte = (lValue >>> (lCount * 8)) & 255;
  340. WordToHexValue_temp = "0" + lByte.toString(16);
  341. WordToHexValue =
  342. WordToHexValue +
  343. WordToHexValue_temp.substr(WordToHexValue_temp.length - 2, 2);
  344. }
  345. return WordToHexValue;
  346. }
  347. function md5_Utf8Encode(string) {
  348. string = string.replace(/\r\n/g, "\n");
  349. var utftext = "";
  350. for (var n = 0; n < string.length; n++) {
  351. var c = string.charCodeAt(n);
  352. if (c < 128) {
  353. utftext += String.fromCharCode(c);
  354. } else if (c > 127 && c < 2048) {
  355. utftext += String.fromCharCode((c >> 6) | 192);
  356. utftext += String.fromCharCode((c & 63) | 128);
  357. } else {
  358. utftext += String.fromCharCode((c >> 12) | 224);
  359. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  360. utftext += String.fromCharCode((c & 63) | 128);
  361. }
  362. }
  363. return utftext;
  364. }
  365. var x = Array();
  366. var k, AA, BB, CC, DD, a, b, c, d;
  367. var S11 = 7,
  368. S12 = 12,
  369. S13 = 17,
  370. S14 = 22;
  371. var S21 = 5,
  372. S22 = 9,
  373. S23 = 14,
  374. S24 = 20;
  375. var S31 = 4,
  376. S32 = 11,
  377. S33 = 16,
  378. S34 = 23;
  379. var S41 = 6,
  380. S42 = 10,
  381. S43 = 15,
  382. S44 = 21;
  383. string = md5_Utf8Encode(string);
  384. x = md5_ConvertToWordArray(string);
  385. a = 0x67452301;
  386. b = 0xefcdab89;
  387. c = 0x98badcfe;
  388. d = 0x10325476;
  389. for (k = 0; k < x.length; k += 16) {
  390. AA = a;
  391. BB = b;
  392. CC = c;
  393. DD = d;
  394. a = md5_FF(a, b, c, d, x[k + 0], S11, 0xd76aa478);
  395. d = md5_FF(d, a, b, c, x[k + 1], S12, 0xe8c7b756);
  396. c = md5_FF(c, d, a, b, x[k + 2], S13, 0x242070db);
  397. b = md5_FF(b, c, d, a, x[k + 3], S14, 0xc1bdceee);
  398. a = md5_FF(a, b, c, d, x[k + 4], S11, 0xf57c0faf);
  399. d = md5_FF(d, a, b, c, x[k + 5], S12, 0x4787c62a);
  400. c = md5_FF(c, d, a, b, x[k + 6], S13, 0xa8304613);
  401. b = md5_FF(b, c, d, a, x[k + 7], S14, 0xfd469501);
  402. a = md5_FF(a, b, c, d, x[k + 8], S11, 0x698098d8);
  403. d = md5_FF(d, a, b, c, x[k + 9], S12, 0x8b44f7af);
  404. c = md5_FF(c, d, a, b, x[k + 10], S13, 0xffff5bb1);
  405. b = md5_FF(b, c, d, a, x[k + 11], S14, 0x895cd7be);
  406. a = md5_FF(a, b, c, d, x[k + 12], S11, 0x6b901122);
  407. d = md5_FF(d, a, b, c, x[k + 13], S12, 0xfd987193);
  408. c = md5_FF(c, d, a, b, x[k + 14], S13, 0xa679438e);
  409. b = md5_FF(b, c, d, a, x[k + 15], S14, 0x49b40821);
  410. a = md5_GG(a, b, c, d, x[k + 1], S21, 0xf61e2562);
  411. d = md5_GG(d, a, b, c, x[k + 6], S22, 0xc040b340);
  412. c = md5_GG(c, d, a, b, x[k + 11], S23, 0x265e5a51);
  413. b = md5_GG(b, c, d, a, x[k + 0], S24, 0xe9b6c7aa);
  414. a = md5_GG(a, b, c, d, x[k + 5], S21, 0xd62f105d);
  415. d = md5_GG(d, a, b, c, x[k + 10], S22, 0x2441453);
  416. c = md5_GG(c, d, a, b, x[k + 15], S23, 0xd8a1e681);
  417. b = md5_GG(b, c, d, a, x[k + 4], S24, 0xe7d3fbc8);
  418. a = md5_GG(a, b, c, d, x[k + 9], S21, 0x21e1cde6);
  419. d = md5_GG(d, a, b, c, x[k + 14], S22, 0xc33707d6);
  420. c = md5_GG(c, d, a, b, x[k + 3], S23, 0xf4d50d87);
  421. b = md5_GG(b, c, d, a, x[k + 8], S24, 0x455a14ed);
  422. a = md5_GG(a, b, c, d, x[k + 13], S21, 0xa9e3e905);
  423. d = md5_GG(d, a, b, c, x[k + 2], S22, 0xfcefa3f8);
  424. c = md5_GG(c, d, a, b, x[k + 7], S23, 0x676f02d9);
  425. b = md5_GG(b, c, d, a, x[k + 12], S24, 0x8d2a4c8a);
  426. a = md5_HH(a, b, c, d, x[k + 5], S31, 0xfffa3942);
  427. d = md5_HH(d, a, b, c, x[k + 8], S32, 0x8771f681);
  428. c = md5_HH(c, d, a, b, x[k + 11], S33, 0x6d9d6122);
  429. b = md5_HH(b, c, d, a, x[k + 14], S34, 0xfde5380c);
  430. a = md5_HH(a, b, c, d, x[k + 1], S31, 0xa4beea44);
  431. d = md5_HH(d, a, b, c, x[k + 4], S32, 0x4bdecfa9);
  432. c = md5_HH(c, d, a, b, x[k + 7], S33, 0xf6bb4b60);
  433. b = md5_HH(b, c, d, a, x[k + 10], S34, 0xbebfbc70);
  434. a = md5_HH(a, b, c, d, x[k + 13], S31, 0x289b7ec6);
  435. d = md5_HH(d, a, b, c, x[k + 0], S32, 0xeaa127fa);
  436. c = md5_HH(c, d, a, b, x[k + 3], S33, 0xd4ef3085);
  437. b = md5_HH(b, c, d, a, x[k + 6], S34, 0x4881d05);
  438. a = md5_HH(a, b, c, d, x[k + 9], S31, 0xd9d4d039);
  439. d = md5_HH(d, a, b, c, x[k + 12], S32, 0xe6db99e5);
  440. c = md5_HH(c, d, a, b, x[k + 15], S33, 0x1fa27cf8);
  441. b = md5_HH(b, c, d, a, x[k + 2], S34, 0xc4ac5665);
  442. a = md5_II(a, b, c, d, x[k + 0], S41, 0xf4292244);
  443. d = md5_II(d, a, b, c, x[k + 7], S42, 0x432aff97);
  444. c = md5_II(c, d, a, b, x[k + 14], S43, 0xab9423a7);
  445. b = md5_II(b, c, d, a, x[k + 5], S44, 0xfc93a039);
  446. a = md5_II(a, b, c, d, x[k + 12], S41, 0x655b59c3);
  447. d = md5_II(d, a, b, c, x[k + 3], S42, 0x8f0ccc92);
  448. c = md5_II(c, d, a, b, x[k + 10], S43, 0xffeff47d);
  449. b = md5_II(b, c, d, a, x[k + 1], S44, 0x85845dd1);
  450. a = md5_II(a, b, c, d, x[k + 8], S41, 0x6fa87e4f);
  451. d = md5_II(d, a, b, c, x[k + 15], S42, 0xfe2ce6e0);
  452. c = md5_II(c, d, a, b, x[k + 6], S43, 0xa3014314);
  453. b = md5_II(b, c, d, a, x[k + 13], S44, 0x4e0811a1);
  454. a = md5_II(a, b, c, d, x[k + 4], S41, 0xf7537e82);
  455. d = md5_II(d, a, b, c, x[k + 11], S42, 0xbd3af235);
  456. c = md5_II(c, d, a, b, x[k + 2], S43, 0x2ad7d2bb);
  457. b = md5_II(b, c, d, a, x[k + 9], S44, 0xeb86d391);
  458. a = md5_AddUnsigned(a, AA);
  459. b = md5_AddUnsigned(b, BB);
  460. c = md5_AddUnsigned(c, CC);
  461. d = md5_AddUnsigned(d, DD);
  462. }
  463. return (
  464. md5_WordToHex(a) +
  465. md5_WordToHex(b) +
  466. md5_WordToHex(c) +
  467. md5_WordToHex(d)
  468. ).toLowerCase();
  469. }