import { useUserStore } from "@/store"

const TOKEN = 'TOKEN'

interface Params {
  params?: any,
  url?: string,
  method: Request.Methods
  responseType?: '',
  baseURL?: string
}

// const BASEURL = import.meta.env.MODE === 'development' ? 'http://local.luojigou.vip:8888' : 'https://open.api.luojigou.vip'
const BASEURL = import.meta.env.MODE === 'development' ? 'https://open.api.luojigou.vip' : 'https://open.api.luojigou.vip'
//  'https://open.test.luojigou.vip'
// window.location.href.split('#')[0] + 'zd-api'
// https://open.test.luojigou.vip
//  "https://open.api.luojigou.vip"

export const request = async <T>(
  params: Params
) => {

  let data = params.params || {};

  console.log("data:", data);
  
  let url = (params.baseURL ? params.baseURL: BASEURL) + params.url;
  let method = params.method
  let responseType = params.responseType

  let token = useUserStore().token

  const response = await new Promise(resolve => {
     uni.request({
      url: url,
      method: method,
      data: data,
      header: {
        'content-type': 'application/json',
        token
      },
      responseType,
      success: res => resolve(res.data)
    })
  })

  return response as API.Response<T>
}


