当前位置:网站首页>如何封装微信小程序的 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
});
}
}边栏推荐
- 李航《统计学习方法》笔记之朴素贝叶斯法
- 程序员的浪漫七夕
- 从零开始入门单片机(一):必会背景知识总结
- R language ggplot2 visualization: use the ggtexttable function of the ggpubr package to visualize tabular data (directly draw tabular graphs or add tabular data to images), use tbody_add_border to add
- 第十六章 协程
- 李航《统计学习方法》笔记之感知机perceptron
- TimerTask(addin timer语音)
- 【New Edition】DeepFakes: Creation, Detection and Influence
- 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
- R语言使用ggpubr包的ggtexttable函数可视化表格数据(直接绘制表格图或者在图像中添加表格数据)、设置theme主题参数自定义表格中表头内容的填充色(使用colnames.style参数)
猜你喜欢

Re22:读论文 HetSANN An Attention-based Graph Neural Network for Heterogeneous Structural Learning

基于列表的排队与叫号系统

Application scenarios of js anti-shake function and function throttling

Shell脚本实现多选DNS同时批量解析域名IP地址(新更新)

周杰伦新歌发布,爬取《Mojito》MV弹幕,看看粉丝们都说的些啥!

npm ERR! 400 Bad Request - PUT xxx - Cannot publish over previously published version “1.0.0“.

typeinfo类型支持库学习

云原生应用平台的核心模块有哪些

后管实现面包屑功能

李航《统计学习方法》笔记之监督学习Supervised learning
随机推荐
R语言时间序列数据的平滑:使用KernSmooth包的dpill函数和locpoly函数对时间序列数据进行平滑以消除噪声
npm ERR! 400 Bad Request - PUT xxx - Cannot publish over previously published version “1.0.0“.
Verilog的随机数系统任务----$random
2022.7.25-7.31 AI行业周刊(第108期):值钱比赚钱更重要
TimerTask(addin timer语音)
如何搭建威纶通触摸屏与S7-200smart之间无线PPI通信?
php组件漏洞
Linux系统卸载,安装,升级,迁移clickHouse数据库
Use compilation to realize special effects of love
瑞吉外卖项目剩余功能补充
iNFTnews | 看见元宇宙的两面,何谓全真互联网和价值互联网?
R语言ggplot2可视化:基于aes函数中的fill参数和shape参数自定义绘制分组折线图并添加数据点(散点)、使用theme函数的legend.position函数配置图例到图像右侧
Use the scrapy to climb to save data to mysql to prevent repetition
开源一夏 | GO语言框架中如何快速集成日志模块
MySql tens of millions of paging optimization, fast insertion method of tens of millions of data
Unknown content monitoring
R语言使用zoo包中的rollapply函数以滚动的方式、窗口移动的方式将指定函数应用于时间序列、设置align参数指定结果数据中的时间标签取自窗口中的位置(参数right指定取自窗口的最右侧)
sqlmap安装教程用w+r打开(sqlyog安装步骤)
牛客刷题——剑指offer(第三期)
Mistakes in Brushing the Questions 1-Implicit Conversion and Loss of Precision