import { useLoginStore } from '@/store/login'

/**
 * 响应拦截
 */
export const responseInterceptors = () => {
  uni.$uv.http.interceptors.response.use(
    (response: any) => {
      /* 对响应成功做点什么 可使用async await 做异步操作*/
      return response.data
    },
    (response: any) => {
      if (response.statusCode === 401) {
        const loginStore = useLoginStore()
        loginStore.$reset()
        setTimeout(() => {
          uni.reLaunch({
            url: '/pages/login/index'
          })
        }, 500)
      }
      const data = response?.data
      if (data?.msg) {
        uni.showModal({
          title: data.msg,
          showCancel: false
        })
      }

      if (data?.code === 1) {
        setTimeout(() => {
          uni.hideLoading()
        }, 1000)
      }
      return Promise.reject(response)
    }
  )
}
