index.js 16 KB

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