1、在项目根目录下新建util目录。
2、在util目录下新建http.js文件,用于封装request请求。
// http.js页面
//#ifdef APP-PLUS
export const baseUrl = "https://miniapp.qhdan.com"
//#endif
//#ifdef H5
const BASE_URL = "/api";
export const baseUrl = BASE_URL
//#endif
const request = (url, method, data = {}, showLoading = false) => {
return new Promise((resolve, reject) => {
// const token = uni.getStorageSync('token')
const token = "9fecba8e-9b8d-43cf-a4af-af940f6cc123"
if (showLoading) {
uni.showLoading({
title: ""
})
}
let api = baseUrl + url
let tempData = data
uni.request({
url: api,
data: tempData,
method: method,
header: { //自定义请求头
'content-type': 'application/json',
'token': token
},
success: res => {
uni.hideLoading()
let code = res.data.code
if (code == 1) {
console.log('接口正常api', api)
console.log('接口正常入参', tempData)
console.log('接口正常返参', res)
let data = res.data.data;
re