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.
 
 
 
 

91 lines
2.7 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 userInfo = uni.getStorageSync('loginInfo') || {};
const UTCOffset = new Date().getTimezoneOffset();
return new Promise((resolve, reject) => {
uni.request({
url: BASE_URL + url, // 完整的URL
method: method,
data: data,
header: {
'AccessToken': userInfo.accessToken || '-1', // 请求头,可根据需要调整
'UserId': userInfo.userId || '1', // 请求头,可根据需要调整
'LanguageType': 0,
'LoginName': userInfo.loginName || '',
'CompanyId': userInfo.companyId || '',
'UTCOffset': UTCOffset,
},
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)
}
/* if (res.data.code === 500) {
uni.showToast({
title: '系统错误', // 提示的文本内容
icon: 'none', // 提示的图标,可选值:'success' | 'loading' | 'none'
duration: 2000 // 提示框的显示时间,单位为毫秒,默认1500ms
});
}
if (res.data.code === 401) {
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);
}
},
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);
}