default-select.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <template>
  2. <div id="default-select">
  3. <el-row type="flex" align="middle" justify="start" class="user-menu" v-loading="loading">
  4. <el-col :span="15" v-if="options">
  5. 当前默认:
  6. <span>
  7. <!-- @change="data => changeList('plan', data)" -->
  8. <el-tooltip :disabled="this.user.type != 0" content="点击更改默认批次" effect="dark" placement="bottom">
  9. <el-select v-model="options.planyearid" :disabled="this.user.type != 0" placeholder="未设置培训计划" size="mini" @change="initPlan">
  10. <el-option v-for="(i, index) in planYearList" :key="index" :label="i.title" :value="i._id"></el-option>
  11. </el-select>
  12. </el-tooltip>
  13. </span>
  14. <span>
  15. <!-- @change="getTermList" -->
  16. <el-tooltip :disabled="this.user.type != 0" content="点击更改默认年度计划" effect="dark" placement="bottom">
  17. <el-select v-model="options.planid" :disabled="this.user.type != 0" placeholder="未设置年度计划" size="mini">
  18. <el-option v-for="(i, index) in planList" :key="index" :label="i.title" :value="i._id"></el-option>
  19. </el-select>
  20. </el-tooltip>
  21. </span>
  22. <span>
  23. <!-- @change="toCheckUserType()" -->
  24. <el-tooltip content="点击更改默认期" effect="dark" placement="bottom">
  25. <el-select v-model="options.termid" placeholder="未设置默认期" size="mini" @change="initClass">
  26. <el-option v-for="(i, index) in termList" :key="index" :label="`第${i.term}期`" :value="i._id"></el-option>
  27. </el-select>
  28. </el-tooltip>
  29. </span>
  30. <span v-if="user.type == 1 || user.type == 3">
  31. <!-- @change="setVuexOpt()" -->
  32. <el-tooltip content="选择要查看的班级" effect="dark" placement="bottom">
  33. <el-select v-model="options.classid" placeholder="选择要查看的班级" size="mini">
  34. <el-option v-for="(i, index) in classList" :key="index" :label="i.name" :value="i._id"></el-option>
  35. </el-select>
  36. </el-tooltip>
  37. </span>
  38. <span>
  39. <el-button type="text" size="mini" @click="settingSave" v-if="this.user.type == 0">保存默认值</el-button>
  40. </span>
  41. </el-col>
  42. </el-row>
  43. </div>
  44. </template>
  45. <script>
  46. import _ from 'lodash';
  47. const moment = require('moment');
  48. import { mapState, mapMutations, createNamespacedHelpers } from 'vuex';
  49. const { mapActions: trainBatch } = createNamespacedHelpers('trainBatch');
  50. const { mapActions: trainplan } = createNamespacedHelpers('trainplan');
  51. const { mapActions: setting } = createNamespacedHelpers('setting');
  52. const { mapActions: classes } = createNamespacedHelpers('classes');
  53. const { mapActions: lesson } = createNamespacedHelpers('lesson');
  54. export default {
  55. name: 'default-select',
  56. props: {},
  57. components: {},
  58. data: function() {
  59. return {
  60. loading: false,
  61. options: undefined,
  62. planYearList: [],
  63. planList: [],
  64. termList: [],
  65. classList: [],
  66. };
  67. },
  68. async created() {
  69. if (this.needInit) {
  70. const cache = sessionStorage.getItem('defaultOption');
  71. if (cache) this.$set(this, `options`, JSON.parse(cache));
  72. } else {
  73. this.$set(this, `options`, _.cloneDeep(this.defaultOption));
  74. }
  75. await this.init({ type: 'planYear' });
  76. this.loading = false;
  77. },
  78. methods: {
  79. ...mapMutations(['deleteUser', 'changeOpt']),
  80. ...trainBatch({ getplanYear: 'query' }),
  81. ...trainplan({ getPlan: 'query' }),
  82. ...setting({ sFetch: 'fetch', sUpdate: 'update' }),
  83. ...classes({ getClassList: 'query' }), //仅供班主任使用
  84. ...lesson({ teaclass: 'teaclass' }), //仅供老师使用
  85. async init() {
  86. let res = await this.getplanYear();
  87. if (this.$checkRes(res)) this.$set(this, `planYearList`, res.data);
  88. const { planyearid } = this.options;
  89. if (planyearid) this.initPlan();
  90. },
  91. async initPlan() {
  92. const { planyearid, planid, termid } = this.options;
  93. if (!planyearid) return;
  94. const res = await this.getPlan({ planyearid });
  95. if (this.$checkRes(res)) {
  96. this.$set(this, `planList`, res.data);
  97. // 没有有计划id,清空后面的值
  98. if (!planid) {
  99. this.toClear('planid');
  100. this.toClear('termid');
  101. this.toClear('classid');
  102. return;
  103. }
  104. const plan = res.data.find(f => f._id == planid);
  105. // 现在的计划id没有找对应的计划,清空后面的值
  106. if (!plan) {
  107. this.toClear('planid');
  108. this.toClear('termid');
  109. this.toClear('classid');
  110. return;
  111. }
  112. // 找到计划了
  113. const { termnum } = plan;
  114. const tlist = _.orderBy(
  115. termnum.map(i => ({ ...i, term: parseInt(i.term) })),
  116. ['term'],
  117. ['asc']
  118. );
  119. this.$set(this, `termList`, tlist);
  120. if (termid) this.initClass('init');
  121. }
  122. },
  123. async initClass(type) {
  124. const { termid, classid, planid } = this.options;
  125. let res;
  126. // 检查身份
  127. let query = { termid };
  128. if (this.user.type == 1) {
  129. query.headteacherid = this.user.userid;
  130. res = await this.getClassList(query);
  131. } else if (this.user.type == 3) {
  132. res = await this.teaclass({ planid: planid, teaid: this.user.userid });
  133. }
  134. if (!res) return;
  135. if (this.$checkRes(res)) this.$set(this, `classList`, res.data);
  136. // 检查是否是初始化,如果是初始化(由上级来),需要检查classid是不是在这里,不在就清除掉
  137. const r = this.classList.find(f => f._id === classid);
  138. if (!r) this.toClear('classid');
  139. if (this.needInit) {
  140. this.checkToday();
  141. }
  142. },
  143. checkToday() {
  144. //教师/班主任,如果今天有班级选择
  145. for (const i of this.classList) {
  146. const { startdate, enddate } = i;
  147. const r = moment().isBetween(startdate, enddate, null, '[]');
  148. if (r) {
  149. const { _id, termid } = i;
  150. let options = _.cloneDeep(this.options);
  151. options.classid = _id;
  152. this.$set(this, `options`, options);
  153. break;
  154. }
  155. }
  156. sessionStorage.setItem('needInit', false);
  157. },
  158. toClear(type) {
  159. this.$set(this, `options`, _.omit(this.options, type));
  160. },
  161. // setVuexOpt() {
  162. // console.log(this.options);
  163. // this.changeOpt(this.options);
  164. // },
  165. async settingSave() {
  166. let res = await this.sUpdate(this.options);
  167. this.$checkRes(res, '设置成功', res.errmsg);
  168. },
  169. },
  170. watch: {
  171. options: {
  172. deep: true,
  173. handler(val, oval) {
  174. this.changeOpt(val);
  175. },
  176. },
  177. },
  178. computed: {
  179. ...mapState(['user', 'defaultOption']),
  180. pageTitle() {
  181. return `${this.$route.meta.title}`;
  182. },
  183. needInit() {
  184. let needInit = sessionStorage.getItem('needInit');
  185. if (needInit === false) return true;
  186. else return !needInit;
  187. },
  188. },
  189. metaInfo() {
  190. return { title: this.$route.meta.title };
  191. },
  192. };
  193. </script>
  194. <style lang="less" scoped>
  195. .user-menu {
  196. height: 4rem;
  197. .el-col {
  198. margin-left: 10px;
  199. span {
  200. margin-left: 10px;
  201. }
  202. }
  203. }
  204. </style>