default-select.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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-if="options">
  5. 当前默认:
  6. <span>
  7. <el-tooltip :disabled="this.user.type != 0" content="点击更改默认批次" effect="dark" placement="bottom">
  8. <el-select
  9. v-model="options.planyearid"
  10. :disabled="this.user.type != 0"
  11. placeholder="未设置培训计划"
  12. size="mini"
  13. @change="data => changeList('plan', data)"
  14. >
  15. <el-option v-for="(i, index) in planYearList" :key="index" :label="i.title" :value="i._id"></el-option>
  16. </el-select>
  17. </el-tooltip>
  18. </span>
  19. <span>
  20. <el-tooltip :disabled="this.user.type != 0" content="点击更改默认年度计划" effect="dark" placement="bottom">
  21. <el-select v-model="options.planid" :disabled="this.user.type != 0" placeholder="未设置年度计划" size="mini" @change="getTermList">
  22. <el-option v-for="(i, index) in planList" :key="index" :label="i.title" :value="i._id"></el-option>
  23. </el-select>
  24. </el-tooltip>
  25. </span>
  26. <span>
  27. <el-tooltip content="点击更改默认期" effect="dark" placement="bottom">
  28. <el-select v-model="options.termid" placeholder="未设置默认期" size="mini" @change="toCheckUserType()">
  29. <el-option v-for="(i, index) in termList" :key="index" :label="`第${i.term}期`" :value="i._id"></el-option>
  30. </el-select>
  31. </el-tooltip>
  32. </span>
  33. <span v-if="user.type == 1 || user.type == 3">
  34. <el-tooltip content="选择要查看的班级" effect="dark" placement="bottom">
  35. <el-select v-model="options.classid" placeholder="选择要查看的班级" size="mini" @change="setVuexOpt()">
  36. <el-option v-for="(i, index) in classList" :key="index" :label="i.name" :value="i._id"></el-option>
  37. </el-select>
  38. </el-tooltip>
  39. </span>
  40. <span>
  41. <el-button type="text" size="mini" @click="settingSave" v-if="this.user.type == 0">保存默认值</el-button>
  42. </span>
  43. </el-col>
  44. </el-row>
  45. </div>
  46. </template>
  47. <script>
  48. import _ from 'lodash';
  49. const moment = require('moment');
  50. import { mapState, mapMutations, createNamespacedHelpers } from 'vuex';
  51. const { mapActions: trainBatch } = createNamespacedHelpers('trainBatch');
  52. const { mapActions: trainplan } = createNamespacedHelpers('trainplan');
  53. const { mapActions: setting } = createNamespacedHelpers('setting');
  54. const { mapActions: classes } = createNamespacedHelpers('classes');
  55. const { mapActions: lesson } = createNamespacedHelpers('lesson');
  56. export default {
  57. name: 'default-select',
  58. props: {},
  59. components: {},
  60. data: function() {
  61. return {
  62. loading: true,
  63. options: undefined,
  64. planYearList: [],
  65. planList: [],
  66. termList: [],
  67. classList: [],
  68. };
  69. },
  70. async created() {
  71. // this.$set(this, `options`, _.cloneDeep(this.defaultOption));
  72. this.search({ type: 'planYear' });
  73. // this.checkOption();
  74. // if (this.user.type == 1 || this.user.type == 3) this.getClassOption();
  75. // this.loading = false;
  76. },
  77. methods: {
  78. ...mapMutations(['deleteUser', 'changeOpt']),
  79. ...trainBatch({ getplanYear: 'query' }),
  80. ...trainplan({ getplan: 'query' }),
  81. ...setting({ sFetch: 'fetch', sUpdate: 'update' }),
  82. ...classes({ getClassList: 'query' }), //仅供班主任使用
  83. ...lesson({ teaclass: 'teaclass' }), //仅供老师使用
  84. checkOption() {
  85. if (_.get(this.options, 'planid')) {
  86. this.search({ type: 'plan', planyearid: _.get(this.options, 'planyearid') });
  87. }
  88. if (this.user.type == 1 || this.user.type == 3) this.getClassOption();
  89. this.loading = false;
  90. },
  91. async search({ type, ...info }) {
  92. let res = await _.get(this, `get${type}`)({ ...info });
  93. if (this.$checkRes(res)) {
  94. this.$set(this, `${type}List`, res.data);
  95. if (type == 'plan') {
  96. let planid = _.get(this.options, 'planid');
  97. this.getTermList(planid);
  98. }
  99. }
  100. },
  101. async changeList(type, data) {
  102. let obj = { type };
  103. let res;
  104. this.toClear();
  105. this.setVuexOpt();
  106. if (type == 'plan') {
  107. obj[`planyearid`] = data;
  108. await this.search(obj);
  109. }
  110. },
  111. getTermList(planid) {
  112. let r = this.planList.find(f => f.id == planid);
  113. if (r) {
  114. let term = _.get(r, 'termnum', []);
  115. this.$set(this, `termList`, term);
  116. }
  117. this.setVuexOpt();
  118. },
  119. //为了处理班主任的班级列表,先复制,再看看是不是需要查询班级列表
  120. toCheckUserType() {
  121. this.setVuexOpt();
  122. this.options.classid = undefined;
  123. if (this.user.type == 1 || this.user.type == 3) this.getClassOption();
  124. },
  125. setVuexOpt() {
  126. this.changeOpt(this.options);
  127. },
  128. toClear() {
  129. let planid = _.get(this.options, 'planid');
  130. if (planid) {
  131. let res = this.planList.find(f => f.id == planid);
  132. if (!res) this.$set(this.options, 'planid', undefined);
  133. }
  134. let termid = _.get(this.options, 'termid');
  135. if (termid) {
  136. let res = this.planList.find(f => f.id == planid);
  137. if (!res) this.$set(this.options, 'termid', undefined);
  138. }
  139. },
  140. async settingSave() {
  141. let res = await this.sUpdate(this.options);
  142. this.$checkRes(res, '设置成功', res.errmsg);
  143. },
  144. //班主任使用,默认班级
  145. async getClassOption(needReturn = true) {
  146. let termid = _.get(this.defaultOption, 'termid');
  147. let planid = _.get(this.defaultOption, 'planid');
  148. if (!termid) return;
  149. let res;
  150. let orginList = [];
  151. if (this.user.type == 1) {
  152. res = await this.getClassList({ planid: planid, headteacherid: this.user.userid });
  153. if (this.$checkRes(res)) {
  154. orginList = res.data;
  155. this.$set(
  156. this,
  157. `classList`,
  158. res.data.filter(f => f.termid == termid)
  159. );
  160. }
  161. }
  162. if (this.user.type == 3) {
  163. res = await this.teaclass({ planid: planid, teaid: this.user.userid });
  164. orginList = res.data;
  165. const elm = res.data.filter(item => item.termid == termid);
  166. this.$set(this, `classList`, elm);
  167. }
  168. //班级数据正确验证
  169. let options = _.cloneDeep(this.options);
  170. if (this.classList.length <= 0) {
  171. delete options.classid;
  172. this.$set(this, `options`, options);
  173. } else {
  174. let res = await this.classList.find(f => f._id === options.classid);
  175. if (!res) {
  176. delete options.classid;
  177. this.$set(this, `options`, options);
  178. }
  179. }
  180. if (this.needInit) {
  181. //教师/班主任,如果今天有班级选择
  182. for (const i of orginList) {
  183. const { startdate, enddate } = i;
  184. const r = moment().isBetween(startdate, enddate, null, '[]');
  185. if (r) {
  186. const { _id, termid } = i;
  187. let options = _.cloneDeep(this.options);
  188. options.classid = _id;
  189. options.termid = termid;
  190. this.$set(this, `options`, options);
  191. break;
  192. }
  193. }
  194. sessionStorage.setItem('init', true);
  195. }
  196. },
  197. },
  198. watch: {
  199. defaultOption: {
  200. handler(val) {
  201. if (val) {
  202. let opt = _.get(this, `options`);
  203. if (!opt) this.$set(this, `options`, _.cloneDeep(val));
  204. }
  205. },
  206. immediate: true,
  207. deep: true,
  208. },
  209. options: {
  210. immediate: true,
  211. deep: true,
  212. handler(val, oval) {
  213. if (!_.isEqual(val, oval) && val) this.checkOption();
  214. },
  215. },
  216. },
  217. computed: {
  218. ...mapState(['user', 'defaultOption']),
  219. pageTitle() {
  220. return `${this.$route.meta.title}`;
  221. },
  222. needInit() {
  223. let needInit = sessionStorage.getItem('init');
  224. return !needInit;
  225. },
  226. },
  227. metaInfo() {
  228. return { title: this.$route.meta.title };
  229. },
  230. };
  231. </script>
  232. <style lang="less" scoped>
  233. .user-menu {
  234. height: 4rem;
  235. .el-col {
  236. margin-left: 10px;
  237. span {
  238. margin-left: 10px;
  239. }
  240. }
  241. }
  242. </style>