default-select.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <div id="default-select">
  3. <el-row type="flex" align="middle" justify="start" class="user-menu" v-if="user" 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" clearable>
  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" clearable>
  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" clearable>
  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" clearable>
  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. <span>
  42. <el-button type="text" size="mini" @click="toRefresh" icon="el-icon-refresh"></el-button>
  43. </span>
  44. </el-col>
  45. </el-row>
  46. </div>
  47. </template>
  48. <script>
  49. import _ from 'lodash';
  50. const moment = require('moment');
  51. import { mapState, mapMutations, mapGetters, createNamespacedHelpers } from 'vuex';
  52. const { mapActions: trainBatch } = createNamespacedHelpers('trainBatch');
  53. const { mapActions: trainplan } = createNamespacedHelpers('trainplan');
  54. const { mapActions: setting } = createNamespacedHelpers('setting');
  55. const { mapActions: classes } = createNamespacedHelpers('classes');
  56. const { mapActions: lesson } = createNamespacedHelpers('lesson');
  57. export default {
  58. name: 'default-select',
  59. props: {},
  60. components: {},
  61. data: function() {
  62. return {
  63. loading: false,
  64. options: undefined,
  65. planYearList: [],
  66. planList: [],
  67. termList: [],
  68. classList: [],
  69. };
  70. },
  71. async created() {
  72. await this.init({ type: 'planYear' });
  73. this.loading = false;
  74. },
  75. methods: {
  76. ...mapMutations(['deleteUser', 'changeOpt']),
  77. ...trainBatch({ getplanYear: 'query' }),
  78. ...trainplan({ getPlan: 'query' }),
  79. ...setting({ sFetch: 'fetch', sUpdate: 'update', checkCache: 'checkCache' }),
  80. ...classes({ getClassList: 'query' }), //仅供班主任使用
  81. ...lesson({ teaclass: 'teaclass' }), //仅供老师使用
  82. async init() {
  83. let res = await this.getplanYear();
  84. if (this.$checkRes(res)) this.$set(this, `planYearList`, res.data);
  85. if (!this.topOptions) return;
  86. const { planyearid } = this.topOptions;
  87. if (planyearid) this.initPlan();
  88. },
  89. async initPlan() {
  90. const { planyearid, planid, termid } = this.topOptions;
  91. if (!planyearid) return;
  92. const res = await this.getPlan({ planyearid });
  93. if (this.$checkRes(res)) {
  94. this.$set(this, `planList`, res.data);
  95. // 没有有计划id,清空后面的值
  96. if (!planid) {
  97. this.toClear('planid');
  98. this.toClear('termid');
  99. this.toClear('classid');
  100. return;
  101. }
  102. const plan = res.data.find(f => f._id == planid);
  103. // 现在的计划id没有找对应的计划,清空后面的值
  104. if (!plan) {
  105. this.toClear('planid');
  106. this.toClear('termid');
  107. this.toClear('classid');
  108. return;
  109. }
  110. // 找到计划了
  111. const { termnum } = plan;
  112. const tlist = _.orderBy(
  113. termnum.map(i => ({ ...i, term: i.term * 1 })),
  114. ['term'],
  115. ['asc']
  116. );
  117. this.$set(this, `termList`, tlist);
  118. if (termid) this.initClass();
  119. }
  120. },
  121. async initClass() {
  122. const { termid, classid, planid } = this.topOptions;
  123. let res;
  124. // 检查身份
  125. let query = { termid };
  126. if (this.user.type == 1) {
  127. query.headteacherid = this.user.userid;
  128. res = await this.getClassList(query);
  129. } else if (this.user.type == 3) {
  130. res = await this.teaclass({ termid: termid, teaid: this.user.userid });
  131. }
  132. if (!res) return;
  133. if (this.$checkRes(res)) {
  134. const d = res.data.filter(f => f.termid === termid);
  135. this.$set(this, `classList`, d);
  136. }
  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(moment().format('YYYY-MM-DD')).isBetween(startdate, enddate, null, '[]');
  149. if (r) {
  150. const { _id, termid } = i;
  151. this.$set(this.topOptions, `classid`, _id);
  152. break;
  153. }
  154. }
  155. sessionStorage.setItem('needInit', false);
  156. },
  157. toClear() {
  158. this.$set(this.topOptions, `classid`, undefined);
  159. // this.$set(this, `topOptions`, _.omit(this.topOptions, type));
  160. },
  161. // setVuexOpt() {
  162. // console.log(this.topOptions);
  163. // this.changeOpt(this.topOptions);
  164. // },
  165. async settingSave() {
  166. let res = await this.sUpdate(this.topOptions);
  167. this.$checkRes(res, '设置成功', res.errmsg);
  168. },
  169. toRefresh() {
  170. this.$emit('refresh');
  171. this.checkCache();
  172. },
  173. },
  174. watch: {
  175. topOptions: {
  176. deep: true,
  177. handler(val, oval) {
  178. this.changeOpt(val);
  179. },
  180. },
  181. },
  182. computed: {
  183. ...mapState(['user', 'defaultOption']),
  184. ...mapGetters(['topOptions']),
  185. pageTitle() {
  186. return `${this.$route.meta.title}`;
  187. },
  188. needInit() {
  189. let needInit = sessionStorage.getItem('needInit');
  190. if (needInit === false) return true;
  191. else return !needInit;
  192. },
  193. },
  194. metaInfo() {
  195. return { title: this.$route.meta.title };
  196. },
  197. };
  198. </script>
  199. <style lang="less" scoped>
  200. .user-menu {
  201. height: 4rem;
  202. .el-col {
  203. margin-left: 10px;
  204. span {
  205. margin-left: 10px;
  206. }
  207. }
  208. }
  209. </style>