一、新建文件夾及文件

二、request.js文件內封裝全局請求
const token = uni.getStorage('token') // 獲取token值
const baseUrl = "http://**.***.***.***:5000"
export default(url, method, params) => { // 傳參: 接口url, method類型, params參數
return new Promise((resolve, reject) => {
uni.request({
method: method,
url: baseUrl + url,
data: params,
header: {
token: token
}
})
.then((response) => {
// 請求成功后的處理
if (response.data.retCode === 0) {
resolve(response.data.strResult) // 返回內容根據后端返回數據決定
} else {
console.log('請求數據不存在')
}
}).catch((reject) => {
// 請求失敗后的處理
console.log('請求失敗')
})
})
}
三、api.js封裝各個接口

四、main.js引入api文件

五、接口調用

調用格式:
this.$api.接口名稱(parmas參數).then().catch()
