attendance.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <div id="attendance">
  3. <listFrame :title="pageTitle" @query="skipsearch" :total="total" :needFilter="false" :returns="toreturn" :needAdd="false">
  4. <attendanceInfo :tableData="tableData" :options="options" @filterDate="filterDate"></attendanceInfo>
  5. </listFrame>
  6. </div>
  7. </template>
  8. <script>
  9. import _ from 'lodash';
  10. var moment = require('moment');
  11. import attendanceInfo from '@frame/parts/attendance';
  12. import listFrame from '@frame/layout/admin/list-frame';
  13. import { mapState, createNamespacedHelpers } from 'vuex';
  14. const { mapActions: attendance } = createNamespacedHelpers('attendance');
  15. const { mapActions: classes } = createNamespacedHelpers('classes');
  16. export default {
  17. name: 'attendance',
  18. props: {},
  19. components: { attendanceInfo, listFrame },
  20. data: () => ({
  21. total: 0,
  22. attendList: [],
  23. tableData: [],
  24. options: [],
  25. dataList: [],
  26. }),
  27. created() {
  28. this.search();
  29. },
  30. computed: {
  31. id() {
  32. return this.$route.query.id;
  33. },
  34. pageTitle() {
  35. return `${this.$route.meta.title}`;
  36. },
  37. },
  38. methods: {
  39. ...attendance(['fetch', 'query']),
  40. ...classes({ classFetch: 'fetch' }),
  41. async search() {
  42. const attendList = await this.query({ classid: this.id });
  43. this.$set(this, `attendList`, attendList.data);
  44. const classInfo = await this.classFetch(this.id);
  45. const timeList = await this.getAllDays(classInfo.data.startdate, classInfo.data.enddate);
  46. this.$set(this, `options`, timeList);
  47. },
  48. skipsearch({ skip = 0, limit = 10 } = {}) {
  49. const data = _.slice(this.dataList, skip, skip + limit);
  50. this.$set(this, `tableData`, data);
  51. },
  52. filterDate(time) {
  53. const data = [];
  54. for (const attendInfo of this.attendList) {
  55. let dataInfo = {};
  56. const attend = attendInfo.attend;
  57. for (const _attend of attend) {
  58. dataInfo.name = attendInfo.stuname;
  59. if (moment(time).isSame(_attend.date)) {
  60. dataInfo.date = time;
  61. dataInfo.time = _attend.time;
  62. dataInfo.status = _attend.status;
  63. dataInfo.type = _attend.type;
  64. data.push(dataInfo);
  65. }
  66. dataInfo = {};
  67. }
  68. }
  69. this.$set(this, `dataList`, data);
  70. this.$set(this, `total`, data.length);
  71. this.skipsearch();
  72. },
  73. toreturn() {
  74. window.history.go(-1);
  75. },
  76. // 取得日期间所有日期
  77. async getAllDays(begin_date, end_date) {
  78. const errArr = [],
  79. resultArr = [],
  80. dateReg = /^[2]\d{3}-[01]\d-[0123]\d$/;
  81. if (typeof begin_date !== 'string' || begin_date === '' || !dateReg.test(begin_date)) {
  82. return errArr;
  83. }
  84. if (typeof end_date !== 'string' || end_date === '' || !dateReg.test(end_date)) {
  85. return errArr;
  86. }
  87. try {
  88. const beginTimestamp = Date.parse(new Date(begin_date)),
  89. endTimestamp = Date.parse(new Date(end_date));
  90. // 开始日期小于结束日期
  91. if (beginTimestamp > endTimestamp) {
  92. return errArr;
  93. }
  94. // 开始日期等于结束日期
  95. if (beginTimestamp === endTimestamp) {
  96. resultArr.push(begin_date);
  97. return resultArr;
  98. }
  99. let tempTimestamp = beginTimestamp,
  100. tempDate = begin_date;
  101. // 新增日期是否和结束日期相等, 相等跳出循环
  102. while (tempTimestamp !== endTimestamp) {
  103. resultArr.push(tempDate);
  104. // 增加一天
  105. tempDate = moment(tempTimestamp)
  106. .add(1, 'd')
  107. .format('YYYY-MM-DD');
  108. // 将增加时间变为时间戳
  109. tempTimestamp = Date.parse(new Date(tempDate));
  110. }
  111. // 将最后一天放入数组
  112. resultArr.push(end_date);
  113. return resultArr;
  114. } catch (err) {
  115. return errArr;
  116. }
  117. },
  118. },
  119. metaInfo() {
  120. return { title: this.$route.meta.title };
  121. },
  122. watch: {
  123. classId: {
  124. handler(val) {
  125. if (val) {
  126. this.search();
  127. }
  128. },
  129. immediate: true,
  130. },
  131. },
  132. };
  133. </script>
  134. <style lang="less" scoped></style>