1234567891011121314151617181920212223242526272829303132333435363738394041 |
- // utils/request.js
- const BASE_URL = 'http://localhost:8080'; // 修改为本地开发服务器地址
- function request(options) {
- return new Promise((resolve, reject) => {
- const token = uni.getStorageSync('token'); // 从本地缓存获取token
-
- // 完整URL,包含查询参数
- const url = BASE_URL + options.url;
-
- console.log(`开始请求: ${options.method} ${url}`, options.data);
-
- uni.request({
- url: url,
- method: options.method || 'GET',
- data: options.data || {},
- header: {
- 'Content-Type': 'application/json',
- ...options.header,
- ...(token && { 'token': `${token}`})
- },
- success: (res) => {
- console.log(`请求成功: ${url}`, res);
- if (res.statusCode === 200) {
- resolve(res.data);
- } else {
- console.error(`请求失败,状态码:${res.statusCode}`, res.data);
- reject(new Error(`请求失败,状态码:${res.statusCode}`));
- }
- },
- fail: (err) => {
- console.error(`网络请求出错:${url}`, err);
- reject(new Error(`网络请求出错:${err.errMsg}`));
- }
- });
- });
- }
- export {
- request
- };
|