default-select.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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-loading="!topOptions">
  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="topOptions.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="topOptions.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="topOptions.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="topOptions.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, mapGetters, 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. if (!this.topOptions) return;
  89. const { planyearid } = this.topOptions;
  90. if (planyearid) this.initPlan();
  91. },
  92. async initPlan() {
  93. const { planyearid, planid, termid } = this.topOptions;
  94. if (!planyearid) return;
  95. const res = await this.getPlan({ planyearid });
  96. if (this.$checkRes(res)) {
  97. this.$set(this, `planList`, res.data);
  98. // 没有有计划id,清空后面的值
  99. if (!planid) {
  100. this.toClear('planid');
  101. this.toClear('termid');
  102. this.toClear('classid');
  103. return;
  104. }
  105. const plan = res.data.find(f => f._id == planid);
  106. // 现在的计划id没有找对应的计划,清空后面的值
  107. if (!plan) {
  108. this.toClear('planid');
  109. this.toClear('termid');
  110. this.toClear('classid');
  111. return;
  112. }
  113. // 找到计划了
  114. const { termnum } = plan;
  115. const tlist = _.orderBy(
  116. termnum.map(i => ({ ...i, term: parseInt(i.term) })),
  117. ['term'],
  118. ['asc']
  119. );
  120. this.$set(this, `termList`, tlist);
  121. if (termid) this.initClass('init');
  122. }
  123. },
  124. async initClass(type) {
  125. const { termid, classid, planid } = this.topOptions;
  126. let res;
  127. // 检查身份
  128. let query = { termid };
  129. if (this.user.type == 1) {
  130. query.headteacherid = this.user.userid;
  131. res = await this.getClassList(query);
  132. } else if (this.user.type == 3) {
  133. res = await this.teaclass({ planid: planid, teaid: this.user.userid });
  134. }
  135. if (!res) return;
  136. if (this.$checkRes(res)) this.$set(this, `classList`, res.data);
  137. // 检查是否是初始化,如果是初始化(由上级来),需要检查classid是不是在这里,不在就清除掉
  138. const r = this.classList.find(f => f._id === classid);
  139. if (!r) this.toClear('classid');
  140. if (this.needInit) {
  141. this.checkToday();
  142. }
  143. },
  144. checkToday() {
  145. //教师/班主任,如果今天有班级选择
  146. for (const i of this.classList) {
  147. const { startdate, enddate } = i;
  148. const r = moment().isBetween(startdate, enddate, null, '[]');
  149. if (r) {
  150. const { _id, termid } = i;
  151. let options = _.cloneDeep(this.topOptions);
  152. options.classid = _id;
  153. this.$set(this, `options`, options);
  154. break;
  155. }
  156. }
  157. sessionStorage.setItem('needInit', false);
  158. },
  159. toClear(type) {
  160. this.$set(this, `options`, _.omit(this.topOptions, type));
  161. },
  162. // setVuexOpt() {
  163. // console.log(this.topOptions);
  164. // this.changeOpt(this.topOptions);
  165. // },
  166. async settingSave() {
  167. let res = await this.sUpdate(this.topOptions);
  168. this.$checkRes(res, '设置成功', res.errmsg);
  169. },
  170. },
  171. watch: {
  172. topOptions: {
  173. deep: true,
  174. handler(val, oval) {
  175. this.changeOpt(val);
  176. },
  177. },
  178. },
  179. computed: {
  180. ...mapState(['user', 'defaultOption']),
  181. ...mapGetters(['topOptions']),
  182. pageTitle() {
  183. return `${this.$route.meta.title}`;
  184. },
  185. needInit() {
  186. let needInit = sessionStorage.getItem('needInit');
  187. if (needInit === false) return true;
  188. else return !needInit;
  189. },
  190. },
  191. metaInfo() {
  192. return { title: this.$route.meta.title };
  193. },
  194. };
  195. </script>
  196. <style lang="less" scoped>
  197. .user-menu {
  198. height: 4rem;
  199. .el-col {
  200. margin-left: 10px;
  201. span {
  202. margin-left: 10px;
  203. }
  204. }
  205. }
  206. </style>