| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- // API工具类
- // 开发环境:使用 localhost
- // 生产环境:需要配置为 HTTPS 域名
- const BASE_URL = 'http://localhost:8080/api';
- // 开发环境提示
- if (BASE_URL.includes('localhost')) {
- console.warn('当前使用 localhost,请在微信开发者工具中关闭域名校验:详情 -> 本地设置 -> 不校验合法域名');
- }
- // 请求封装
- function request(url, method = 'GET', data = {}, needAuth = true) {
- return new Promise((resolve, reject) => {
- // 获取token
- const token = wx.getStorageSync('token') || '';
-
- // 构建请求头
- const header = {
- 'Content-Type': 'application/json'
- };
-
- if (needAuth && token) {
- header['Authorization'] = `Bearer ${token}`;
- }
-
- wx.request({
- url: BASE_URL + url,
- method: method,
- data: data,
- header: header,
- success: (res) => {
- console.log('API请求成功:', {
- url: BASE_URL + url,
- statusCode: res.statusCode,
- data: res.data
- });
-
- if (res.statusCode === 200) {
- if (res.data.code === 200) {
- resolve(res.data.data);
- } else {
- console.error('API返回错误:', {
- code: res.data.code,
- message: res.data.message || res.data.msg
- });
- // token过期或未授权
- if (res.data.code === 401 || res.statusCode === 401) {
- wx.removeStorageSync('token');
- wx.removeStorageSync('userInfo');
- wx.reLaunch({
- url: '/pages/login/login'
- });
- }
- // 403禁止访问(如管理员尝试登录小程序)
- if (res.data.code === 403 || res.statusCode === 403) {
- // 不跳转,只显示错误信息
- }
- reject(res.data.message || res.data.msg || '请求失败');
- }
- } else if (res.statusCode === 400) {
- // 400错误通常是参数问题
- console.error('请求参数错误:', res.data);
- reject(res.data.message || res.data.msg || '请求参数错误');
- } else if (res.statusCode === 403) {
- // 403禁止访问
- console.error('访问被禁止:', res.data);
- reject(res.data.message || res.data.msg || '访问被禁止');
- } else {
- console.error('HTTP错误:', res.statusCode, res.data);
- reject('网络错误: ' + res.statusCode + (res.data ? (' - ' + (res.data.message || JSON.stringify(res.data))) : ''));
- }
- },
- fail: (err) => {
- console.error('API请求失败:', {
- url: BASE_URL + url,
- error: err
- });
- reject(err.errMsg || '网络请求失败');
- }
- });
- });
- }
- // GET请求
- function get(url, data = {}, needAuth = true) {
- return request(url, 'GET', data, needAuth);
- }
- // POST请求
- function post(url, data = {}, needAuth = true, params = {}) {
- // 如果有查询参数,拼接到URL
- if (Object.keys(params).length > 0) {
- const queryString = Object.keys(params)
- .map(key => `${key}=${encodeURIComponent(params[key])}`)
- .join('&');
- url = url + (url.includes('?') ? '&' : '?') + queryString;
- }
- return request(url, 'POST', data, needAuth);
- }
- // PUT请求
- function put(url, data = {}, needAuth = true) {
- return request(url, 'PUT', data, needAuth);
- }
- // DELETE请求
- function del(url, data = {}, needAuth = true) {
- // DELETE请求的参数通常放在URL中
- if (Object.keys(data).length > 0) {
- const queryString = Object.keys(data)
- .map(key => `${key}=${encodeURIComponent(data[key])}`)
- .join('&');
- url = url + (url.includes('?') ? '&' : '?') + queryString;
- }
- return request(url, 'DELETE', {}, needAuth);
- }
- module.exports = {
- request,
- get,
- post,
- put,
- del,
- BASE_URL
- };
|