You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

114 lines
3.3 KiB

// api.js
// 基础URL,可以根据环境来设置
// uEnvDev
const accountInfo = wx.getAccountInfoSync();
// env类型 develop:开发版、trial:体验版、release:正式版
export const env = accountInfo.miniProgram.envVersion;
if (!env) {
console.error("获取运行环境失败!");
}
const baseApi = {
// 开发版
develop: "http://110.40.156.216:30005/api",
// 体验版
trial: "http://110.40.156.216:30005/api",
// 正式版
release: "http://110.40.156.216:30005/api"
};
console.log(env, 'env')
// request请求baseURL
const BASE_URL = baseApi[env] || 'http://110.40.156.216:30005/api';
function request(url, method, data = {}) {
const loginRes = uni.getStorageSync('loginRes') || {};
const UTCOffset = new Date().getTimezoneOffset();
return new Promise((resolve, reject) => {
uni.request({
url: BASE_URL + url, // 完整的URL
method: method,
data: data,
header: {
'AccessToken': loginRes.accessToken || '-1', // 请求头,可根据需要调整
'UserId': loginRes.appUserId || '-1', // 请求头,可根据需要调整
'LanguageType': 0,
},
success: (res) => {
if (res.statusCode === 200) {
console.log(res)
if (res.data.code === 200 | res.data.code === 0) {
resolve(res.data)
} else {
reject(res.data)
}
} else {
reject(res);
}
},
fail: (err) => {
uni.showToast({
title: '请求错误', // 提示的文本内容
icon: 'none', // 提示的图标,可选值:'success' | 'loading' | 'none'
duration: 2000 // 提示框的显示时间,单位为毫秒,默认1500ms
});
reject(err); // 请求失败
}
});
});
}
// GET 请求
export function get(url, params = {}) {
return request(url, 'GET', params);
}
// POST 请求
export function post(url, data = {}) {
return request(url, 'POST', data);
}
export function uploadFile(url, filePath = '', data = {}) {
const loginRes = uni.getStorageSync('loginRes') || {};
return new Promise((resolve, reject) => {
uni.uploadFile({
url: BASE_URL + url, // 完整的URL
filePath: filePath,
name: 'image',
header: {
'AccessToken': loginRes.accessToken || '-1', // 请求头,可根据需要调整
'UserId': loginRes.appUserId || '-1', // 请求头,可根据需要调整
LanguageType: 0,
// 其他需要的请求头信息(如Token)
},
formData: data,
success: (res) => {
if (res.statusCode === 200) {
if (res.data.code === 500) {
uni.showToast({
title: '系统错误', // 提示的文本内容
icon: 'none', // 提示的图标,可选值:'success' | 'loading' | 'none'
duration: 2000 // 提示框的显示时间,单位为毫秒,默认1500ms
});
}
if (res.data.code === 20001) {
reject(res.data);
}
resolve(res.data); // 返回数据
} else {
reject(res.data); // 返回错误信息
}
},
fail: (err) => {
uni.showToast({
title: '请求错误', // 提示的文本内容
icon: 'none', // 提示的图标,可选值:'success' | 'loading' | 'none'
duration: 2000 // 提示框的显示时间,单位为毫秒,默认1500ms
});
reject(err); // 请求失败
}
});
})
}