script.html 20 KB

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