default-select.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 @change="getTermList">
  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 @change="selectClass">
  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) await this.initPlan();
  88. if (this.user.type === '1' || this.user.type === '3') this.useClassTemp();
  89. this.$emit('defaultLoad');
  90. },
  91. async initPlan() {
  92. const { planyearid, planid, termid } = this.topOptions;
  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: i.term * 1 })),
  116. ['term'],
  117. ['asc']
  118. );
  119. this.$set(this, `termList`, tlist);
  120. if (termid) await this.initClass();
  121. }
  122. },
  123. getTermList(planid) {
  124. const plan = this.planList.find(f => f._id === planid);
  125. if (!plan) return;
  126. // 找到计划了
  127. const { termnum } = plan;
  128. const tlist = _.orderBy(
  129. termnum.map(i => ({ ...i, term: i.term * 1 })),
  130. ['term'],
  131. ['asc']
  132. );
  133. this.$set(this, `termList`, tlist);
  134. },
  135. async initClass() {
  136. const { termid, classid, planid } = this.topOptions;
  137. let res;
  138. // 检查身份
  139. let query = { termid };
  140. if (this.user.type == 1) {
  141. query.headteacherid = this.user.userid;
  142. res = await this.getClassList(query);
  143. } else if (this.user.type == 3) {
  144. res = await this.teaclass({ termid: termid, teaid: this.user.userid });
  145. }
  146. if (!res) return;
  147. if (this.$checkRes(res)) {
  148. if (_.isArray(res.data) && res.data.length > 0) {
  149. const d = res.data.filter(f => f.termid === termid);
  150. this.$set(this, `classList`, d);
  151. }
  152. }
  153. // 检查是否是初始化,如果是初始化(由上级来),需要检查classid是不是在这里,不在就清除掉
  154. const r = this.classList.find(f => f._id === classid);
  155. if (!r) this.toClear('classid');
  156. if (this.needInit) {
  157. this.checkToday();
  158. }
  159. },
  160. checkToday() {
  161. //教师/班主任,如果今天有班级选择
  162. for (const i of this.classList) {
  163. const { startdate, enddate } = i;
  164. const r = moment(moment().format('YYYY-MM-DD')).isBetween(startdate, enddate, null, '[]');
  165. if (r) {
  166. const { _id, termid } = i;
  167. this.$set(this.topOptions, `classid`, _id);
  168. break;
  169. }
  170. }
  171. sessionStorage.setItem('needInit', false);
  172. },
  173. selectClass(id) {
  174. sessionStorage.setItem('tempClassId', id);
  175. },
  176. useClassTemp() {
  177. const classid = sessionStorage.getItem('tempClassId');
  178. if (classid) this.$set(this.topOptions, `classid`, classid);
  179. },
  180. toClear() {
  181. this.$set(this.topOptions, `classid`, undefined);
  182. // this.$set(this, `topOptions`, _.omit(this.topOptions, type));
  183. },
  184. // setVuexOpt() {
  185. // console.log(this.topOptions);
  186. // this.changeOpt(this.topOptions);
  187. // },
  188. async settingSave() {
  189. let res = await this.sUpdate(this.topOptions);
  190. this.$checkRes(res, '设置成功', res.errmsg);
  191. },
  192. toRefresh() {
  193. this.$emit('refresh');
  194. this.checkCache();
  195. },
  196. },
  197. watch: {
  198. topOptions: {
  199. deep: true,
  200. handler(val, oval) {
  201. this.changeOpt(val);
  202. if (this.user.type === '1' || this.user.type === '3') this.useClassTemp();
  203. },
  204. },
  205. },
  206. computed: {
  207. ...mapState(['user', 'defaultOption']),
  208. ...mapGetters(['topOptions']),
  209. pageTitle() {
  210. return `${this.$route.meta.title}`;
  211. },
  212. needInit() {
  213. let needInit = sessionStorage.getItem('needInit');
  214. if (needInit === false) return true;
  215. else return !needInit;
  216. },
  217. },
  218. metaInfo() {
  219. return { title: this.$route.meta.title };
  220. },
  221. };
  222. </script>
  223. <style lang="less" scoped>
  224. .user-menu {
  225. height: 4rem;
  226. .el-col {
  227. margin-left: 10px;
  228. span {
  229. margin-left: 10px;
  230. }
  231. }
  232. }
  233. </style>