/**
 * 响应拦截
 * @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 !== 0) {
      //   uni.showModal({
      //     title: '提示',
      //     content: data.msg,
      //     showCancel: false
      //   })
      //   return Promise.reject(data.msg)
      // }

      return data
    }, (error) => {
      // 401 登录超时 402 需要登录
      if (typeof error.data.status_code !== 'undefined') {
        if (error.data.status_code === 401 || error.data.status_code === 402) {
          if (!getApp().globalData.isLogin) {
            getApp().globalData.isLogin = true
            uni.reLaunch({
              url: '/pages/login'
            })
          }
          return Promise.resolve()
        }
      }
      uni.showModal({
        title: '提示',
        content: error.data.message,
        showCancel: false
      })
      return Promise.reject(error)
    })
}
