lrf402788946 4 years ago
parent
commit
dedf738328
1 changed files with 92 additions and 129 deletions
  1. 92 129
      layout/admin/navBar/default-select.vue

+ 92 - 129
layout/admin/navBar/default-select.vue

@@ -4,35 +4,33 @@
       <el-col :span="15" v-if="options">
         当前默认:
         <span>
+          <!--  @change="data => changeList('plan', data)" -->
           <el-tooltip :disabled="this.user.type != 0" content="点击更改默认批次" effect="dark" placement="bottom">
-            <el-select
-              v-model="options.planyearid"
-              :disabled="this.user.type != 0"
-              placeholder="未设置培训计划"
-              size="mini"
-              @change="data => changeList('plan', data)"
-            >
+            <el-select v-model="options.planyearid" :disabled="this.user.type != 0" placeholder="未设置培训计划" size="mini" @change="initPlan">
               <el-option v-for="(i, index) in planYearList" :key="index" :label="i.title" :value="i._id"></el-option>
             </el-select>
           </el-tooltip>
         </span>
         <span>
+          <!-- @change="getTermList" -->
           <el-tooltip :disabled="this.user.type != 0" content="点击更改默认年度计划" effect="dark" placement="bottom">
-            <el-select v-model="options.planid" :disabled="this.user.type != 0" placeholder="未设置年度计划" size="mini" @change="getTermList">
+            <el-select v-model="options.planid" :disabled="this.user.type != 0" placeholder="未设置年度计划" size="mini">
               <el-option v-for="(i, index) in planList" :key="index" :label="i.title" :value="i._id"></el-option>
             </el-select>
           </el-tooltip>
         </span>
         <span>
+          <!--  @change="toCheckUserType()" -->
           <el-tooltip content="点击更改默认期" effect="dark" placement="bottom">
-            <el-select v-model="options.termid" placeholder="未设置默认期" size="mini" @change="toCheckUserType()">
+            <el-select v-model="options.termid" placeholder="未设置默认期" size="mini" @change="initClass">
               <el-option v-for="(i, index) in termList" :key="index" :label="`第${i.term}期`" :value="i._id"></el-option>
             </el-select>
           </el-tooltip>
         </span>
         <span v-if="user.type == 1 || user.type == 3">
+          <!--  @change="setVuexOpt()" -->
           <el-tooltip content="选择要查看的班级" effect="dark" placement="bottom">
-            <el-select v-model="options.classid" placeholder="选择要查看的班级" size="mini" @change="setVuexOpt()">
+            <el-select v-model="options.classid" placeholder="选择要查看的班级" size="mini">
               <el-option v-for="(i, index) in classList" :key="index" :label="i.name" :value="i._id"></el-option>
             </el-select>
           </el-tooltip>
