239
社区成员




这个作业属于哪个课程 | FZU_SE_teacherW_4 |
---|---|
这个作业要求在哪里 | 软件工程实践总结&个人技术博客-CSDN社区 |
这个作业的目标 | 课程实践总结、分析软件开发模式 |
其他参考文献 | uni.request(OBJECT) uni-app官网 、 网络 / 发起请求 / wx.request |
uni.request
用于实现小程序与后端服务器之间的数据交互,支持GET、POST等多种请求方式。GET请求示例:
uni.request({
url: 'https://api.example.com/data',
method: 'GET',
// success和fail回调分别处理请求成功和失败的情况
success: (res) => {
console.log(res.data);
},
fail: (err) => {
console.error(err);
}
});
POST请求示例:
uni.request({
url: 'https://api.example.com/submit',
method: 'POST',
data: { name: 'John', age: 30 },
success: (res) => {
console.log(res.data);
},
fail: (err) => {
console.error(err);
}
});
为了提高代码复用性,通常会对请求进行封装(通常项目会使用token进行登录验证,以下考虑了token验证)
const BASE_URL = 'https://api.example.com';
/*
* 全局请求封装
* @param path 请求路径
* @param method 请求类型(GET/POST/DELETE等)
* @oaram data 请求体数据
* @param loading 请求未完成是是否显示加载中,默认为true
*/
export default (path, method, data = {}, loading = true) => {
// 从localStorage中获取存储token
const token = uni.getStorageSync("token");
if (loading) {
uni.showLoading({ //这里需主动调用 uni.hideLoading 才能关闭提示框
title: "加载中",
mask: true
});
};
//根据有无token调用函数
if (token != '') {
return tokenRequest(path, method, data, loading, token)
} else {
return noTokenRequest(path, method, data, loading)
}
};
// 有token时发送请求函数
function tokenRequest(path, method, data, loading, token) {
return new Promise((resolve, reject) => {
uni.request({
url: BASEURL + path,
method: method,
data,
header: {
"token": token
},
success(response) {
//这里可以做响应拦截(以返回401表示未登录为例)
if (response.data.code === 401) {
logout();
}
console.log(response.data);
resolve(response.data);
},
fail(err) {
uni.showToast({
icon: "none",
title: '服务响应失败'
});
console.error(err);
reject(err);
},
complete() {
uni.hideLoading();
}
});
});
}
// 无token时发送请求函数
function noTokenRequest(path, method, data, loading) {
return new Promise((resolve, reject) => {
uni.request({
url: BASEURL + path,
method: method,
data,
success(response) {
//这里同样可以做响应拦截
console.log(response.data)
resolve(response.data);
},
fail(err) {
uni.showToast({
title: '服务响应失败'
});
console.error(err);
reject(err);
},
complete() {
uni.hideLoading();
}
});
});
}
调用
import request from '@/utils/request.js';
// 获取用户信息
export const getBaseInfo = () => {
return request(`/user/info`, 'GET')
}
// 登录
export const login = (user) => {
return request(`/user/login`, 'POST', user)
}
跨域请求问题
小程序开发中可能遇到跨域请求限制,导致请求失败。解决方法是通过后端设置CORS
,允许特定来源访问,或者使用uniCloud
代理功能来绕过跨域问题。
请求失败和错误处理
网络请求有时会因网络不稳定而失败,需要通过fail
回调对不同类型的错误进行处理:
fail: (err) => {
if (err.statusCode === 500) {
uni.showToast({ title: '服务器错误,请稍后再试', icon: 'none' });
} else {
uni.showToast({ title: '网络错误,请检查网络连接', icon: 'none' });
}
}
性能优化
请求频繁时可能影响小程序性能,可以通过分页加载、请求合并等方式减少请求次数,提升用户体验。
分页请求示例:
uni.request({
url: 'https://api.example.com/data',
method: 'GET',
data: { page: currentPage, size: pageSize },
success: (res) => {
console.log(res.data);
}
});
uni.request
能够有效地实现微信小程序与后端之间的数据交互。通过封装请求、处理跨域问题和优化网络请求性能,提升了代码的复用性和系统的稳定性。在遇到请求失败或网络不稳定时,及时捕获并进行错误处理,确保用户获得清晰的反馈。这些技术措施不仅增强了小程序的功能性,也提升了用户体验和开发效率。