<template>
  <div>
    <button @click="main('admin')">管理员查询</button> 
    <button @click="main('')">参数缺失</button> 
    <button @click="main('zs')">ZS</button> 
    <div v-for="item in items">
      {{ item }}
    </div>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data() {
    return {
      items: []
    }
  },
  mounted() {
    this.main()
  },
  methods: {

    async main(param) {
      this.items = []
      try {
        let user = await this.userInfo(param)
        let order = await this.orderInfo(user);
        this.items.push(new Date().toLocaleString() + ' ' + order)
      } catch (err) {
        //这里处理reject和error信息
        this.items.push(typeof err === 'object' ? err.message : err)
      }
    },

    userInfo(roleName) {
      return new Promise((resolve, reject) => {
        this.items.push(new Date().toLocaleString() + ' 查询用户信息...')

        if (!roleName) {
          // 这里模拟抛出异常
          throw new Error('参数为空,查询异常')
        } else if (roleName === 'zs') {
          //拒绝标识
          return reject('无权查询')
        }

        //这里我们模拟异步请求, 等待三秒
        setTimeout(() => {
          resolve('王二')
        }, 3000)
      })
    },
    orderInfo(userName) {
      return new Promise((resolve, reject) => {
        this.items.push(new Date().toLocaleString() + ` 查询用户${userName}的订单信息...`)
        resolve('ORDER_20230820000000001')
      })
    }
  }
}
</script>

<style scoped>
* {
  font-size: 12px
}
</style>
