多个请求并发,解决数据不固定的问题

2020-08-14  本文已影响0人  焚心123
//在methods中
  qiX(url,data){//前7天数据新增
            return new Promise(reslove=>{
                this.$axios.get(`https://elm.cangdu.org/statis/${url}/${data}/count`).then(res=>{
                    if(res.data.status==1){
                        reslove(res.data.count);
                    }
                })
            })
        },
   created(){
            const dateList = [...Array(7).keys()].map(days => {
            let t = new Date(Date.now() - 86400000 * days);
            let m=t.getMonth() + 1;
            let a=m<10?'0'+m:m;
            let str = `${t.getFullYear()}-${a}-${t.getDate()}`;
            // console.log('aa', srt);
            return str;
        });
            console.log(dateList);
            this.y=dateList.reverse();
            // this.y=dateList;//七天时间的整个数组
            this.timer();
    },
    async mounted(){
        this.$axios.all([this.axOne(),this.axTwo(),this.axThree(),this.axFour(),this.axFive(),
        this.axSix()])
        .then(this.$axios.spread((res1, res2,res3,res4,res5,res6)=>{
            // console.log(res1.data.count);
            this.one=res1.data.count;
            this.two=res2.data.count;
            this.three=res3.data.count;
            this.four=res4.data.count;
            this.five=res5.data.count;
            this.six=res6.data.count;
            // console.log(res2);
            // console.log(res3);
        }));

        //
        var p=[];
        var p1=[];
        var p2=[];
        for(var i=0;i<this.y.length;i++){
            var a=this.qiX('user',this.y[i]);//user接口的 七天时间
            p.push(a);
            var b=this.qiX('order',this.y[i]);//user接口的 七天时间
            p1.push(b);
             var c=this.qiX('admin',this.y[i]);//user接口的 七天时间
            p2.push(c);
        }
        Promise.all(p).then(res=>{
            console.log(res);
            this.xX=res;
        });
        Promise.all(p1).then(res=>{
            console.log(res);
            this.xD=res;
        });
        Promise.all(p2).then(res=>{
            console.log(res);
            this.xG=res;
        })
    },
上一篇 下一篇

猜你喜欢

热点阅读