当前位置:网站首页>如何封装微信小程序的 wx.request() 请求
如何封装微信小程序的 wx.request() 请求
2022-08-02 10:01:00 【王——小喵】
先对 wx.request() 进行基本的请求封装,再继续封装 post 和 get 请求
//在utils/http.js文件中
/**
* 请求相关的封装
*/
let baseUrl = "http://xxx.xxx.xx.xx:xx/"; // 接口地址
let header = {
'content-type': 'application/x-www-form-urlencoded',
'Authorization': wx.getStorageSync("token")?wx.getStorageSync("token"):''
}
/**
* 封装请求
*/
function fetch(options) {
if (options.loading) {
wx.showLoading({
title: '加载中',
mask: true
})
}
return new Promise((resolve, reject) => {
wx.request({
url: baseUrl + options.url,
data: options.data,
header: header,
method: options.method,
success: function(res) {
if (options.loading) {
wx.hideLoading()
}
if (res.data.status != 0) { //根据自己的接口返回值进行判断
// 重新登陆
return false;
}
resolve(res.data); //把请求到的数据发到引用请求的地方
},
fail: function(err) {
if (options.loading) {
wx.hideLoading()
}
wx.showToast({
title: "网络连接超时",
icon: 'none',
duration: 3000,
})
}
})
})
}
/**
* POST 请求
*/
export function post(url, params, loading = true) {
var option = {
url: url,
data: params,
method: 'POST',
loading
}
return fetch(option);
}
/**
* GET请求
*/
export function get(urls, params, loading = true) {
var option = {
url: urls,
data: params,
method: 'GET',
loading
}
return fetch(option);
}
使用
//先通过模块化引入
const util = require('../../utils/util.js')
//get请求
http.get('getuserInfo', params).then((res) =>{
console.log(res)
})
//post请求
http.post('findDepartment', params).then( (res) =>{
console.log(res);
})
//也可以使用 async 和 await,比如
async formSubmit(e) {
let params = {
name:e.detail.value.name
passward:e.detail.value.passward
}
//let params = e.detail.value
const res = await http.post('login',params)
if(res.status===0){
//将token和用户信息存到本地
wx.setStorageSync('token', res.token);
wx.setStorageSync('userinfo', res.userinfo);
wx.showToast({
title: res.message,
icon: 'none',
duration: 3000
});
}
}
边栏推荐
- The love-hate relationship between C language volatile keyword, inline assembly volatile and compiler
- 身为程序猿——谷歌浏览器的这些骚操作你真的废吗!【熬夜整理&建议收藏】[通俗易懂]
- 软件测试与质量 之白盒测试
- sqlmap安装教程用w+r打开(sqlyog安装步骤)
- Use the scrapy to climb to save data to mysql to prevent repetition
- 行为型模式-模板方法模式
- WPF 截图控件之文字(七)「仿微信」
- 全新荣威RX5,27寸大屏吸引人,安全、舒适一个不落
- c#反射和特性
- 一款优秀的中文识别库——ocr
猜你喜欢
【新版干货书】深度伪造 (DeepFakes):创造,检测和影响
Supervised learning of Li Hang's "Statistical Learning Methods" Notes
带你认识40G单纤双向光模块-QSFP+ BiDi光模块
超赞!发现一个APP逆向神器!
This article takes you to understand the commonly used models and frameworks of recommender systems
李航《统计学习方法》笔记之k近邻法
牛客网项目17节生成验证码 刷新验证码一直没反应
The k-nearest neighbor method in the notes of Li Hang's "Statistical Learning Methods"
yolov7创新点
The perceptron perceptron of Li Hang's "Statistical Learning Methods" notes
随机推荐
牛客网项目2.7开发注册功能 报错This application has no explicit mapping for /error......
In the whole development of chi V853 board tried to compile QT test
从零开始Blazor Server(5)--权限验证
瑞吉外卖项目剩余功能补充
利用二维数据学习纹理三维网格生成(CVPR 2020)
The heavyweights are coming!Spoilers for the highlights of the Alibaba Cloud Life Science and Intelligent Computing Summit
使用scrapy 把爬到的数据保存到mysql 防止重复
Using the TCP protocol, will there be no packet loss?
使用较广泛的安全测试工具有哪些?
如何搭建威纶通触摸屏与S7-200smart之间无线PPI通信?
享年94岁,图灵奖得主、计算复杂性理论先驱Juris Hartmanis逝世
Facebook's automated data analysis solution saves worry and effort in advertising
8月份的.NET Conf 活动 专注于 .NET MAUI
Weak yen turns game consoles into "financial products" in Japan: scalpers make big profits
c#反射和特性
瑞萨RZ/G2L处理器详细测评
Use compilation to realize special effects of love
云原生应用平台的核心模块有哪些
读博一年后对机器学习工程的思考
The ggline function of the R language ggpubr package visualizes grouped line graphs, the add parameter is mean_se and dotplot to visualize line graphs of different level averages, and adds error bars