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">
  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();
  122. }
  123. },
  124. async initClass() {
  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({ termid: termid, teaid: this.user.userid });
  134. }
  135. if (!res) return;
  136. if (this.$checkRes(res)) {
  137. const d = res.data.filter(f => f.termid === termid);
  138. this.$set(this, `classList`, d);
  139. }
  140. // 检查是否是初始化,如果是初始化(由上级来),需要检查classid是不是在这里,不在就清除掉
  141. const r = this.classList.find(f => f._id === classid);
  142. if (!r) this.toClear('classid');
  143. if (this.needInit) {
  144. this.checkToday();
  145. }
  146. },
  147. checkToday() {
  148. //教师/班主任,如果今天有班级选择
  149. for (const i of this.classList) {
  150. const { startdate, enddate } = i;
  151. const r = moment(moment().format('YYYY-MM-DD')).isBetween(startdate, enddate, null, '[]');
  152. if (r) {
  153. const { _id, termid } = i;
  154. this.$set(this.topOptions, `classid`, _id);
  155. break;
  156. }
  157. }
  158. sessionStorage.setItem('needInit', false);
  159. },
  160. toClear() {
  161. this.$set(this.topOptions, `classid`, undefined);
  162. // this.$set(this, `topOptions`, _.omit(this.topOptions, type));
  163. },
  164. // setVuexOpt() {
  165. // console.log(this.topOptions);
  166. // this.changeOpt(this.topOptions);
  167. // },
  168. async settingSave() {
  169. let res = await this.sUpdate(this.topOptions);
  170. this.$checkRes(res, '设置成功', res.errmsg);
  171. },
  172. },
  173. watch: {
  174. topOptions: {
  175. deep: true,
  176. handler(val, oval) {
  177. console.log(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>