/**
 * 请求拦截
 */
import qs from 'qs'

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

export const requestInterceptors = () => {
  uni.$uv.http.interceptors.request.use(
    (config: any) => {
      const loginStore = useLoginStore()
      config.data = config.data || {}
      if (config.url.includes('auth/mobile/token/sms')) {
        config.header['Authorization'] = 'Basic cGlnOnBpZw=='
      } else if (config.url.includes('auth/oauth/token')) {
        config.header['Authorization'] = 'Basic dGVzdDp0ZXN0'
      } else {
        //如果登录的话
        const accessToken = loginStore.accessToken
        config.header['Authorization'] = `Bearer ${accessToken}`
      }
      return config
    },
    (
      config: any // 可使用async await 做异步操作
    ) => Promise.reject(config)
  )
}
