天天看点

多级嵌套请求

要求以

省 > 市 > 区 > 终端

的顺序获取终端数据

getAllProvince() {
    return new Promise((resolve) => {
      this.http.get(this.rootUrl + '/place/getprovince').subscribe((data: any) => {
        resolve(data)
      })
    })
  }

  // Promise.all + map 循环 + Promise 请求循环数据
  getAllCity(province) {
    return Promise.all(
      province.map(province => {
        return new Promise(resolve => {
          this.http.get(this.rootUrl + '/place/getcity', {
            params: {
              pId: province.id,
              teidstr: this.teids
            },
          }).subscribe((data: any) => {
            resolve(data)
          })
        })
      })
    )
  }

  getAllArea(city) {
    return Promise.all(
      city.map(city => {
        return new Promise(resolve => {
          this.http.get(this.rootUrl + '/place/getarea', {
            params: {
              pId: city.pId,
              cId: city.id,
              teidstr: this.teids
            },
          }).subscribe((data: any) => {
            resolve(data)
          })
        })
      })
    )
  }

  getAllTeminal(area) {
    return Promise.all(
      area.map(area => {
        return new Promise(resolve => {
          this.http.get(this.rootUrl + '/terminal/getall', {
            params: {
              pId: area.pId,
              cId: area.cId,
              aId: area.id,
              teidstr: this.teids
            },
          }).subscribe((data: any) => {
            resolve(data)
          })
        })
      })
    )
  }

  async getAll(callback) {
    let province: any = await this.getAllProvince() // 省
    let city: any = await this.getAllCity(province) // 市
    let area: any = await this.getAllArea(...city) // 区
    let teminal: any = await this.getAllTeminal(...area) // 终端

    callback(province, city, area, teminal)
  }
           

继续阅读