/**
 * 响应拦截
 * @param {Object} http
 */
module.exports = vm => {
  uni.$u.http.interceptors.response.use(
    response => {
      const data = response.data
      // 刷新token
      const authorization = response.headers?.authorization
      if (authorization) {
        vm.$store.dispatch('user/token', authorization)
      }

      if (data.code !== 200) {
        uni.showModal({
          title: '提示',
          content: data.message,
          showCancel: false
        })
        // 401 登录超时 402 需要登录
        if (data.code === 401 || data.code === 402) {
          uni.reLaunch({
            url: 'pages/index/index'
          })
        }
        return Promise.reject(data.message)
      }

      return data
    }, (error) => {
      console.error('-->error', error)
      uni.showModal({
        title: '提示',
        content: error.message,
        showCancel: false
      })
      return Promise.reject(error)
    })
}
