promiseAll使用

    async fnSubmitTask() {
      const _selectedRowKeys = [...new Set(this.selectedRowKeys)]
      const _notSelectedKeys = [...new Set(this.notSelectedKeys)]
      // console.log('_selectedRowKeys', _selectedRowKeys)
      // console.log('_notSelectedKeys', _notSelectedKeys)
      // // 选中的审核不通过 @1:审核通过,@2:审核不通过
      // if (_selectedRowKeys.length) {
      //   const params = {
      //     review_ids: _selectedRowKeys.join(','),
      //     deal_status: 2
      //   }
      //   this.$api.audit.submit(params)
      //     .then(res => {
      //       this.$message.success('提交成功')
      //       this.pagination.current = 1
      //       this.getImageList()
      //     })
      //     .catch(() => {})
      // }
      // // 未选中的通过
      // if (_notSelectedKeys.length) {
      //   const params = {
      //     review_ids: _notSelectedKeys.join(','),
      //     deal_status: 1
      //   }
      //   this.$api.audit.submit(params)
      //     .then(res => {
      //       this.$message.success(res.msg)
      //       this.pagination.current = 1
      //       this.getImageList()
      //     })
      //     .catch(() => {})
      // }
      const promiseGroup = []
      if (_selectedRowKeys.length) {
        this.loading = true
        const params = {
          review_ids: _selectedRowKeys.join(','),
          deal_status: 2,
        }
        const promiseAdopt = await this.$api.audit.submit(params)
        promiseGroup.push(promiseAdopt)
      }
      if (_notSelectedKeys.length) {
        this.loading = true
        const params = {
          review_ids: _notSelectedKeys.join(','),
          deal_status: 1,
        }
        const promiseReject = await this.$api.audit.submit(params)
        promiseGroup.push(promiseReject)
      }
      Promise.all(promiseGroup)
        .then((res) => {
          this.loading = false
          if (promiseGroup.length) {
            this.pagination.current = 1
            this.getImageList()
          }
        })
        .catch((err) => {
          this.loading = false
          this.$message.error(err)
        })
        .finally(() => {
          this.loading = false
        })
    },

 写法二:

    async fnSubmitTask () {
      const _selectedRowKeys = [...new Set(this.selectedRowKeys)]
      const _notSelectedKeys = [...new Set(this.notSelectedKeys)]
      let code1 = 0
      let code2 = 0
      let result1 = null
      let result2 = null
      let req1 = false
      let req2 = false

      const res1 = () =>
        new Promise((resolve, reject) => {
          if (_selectedRowKeys.length > 0) {
            req1 = true
            const params = {
              material_ids: _selectedRowKeys,
              deal_status: 2
            }
            this.$api.audit
              .dealMaterialOrder(params)
              .then(res => {
                result1 = res
                code1 = result1.code
                resolve()
                // this.getMaterialOrderList()
              })
              .catch(() => {})
          } else {
            resolve()
          }
        })
      const res2 = () =>
        new Promise((resolve, reject) => {
          if (_notSelectedKeys.length > 0) {
            req2 = true
            const params = {
              material_ids: _notSelectedKeys,
              deal_status: 1
            }
            this.$api.audit
              .dealMaterialOrder(params)
              .then(res => {
                result2 = res
                code2 = result2.code
                resolve()
                // this.getMaterialOrderList()
              })
              .catch(() => {})
          } else {
            resolve()
          }
        })

      await Promise.all([res1(), res2()])
      if (code1 === 0 && code2 === 0) {
        if (req1 && req2) {
          this.$message.success(result2.msg)
        } else if (!req1 && req2) {
          this.$message.success(result2.msg)
        } else if (!req2 && req1) {
          this.$message.success(result1.msg)
        }
        if (req1 || req2) {
          if (this.pagination.current !== 1) {
            this.pagination.current = 1
          } else {
            this.getMaterialOrderList()
          }
        }
      } else {
        if (code1 !== 0) {
          this.$message.error(result1.msg)
        } else if (code2 !== 0) {
          this.$message.error(result2.msg)
        }
      }
    },

 

posted @ 2020-08-11 10:55  hjswlqd  阅读(218)  评论(0)    收藏  举报