@@ -60,7 +58,7 @@ export default {
   components: {},
   data: function() {
     return {
-      loading: true,
+      loading: false,
       options: undefined,
       planYearList: [],
       planList: [],
@@ -69,149 +67,113 @@ export default {
     };
   },
   async created() {
-    // this.$set(this, `options`, _.cloneDeep(this.defaultOption));
-    this.search({ type: 'planYear' });
-    // this.checkOption();
-    // if (this.user.type == 1 || this.user.type == 3) this.getClassOption();
-    // this.loading = false;
+    if (this.needInit) {
+      const cache = sessionStorage.getItem('defaultOption');
+      if (cache) this.$set(this, `options`, JSON.parse(cache));
+    } else {
+      this.$set(this, `options`, _.cloneDeep(this.defaultOption));
+    }
+    await this.init({ type: 'planYear' });
+    this.loading = false;
   },
   methods: {
     ...mapMutations(['deleteUser', 'changeOpt']),
     ...trainBatch({ getplanYear: 'query' }),
-    ...trainplan({ getplan: 'query' }),
+    ...trainplan({ getPlan: 'query' }),
     ...setting({ sFetch: 'fetch', sUpdate: 'update' }),
     ...classes({ getClassList: 'query' }), //仅供班主任使用
     ...lesson({ teaclass: 'teaclass' }), //仅供老师使用
-    checkOption() {
-      if (_.get(this.options, 'planid')) {
-        this.search({ type: 'plan', planyearid: _.get(this.options, 'planyearid') });
-      }
-      if (this.user.type == 1 || this.user.type == 3) this.getClassOption();
-      this.loading = false;
+
+    async init() {
+      let res = await this.getplanYear();
+      if (this.$checkRes(res)) this.$set(this, `planYearList`, res.data);
+      const { planyearid } = this.options;
+      if (planyearid) this.initPlan();
     },
-    async search({ type, ...info }) {
-      let res = await _.get(this, `get${type}`)({ ...info });
+    async initPlan() {
+      const { planyearid, planid, termid } = this.options;
+      if (!planyearid) return;
+      const res = await this.getPlan({ planyearid });
       if (this.$checkRes(res)) {
-        this.$set(this, `${type}List`, res.data);
-        if (type == 'plan') {
-          let planid = _.get(this.options, 'planid');
-          this.getTermList(planid);
+        this.$set(this, `planList`, res.data);
+        // 没有有计划id,清空后面的值
+        if (!planid) {
+          this.toClear('planid');
+          this.toClear('termid');
+          this.toClear('classid');
+          return;
+        }
+        const plan = res.data.find(f => f._id == planid);
+        // 现在的计划id没有找对应的计划,清空后面的值
+        if (!plan) {
+          this.toClear('planid');
+          this.toClear('termid');
+          this.toClear('classid');
+          return;
         }
+        // 找到计划了
+        const { termnum } = plan;
+        const tlist = _.orderBy(
+          termnum.map(i => ({ ...i, term: parseInt(i.term) })),
+          ['term'],
+          ['asc']
+        );
+        this.$set(this, `termList`, tlist);
+        if (termid) this.initClass('init');
       }
     },
-    async changeList(type, data) {
-      let obj = { type };
+    async initClass(type) {
+      const { termid, classid, planid } = this.options;
       let res;
-      this.toClear();
-      this.setVuexOpt();
-      if (type == 'plan') {
-        obj[`planyearid`] = data;
-        await this.search(obj);
+      // 检查身份
+      let query = { termid };
+      if (this.user.type == 1) {
+        query.headteacherid = this.user.userid;
+        res = await this.getClassList(query);
+      } else if (this.user.type == 3) {
+        res = await this.teaclass({ planid: planid, teaid: this.user.userid });
       }
-    },
-    getTermList(planid) {
-      let r = this.planList.find(f => f.id == planid);
-      if (r) {
-        let term = _.get(r, 'termnum', []);
-        this.$set(this, `termList`, term);
+      if (!res) return;
+      if (this.$checkRes(res)) this.$set(this, `classList`, res.data);
+      // 检查是否是初始化,如果是初始化(由上级来),需要检查classid是不是在这里,不在就清除掉
+      const r = this.classList.find(f => f._id === classid);
+      if (!r) this.toClear('classid');
+      if (this.needInit) {
+        this.checkToday();
       }
-      this.setVuexOpt();
     },
-    //为了处理班主任的班级列表,先复制,再看看是不是需要查询班级列表
-    toCheckUserType() {
-      this.setVuexOpt();
-      this.options.classid = undefined;
-      if (this.user.type == 1 || this.user.type == 3) this.getClassOption();
-    },
-    setVuexOpt() {
-      this.changeOpt(this.options);
-    },
-    toClear() {
-      let planid = _.get(this.options, 'planid');
-      if (planid) {
-        let res = this.planList.find(f => f.id == planid);
-        if (!res) this.$set(this.options, 'planid', undefined);
-      }
-      let termid = _.get(this.options, 'termid');
-      if (termid) {
-        let res = this.planList.find(f => f.id == planid);
-        if (!res) this.$set(this.options, 'termid', undefined);
+    checkToday() {
+      //教师/班主任,如果今天有班级选择
+      for (const i of this.classList) {
+        const { startdate, enddate } = i;
+        const r = moment().isBetween(startdate, enddate, null, '[]');
+        if (r) {
+          const { _id, termid } = i;
+          let options = _.cloneDeep(this.options);
+          options.classid = _id;
+          this.$set(this, `options`, options);
+          break;
+        }
       }
+      sessionStorage.setItem('needInit', false);
+    },
+    toClear(type) {
+      this.$set(this, `options`, _.omit(this.options, type));
     },
+    // setVuexOpt() {
+    //   console.log(this.options);
+    //   this.changeOpt(this.options);
+    // },
     async settingSave() {
       let res = await this.sUpdate(this.options);
       this.$checkRes(res, '设置成功', res.errmsg);
     },
-    //班主任使用,默认班级
-    async getClassOption(needReturn = true) {
-      let termid = _.get(this.defaultOption, 'termid');
-      let planid = _.get(this.defaultOption, 'planid');
-      if (!termid) return;
-      let res;
-      let orginList = [];
-      if (this.user.type == 1) {
-        res = await this.getClassList({ planid: planid, headteacherid: this.user.userid });
-        if (this.$checkRes(res)) {
-          orginList = res.data;
-          this.$set(
-            this,
-            `classList`,
-            res.data.filter(f => f.termid == termid)
-          );
-        }
-      }
-      if (this.user.type == 3) {
-        res = await this.teaclass({ planid: planid, teaid: this.user.userid });
-        orginList = res.data;
-        const elm = res.data.filter(item => item.termid == termid);
-        this.$set(this, `classList`, elm);
-      }
-      //班级数据正确验证
-      let options = _.cloneDeep(this.options);
-      if (this.classList.length <= 0) {
-        delete options.classid;
-        this.$set(this, `options`, options);
-      } else {
-        let res = await this.classList.find(f => f._id === options.classid);
-        if (!res) {
-          delete options.classid;
-          this.$set(this, `options`, options);
-        }
-      }
-      if (this.needInit) {
-        //教师/班主任,如果今天有班级选择
-        for (const i of orginList) {
-          const { startdate, enddate } = i;
-          const r = moment().isBetween(startdate, enddate, null, '[]');
-          if (r) {
-            const { _id, termid } = i;
-            let options = _.cloneDeep(this.options);
-            options.classid = _id;
-            options.termid = termid;
-            this.$set(this, `options`, options);
-            break;
-          }
-        }
-        sessionStorage.setItem('init', true);
-      }
-    },
   },
   watch: {
-    defaultOption: {
-      handler(val) {
-        if (val) {
-          let opt = _.get(this, `options`);
-          if (!opt) this.$set(this, `options`, _.cloneDeep(val));
-        }
-      },
-      immediate: true,
-      deep: true,
-    },
     options: {
-      immediate: true,
       deep: true,
       handler(val, oval) {
-        if (!_.isEqual(val, oval) && val) this.checkOption();
+        this.changeOpt(val);
       },
     },
   },
@@ -221,8 +183,9 @@ export default {
       return `${this.$route.meta.title}`;
     },
     needInit() {
-      let needInit = sessionStorage.getItem('init');
-      return !needInit;
+      let needInit = sessionStorage.getItem('needInit');
+      if (needInit === false) return true;
+      else return !needInit;
     },
   },
   metaInfo() {