当前位置:网站首页>如何封装微信小程序的 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
});
}
}边栏推荐
- 软件测试之发现和解决bug
- R语言ggpubr包的ggbarplot函数可视化分组柱状图、设置add参数为mean_se可视化不同水平均值的柱状图并为柱状图添加误差线(se标准误差)、position参数自定义分组柱状图分离
- R语言使用ggpubr包的ggtexttable函数可视化表格数据(直接绘制表格图或者在图像中添加表格数据)、设置theme主题参数自定义表格中表头内容的填充色(使用colnames.style参数)
- 日元疲软令游戏机在日本变身“理财产品”:黄牛大赚
- The ggbarplot function of the R language ggpubr package visualizes the grouped histogram, sets the add parameter to mean_se to visualize the histogram of the mean values of different levels and adds
- 用了TCP协议,就一定不会丢包嘛?
- js防抖函数和函数节流的应用场景
- You Only Hypothesize Once: 用旋转等变描述子估计变换做点云配准(已开源)
- The R language uses the ggtexttable function of the ggpubr package to visualize the table data (draw the table directly or add the table data to the image), set the theme parameter to customize the fi
- R language ggplot2 visualization: use the ggbarplot function of the ggpubr package to visualize the stacked bar plot, the lab.pos parameter specifies the position of the numerical label of the bar cha
猜你喜欢

List-based queuing and calling system

Supervised learning of Li Hang's "Statistical Learning Methods" Notes

Re23:读论文 How Does NLP Benefit Legal System: A Summary of Legal Artificial Intelligence

用正向迭代器封装实现反向迭代器

Verilog的随机数系统任务----$random

食品安全 | 鱼肝油不是鱼油,家有宝宝的注意了

The heavyweights are coming!Spoilers for the highlights of the Alibaba Cloud Life Science and Intelligent Computing Summit

向量点积(Dot Product),向量叉积(Cross Product)

软件测试H模型

在全志V853开发板试编译QT测试
随机推荐
Application scenarios of js anti-shake function and function throttling
qq邮箱日发5万邮件群发技术(qq邮箱怎样定时发送邮件)
This article takes you to understand the commonly used models and frameworks of recommender systems
刷题错题录1-隐式转换与精度丢失
typeinfo类型支持库学习
Use the scrapy to climb to save data to mysql to prevent repetition
The 17th day of the special assault version of the sword offer
【新版干货书】深度伪造 (DeepFakes):创造,检测和影响
Weak yen turns game consoles into "financial products" in Japan: scalpers make big profits
R语言使用ggpubr包的ggtexttable函数可视化表格数据(直接绘制表格图或者在图像中添加表格数据)、设置theme主题参数自定义表格中表头内容的填充色(使用colnames.style参数)
练习-17
Unknown content monitoring
未知内容监控
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
Linux系统卸载,安装,升级,迁移clickHouse数据库
周杰伦新歌发布,爬取《Mojito》MV弹幕,看看粉丝们都说的些啥!
百战RHCE(第四十六战:运维工程师必会技-Ansible学习1-基础知识讲解)
百战RHCE(第四十七战:运维工程师必会技-Ansible学习2-Ansible安装配置练习环境)
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
带你认识40G单纤双向光模块-QSFP+ BiDi光模块