default-select.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. const d = res.data.filter(f => f.termid === termid);
  149. this.$set(this, `classList`, d);
  150. }
  151. // 检查是否是初始化,如果是初始化(由上级来),需要检查classid是不是在这里,不在就清除掉
  152. const r = this.classList.find(f => f._id === classid);
  153. if (!r) this.toClear('classid');
  154. if (this.needInit) {
  155. this.checkToday();
  156. }
  157. },
  158. checkToday() {
  159. //教师/班主任,如果今天有班级选择
  160. for (const i of this.classList) {
  161. const { startdate, enddate } = i;
  162. const r = moment(moment().format('YYYY-MM-DD')).isBetween(startdate, enddate, null, '[]');
  163. if (r) {
  164. const { _id, termid } = i;
  165. this.$set(this.topOptions, `classid`, _id);
  166. break;
  167. }
  168. }
  169. sessionStorage.setItem('needInit', false);
  170. },
  171. selectClass(id) {
  172. sessionStorage.setItem('tempClassId', id);
  173. },
  174. useClassTemp() {
  175. const classid = sessionStorage.getItem('tempClassId');
  176. if (classid) this.$set(this.topOptions, `classid`, classid);
  177. },
  178. toClear() {
  179. this.$set(this.topOptions, `classid`, undefined);
  180. // this.$set(this, `topOptions`, _.omit(this.topOptions, type));
  181. },
  182. // setVuexOpt() {
  183. // console.log(this.topOptions);
  184. // this.changeOpt(this.topOptions);
  185. // },
  186. async settingSave() {
  187. let res = await this.sUpdate(this.topOptions);
  188. this.$checkRes(res, '设置成功', res.errmsg);
  189. },
  190. toRefresh() {
  191. this.$emit('refresh');
  192. this.checkCache();
  193. },
  194. },
  195. watch: {
  196. topOptions: {
  197. deep: true,
  198. handler(val, oval) {
  199. this.changeOpt(val);
  200. if (this.user.type === '1' || this.user.type === '3') this.useClassTemp();
  201. },
  202. },
  203. },
  204. computed: {
  205. ...mapState(['user', 'defaultOption']),
  206. ...mapGetters(['topOptions']),
  207. pageTitle() {
  208. return `${this.$route.meta.title}`;
  209. },
  210. needInit() {
  211. let needInit = sessionStorage.getItem('needInit');
  212. if (needInit === false) return true;
  213. else return !needInit;
  214. },
  215. },
  216. metaInfo() {
  217. return { title: this.$route.meta.title };
  218. },
  219. };
  220. </script>
  221. <style lang="less" scoped>
  222. .user-menu {
  223. height: 4rem;
  224. .el-col {
  225. margin-left: 10px;
  226. span {
  227. margin-left: 10px;
  228. }
  229. }
  230. }
  231. </style>