reportGM.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <template>
  2. <div class="r_box">
  3. <div class="r_cbox">
  4. <span>学生姓名</span>
  5. <el-select
  6. multiple
  7. collapse-tags
  8. class="r_select"
  9. v-model="uname"
  10. placeholder="全部"
  11. filterable
  12. >
  13. <el-option
  14. v-for="item in userAarray"
  15. :key="item.userid"
  16. :label="item.username"
  17. :value="item.userid"
  18. >
  19. </el-option>
  20. </el-select>
  21. </div>
  22. <div class="r_cbox">
  23. <span>选择阶段</span>
  24. <el-select
  25. class="r_select"
  26. v-model="chap"
  27. multiple
  28. collapse-tags
  29. placeholder="全部"
  30. @change="changeChap"
  31. >
  32. <el-option
  33. v-for="(item, index) in dyList"
  34. :key="index"
  35. :label="item.name"
  36. :value="item.id"
  37. ></el-option>
  38. </el-select>
  39. </div>
  40. <div class="r_cbox">
  41. <span>任务内容</span>
  42. <el-select
  43. class="r_select"
  44. v-model="task"
  45. multiple
  46. collapse-tags
  47. placeholder="全部"
  48. >
  49. <el-option-group
  50. v-for="(group, index) in dyAarray"
  51. :key="index"
  52. :label="group.name"
  53. >
  54. <el-option
  55. v-for="(item, index2) in group.taskList"
  56. :key="index + '-' + index2"
  57. :label="item.name"
  58. :value="group.id + '-' + item.id"
  59. >
  60. </el-option>
  61. </el-option-group>
  62. </el-select>
  63. </div>
  64. <div class="btn_box">
  65. <el-button class="btn btnClassGM" type="primary" @click="confirm">Save</el-button>
  66. </div>
  67. </div>
  68. </template>
  69. <script>
  70. import htmlDocx from "html-docx-js/dist/html-docx";
  71. import saveAs from "file-saver";
  72. export default {
  73. props: ["cid"],
  74. data() {
  75. return {
  76. uname: [],
  77. chap: [],
  78. task: [],
  79. dyAarray: [],
  80. dyList: [],
  81. userAarray: [],
  82. imgList: [],
  83. askList: [],
  84. questList: [],
  85. wordList: [],
  86. course: {},
  87. };
  88. },
  89. methods: {
  90. getCourseDetail() {
  91. let params = {
  92. cid: this.cid,
  93. };
  94. this.ajax
  95. .get(this.$store.state.api + "getCourseWorksReport", params)
  96. .then((res) => {
  97. this.course = res.data[0][0];
  98. var dyJSON = JSON.parse(res.data[0][0].chapters);
  99. let dyList = [];
  100. for (var i = 0; i < dyJSON.length; i++) {
  101. dyList.push({ name: dyJSON[i].dyName, id: i, taskList: [] });
  102. var a = dyJSON[i].chapterInfo[0].taskJson;
  103. for (var j = 0; j < a.length; j++) {
  104. dyList[i].taskList.push({ name: a[j].task, id: j });
  105. }
  106. }
  107. this.dyList = dyList;
  108. this.userAarray = res.data[1];
  109. console.log(res.data[0][1]);
  110. this.imgList = res.data[2];
  111. this.askList = res.data[3];
  112. this.questList = res.data[4];
  113. this.wordList = res.data[5];
  114. })
  115. .catch((err) => {
  116. console.error(err);
  117. });
  118. },
  119. confirm() {
  120. var _uname = [];
  121. var _userA = this.userAarray;
  122. var _chap = JSON.parse(JSON.stringify(this.chap));
  123. var _task = this.task;
  124. if (this.uname.length) {
  125. for (var i = 0; i < _userA.length; i++) {
  126. if (this.uname.join(",").indexOf(_userA[i].userid) != -1) {
  127. _uname.push(_userA[i]);
  128. }
  129. }
  130. } else {
  131. for (var i = 0; i < _userA.length; i++) {
  132. _uname.push(_userA[i]);
  133. }
  134. }
  135. if (!_chap.length) {
  136. for (var i = 0; i < this.dyList.length; i++) {
  137. _chap.push(this.dyList[i].id);
  138. }
  139. }
  140. let imgList = this.imgList;
  141. let askList = this.askList;
  142. let questList = this.questList;
  143. let wordList = this.wordList;
  144. let workArray = [];
  145. // json{
  146. // img: [],
  147. // askList: [],
  148. // questList: {},
  149. // wordList: [],
  150. // chapName: "",
  151. // taskName: "",
  152. // username:""
  153. // }
  154. for (let i = 0; i < _uname.length; i++) {
  155. for (let chapI = 0; chapI < _chap.length; chapI++) {
  156. if (_task.length) {
  157. for (let taskI = 0; taskI < _task.length; taskI++) {
  158. let _tI = _task[taskI].split("-");
  159. if (_tI[0] == _chap[chapI]) {
  160. let _workjson = {
  161. img: [],
  162. askList: [],
  163. questList: [],
  164. wordList: [],
  165. chapName:
  166. "第" +
  167. (parseInt(_chap[chapI]) + 1) +
  168. "阶段 " +
  169. this.dyList[_chap[chapI]].name,
  170. taskName:
  171. "任务" +
  172. (parseInt(_tI[1]) + 1) +
  173. this.dyList[_chap[chapI]].taskList[_tI[1]].name,
  174. username: _uname[i].username,
  175. };
  176. // imgList;askList;questList;wordList;
  177. for (let a = 0; a < imgList.length; a++) {
  178. if (
  179. imgList[a].stage == _chap[chapI] &&
  180. imgList[a].task == _tI[1] &&
  181. imgList[a].userid == _uname[i].userid
  182. ) {
  183. _workjson.img.push({ src: imgList[a].content });
  184. }
  185. }
  186. for (let b = 0; b < askList.length; b++) {
  187. if (
  188. askList[b].stage == _chap[chapI] &&
  189. askList[b].task == _tI[1] &&
  190. askList[b].userid == _uname[i].userid
  191. ) {
  192. const element = askList[b];
  193. let a1 = JSON.parse(element.content)[0];
  194. let b1 = a1.anwer.split(",");
  195. let c1 = [];
  196. for (var j1 = 0; j1 < b1.length; j1++) {
  197. c1.push(parseInt(b1[j1]));
  198. }
  199. a1.askJson.radio = c1;
  200. a1.askJson.time = element.time;
  201. _workjson.askList.push(a1.askJson);
  202. }
  203. }
  204. for (let c = 0; c < questList.length; c++) {
  205. if (
  206. questList[c].stage == _chap[chapI] &&
  207. questList[c].task == _tI[1] &&
  208. questList[c].userid == _uname[i].userid
  209. ) {
  210. const element = questList[c];
  211. _workjson.questList.push(JSON.parse(element.content)[0]);
  212. }
  213. }
  214. for (let d = 0; d < wordList.length; d++) {
  215. if (
  216. wordList[d].stage == _chap[chapI] &&
  217. wordList[d].task == _tI[1] &&
  218. wordList[d].userid == _uname[i].userid
  219. ) {
  220. const element = wordList[d];
  221. _workjson.wordList.push(element.content);
  222. }
  223. }
  224. workArray.push(_workjson);
  225. }
  226. }
  227. } else {
  228. for (
  229. let taskI = 0;
  230. taskI < this.dyList[_chap[chapI]].taskList.length;
  231. taskI++
  232. ) {
  233. let _tI = [
  234. _chap[chapI],
  235. this.dyList[_chap[chapI]].taskList[taskI].id,
  236. ];
  237. if (_tI[0] == _chap[chapI]) {
  238. let _workjson = {
  239. img: [],
  240. askList: [],
  241. questList: [],
  242. wordList: [],
  243. chapName:
  244. "第" +
  245. (parseInt(_chap[chapI]) + 1) +
  246. "阶段 " +
  247. this.dyList[_chap[chapI]].name,
  248. taskName:
  249. "任务" +
  250. (parseInt(_tI[1]) + 1) +
  251. this.dyList[_chap[chapI]].taskList[_tI[1]].name,
  252. username: _uname[i].username,
  253. };
  254. // imgList;askList;questList;wordList;
  255. for (let a = 0; a < imgList.length; a++) {
  256. if (
  257. imgList[a].stage == _chap[chapI] &&
  258. imgList[a].task == _tI[1] &&
  259. imgList[a].userid == _uname[i].userid
  260. ) {
  261. _workjson.img.push({ src: imgList[a].content });
  262. }
  263. }
  264. for (let b = 0; b < askList.length; b++) {
  265. if (
  266. askList[b].stage == _chap[chapI] &&
  267. askList[b].task == _tI[1] &&
  268. askList[b].userid == _uname[i].userid
  269. ) {
  270. const element = askList[b];
  271. let a1 = JSON.parse(element.content)[0];
  272. let b1 = a1.anwer.split(",");
  273. let c1 = [];
  274. for (var j1 = 0; j1 < b1.length; j1++) {
  275. c1.push(parseInt(b1[j1]));
  276. }
  277. a1.askJson.radio = c1;
  278. a1.askJson.time = element.time;
  279. _workjson.askList.push(a1.askJson);
  280. }
  281. }
  282. for (let c = 0; c < questList.length; c++) {
  283. if (
  284. questList[c].stage == _chap[chapI] &&
  285. questList[c].task == _tI[1] &&
  286. questList[c].userid == _uname[i].userid
  287. ) {
  288. const element = questList[c];
  289. _workjson.questList.push(JSON.parse(element.content)[0]);
  290. }
  291. }
  292. for (let d = 0; d < wordList.length; d++) {
  293. if (
  294. wordList[d].stage == _chap[chapI] &&
  295. wordList[d].task == _tI[1] &&
  296. wordList[d].userid == _uname[i].userid
  297. ) {
  298. const element = wordList[d];
  299. _workjson.wordList.push(element.content);
  300. }
  301. }
  302. workArray.push(_workjson);
  303. }
  304. }
  305. }
  306. }
  307. }
  308. var _course = `<h1>${this.course.title}</h1>`;
  309. let html = "";
  310. for (let i = 0; i < workArray.length; i++) {
  311. let _html = this.setHtml(workArray[i]);
  312. html += _html;
  313. }
  314. this.generate(_course + html);
  315. console.log(workArray);
  316. },
  317. setHtml(workJson) {
  318. var _title =
  319. // style='display:flex;align-items:center'
  320. "<div >" +
  321. "<h2>" +
  322. workJson.chapName +
  323. "-" +
  324. workJson.username;
  325. ("</h2>");
  326. ("</div>");
  327. var _subtitle = "<h3>" + workJson.taskName + "</h3>";
  328. var _img = "";
  329. if (workJson.img.length > 0) {
  330. for (var i = 0; i < workJson.img.length; i++) {
  331. // _img+=`<div style="margin-top:10px"><div>图片${
  332. // i + 1
  333. // }:<a href='${ workJson.img[i].src}'>${ workJson.img[i].src}</a></div>`
  334. _img += `<div style="margin-top:10px"><div>图片${
  335. i + 1
  336. }</div><img style="max-width:500px" src="${
  337. workJson.img[i].src
  338. }"/></div>`;
  339. }
  340. }
  341. var _ask = "";
  342. if (workJson.askList.length > 0) {
  343. var _ask = "<h3>问卷调查</h3>";
  344. for (var i = 0; i < workJson.askList.length; i++) {
  345. var _div = document.createElement("div");
  346. _div.innerHTML = `<h4>问卷标题:${workJson.askList[i].askTitle}</h4>`;
  347. for (var j = 0; j < workJson.askList[i].askJson.length; j++) {
  348. var _div2 = document.createElement("div");
  349. _div2.innerHTML = `<h5>第${j + 1}题:${
  350. workJson.askList[i].askJson[j].askstitle
  351. } 选择:${workJson.askList[i].radio[j] + 1}</h5> `;
  352. var _div3 = document.createElement("div");
  353. for (
  354. var z = 0;
  355. z < workJson.askList[i].askJson[j].checkList.length;
  356. z++
  357. ) {
  358. _div3.innerHTML += `<span style="margin-right:5px">${z + 1}.${
  359. workJson.askList[i].askJson[j].checkList[z]
  360. }</span>`;
  361. }
  362. _div2.innerHTML += `<div>${_div3.innerHTML}</div>`;
  363. _div.innerHTML += `<div>${_div2.innerHTML}</div>`;
  364. }
  365. _ask += `<div>${_div.innerHTML}</div>`;
  366. }
  367. }
  368. var _answer = "";
  369. if (workJson.questList.length > 0) {
  370. var _answer = "<h3>问答</h3>";
  371. for (var i = 0; i < workJson.questList.length; i++) {
  372. var _div = document.createElement("div");
  373. _div.innerHTML = `<h4>问答:${workJson.questList[i].answerTitle}</h4>`;
  374. _div.innerHTML += `<div>${workJson.questList[i].answer}</div>`;
  375. _answer += `<div>${_div.innerHTML}</div>`;
  376. }
  377. }
  378. var _link = "";
  379. if (workJson.wordList.length > 0) {
  380. for (var i = 0; i < workJson.wordList.length; i++) {
  381. _link += `<div style="margin-top:10px"><a href='${workJson.wordList[i]}'>${workJson.wordList[i]}</a></div>`;
  382. }
  383. }
  384. if (
  385. !workJson.img.length &&
  386. !workJson.askList.length &&
  387. !workJson.questList.length &&
  388. !workJson.wordList.length
  389. ) {
  390. return "";
  391. }
  392. return _title + _subtitle + _img + _ask + _answer + _link;
  393. },
  394. async generate(a) {
  395. // 将html文件中需要用到的数据挂载到store上
  396. this.$store.commit("update", ["report", a]);
  397. console.log(this.$store.state.report);
  398. const content = `<!DOCTYPE html>
  399. <html lang="en">
  400. <head>
  401. <meta charset="UTF-8">
  402. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  403. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  404. <title>报告</title>
  405. </head>
  406. <body>
  407. ${this.$store.state.report}
  408. </body>
  409. </html>`;
  410. // debugger
  411. //生成报告
  412. const link = document.createElement("a");
  413. // link.download = "报告.html"; // 文件名
  414. let dname = this.course.title+'-'
  415. var _uname = [];
  416. var _userA = this.userAarray;
  417. if (this.uname.length) {
  418. for (var i = 0; i < _userA.length; i++) {
  419. if (this.uname.join(",").indexOf(_userA[i].userid) != -1) {
  420. _uname.push(_userA[i].username);
  421. }
  422. }
  423. dname+=_uname.join('、')+'.html'
  424. } else {
  425. dname+='全部学生.html'
  426. }
  427. link.download = dname; // 文件名
  428. link.style.display = "none";
  429. // 创建文件流
  430. // 创建bolb实例时,内容一定要放在[]中
  431. const blob = new Blob([content], {
  432. type: "text/plain;charset='utf-8'",
  433. });
  434. link.href = window.URL.createObjectURL(blob);
  435. document.body.appendChild(link);
  436. link.click();
  437. document.body.removeChild(link);
  438. // saveAs(
  439. // htmlDocx.asBlob(content, {
  440. // orientation: "landscape", //跨域设置
  441. // }),
  442. // //文件名
  443. // "报告.doc"
  444. // );
  445. },
  446. changeChap() {
  447. let _dyList = this.dyList;
  448. this.dyAarray = [];
  449. this.task = [];
  450. for (var i = 0; i < _dyList.length; i++) {
  451. if (this.chap.indexOf(_dyList[i].id) != -1) {
  452. this.dyAarray.push(_dyList[i]);
  453. }
  454. }
  455. },
  456. },
  457. watch: {
  458. cid(newValue, oldValue) {
  459. this.getCourseDetail();
  460. },
  461. },
  462. mounted() {
  463. this.getCourseDetail();
  464. },
  465. };
  466. </script>
  467. <style scoped>
  468. .r_box {
  469. }
  470. .r_cbox {
  471. display: flex;
  472. align-items: center;
  473. justify-content: center;
  474. margin-bottom: 20px;
  475. }
  476. .r_cbox span {
  477. margin-right: 20px;
  478. }
  479. .r_box .r_select {
  480. width: 400px;
  481. }
  482. .btn_box {
  483. margin: 40px 0 0 0;
  484. width: 100%;
  485. display: flex;
  486. justify-content: center;
  487. }
  488. .btn_box .btn {
  489. width: 60%;
  490. /* background: rgb(14, 114, 230); */
  491. }
  492. .r_cbox >>> .el-select .el-input.is-focus .el-input__inner {
  493. border-color: #5c549f;
  494. }
  495. .r_cbox.student_search >>> .el-select .el-input__inner:focus {
  496. border-color: #5c549f;
  497. }
  498. .el-select-dropdown__item.selected {
  499. color: #5c549f !important;
  500. }
  501. </style>