request.js
import BASE_URL from "@/utils/env.js";
export const request = (options = {}) => {
return new Promise((resolve, reject) => {
uni.request({
url: BASE_URL + options.url,
timeout: 9000,
method: options.method || 'GET',
data: options.data || {},
header: options.header || {
'Content-Type': 'application/json'
},
success: (res) => {
if(res.statusCode !== 200 && res.statusCode !== 201){
return uni.showToast({
title:"请求失败! 请检查网络连接状态",
icon: "none"
})
};
resolve(res);
},
fail: (err) => {
uni.showToast({
icon: "none",
title: '请求失败!请检查网络连接状态'+ JSON.stringify(err),
content: err,
});
console.log(BASE_URL + options.url, err);
reject(err);
},
});
});
};
env.js
let BASE_URL
if (process.env.NODE_ENV === 'production') {
} else {
BASE_URL = 'yourURL';
}
export default BASE_URL
将api写成js文件
import {
request
} from "@/utils/request.js";
export const getCategories = (isvip) => {
return request({
url: 'dcgl/orderMeal/getZdLabel',
data:{
isvip:isvip
}
})
}
在使用页面调用
import {
getCategories,
} from '@/api/dc/dc.js';
getCategories() {
getCategories(this.isvip).then(res => {
this.categories = res.data.data;
})
},
onLoad():{
this.getCategories();
}