当前位置:网站首页>How to encapsulate the wx.request() request of WeChat applet
How to encapsulate the wx.request() request of WeChat applet
2022-08-02 10:12:00 【Wang - Xiao Miao】
Perform basic request encapsulation on wx.request() first, and then continue to encapsulate post and get requests
//in utils/http.js file/*** Request related packages*/let baseUrl = "http://xxx.xxx.xx.xx:xx/"; // interface addresslet header = {'content-type': 'application/x-www-form-urlencoded','Authorization': wx.getStorageSync("token")?wx.getStorageSync("token"):''}/*** package request*/function fetch(options) {if (options.loading) {wx.showLoading({title: 'Loading',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) { // Judge according to the return value of your own interface// re-loginreturn false;}resolve(res.data); //Send the requested data to the place where the request is referenced},fail: function(err) {if (options.loading) {wx.hideLoading()}wx.showToast({title: "Network connection timed out",icon: 'none',duration: 3000,})}})})}/*** POST request*/export function post(url, params, loading = true) {var option = {url: url,data: params,method: 'POST',loading}return fetch(option);}/*** GET request*/export function get(urls, params, loading = true) {var option = {url: urls,data: params,method: 'GET',loading}return fetch(option);}Use
//First introduced through modularizationconst util = require('../../utils/util.js')//get requesthttp.get('getuserInfo', params).then((res) =>{console.log(res)})//post requesthttp.post('findDepartment', params).then( (res) =>{console.log(res);})// You can also use async and await, e.g.async formSubmit(e) {let params = {name: e.detail.value.namepassward:e.detail.value.passward}//let params = e.detail.valueconst res = await http.post('login',params)if(res.status====0){//Save the token and user information to the localwx.setStorageSync('token', res.token);wx.setStorageSync('userinfo', res.userinfo);wx.showToast({title: res.message,icon: 'none',duration: 3000});}}边栏推荐
- iNFTnews | 看见元宇宙的两面,何谓全真互联网和价值互联网?
- 超赞!发现一个APP逆向神器!
- 3D激光slam:LeGO-LOAM---地面点提取方法及代码分析
- 牛客网项目2.7开发注册功能 报错This application has no explicit mapping for /error......
- 后管实现面包屑功能
- 零代码工具推荐---HiFlow
- 为什么要使用BGP?
- Application scenarios of js anti-shake function and function throttling
- R语言时间序列数据算术运算:使用log函数将时间序列数据的数值对数化、使用diff函数计算对数化后的时间序列数据的逐次差分(计算价格的对数差分)
- yolov7创新点
猜你喜欢
随机推荐
图形化矩阵,矩阵到底长什么样?
R语言ggpubr包的ggline函数可视化分组折线图、add参数为mean_se和dotplot可视化不同水平均值的折线图并为折线图添加误差线(se标准误差)和点阵图、自定义palette设置颜色
R language ggplot2 visualization: based on the fill parameter and shape parameter in the aes function, custom draw a grouped line chart and add data points (scatter points), use the legend.position fu
鸿星尔克再捐一个亿
向量点积(Dot Product),向量叉积(Cross Product)
List-based queuing and calling system
周杰伦新歌发布,爬取《Mojito》MV弹幕,看看粉丝们都说的些啥!
太帅了!我用炫酷大屏展示爬虫数据!
软件测试与质量 之白盒测试
The love-hate relationship between C language volatile keyword, inline assembly volatile and compiler
Spearman's correlation coefficient
Pytorch的LSTM参数解释
21年毕业转行软件测试,从0收入到月薪过万,我真的很幸运...
Naive Bayesian Method of Li Hang's "Statistical Learning Methods" Notes
LayaBox---TypeScript---命名空间和模块
如何安装dosbox(pycharm详细安装教程)
Rear tube implements breadcrumb function
R语言ggplot2可视化:使用ggpubr包的ggbarplot函数可视化堆叠的柱状图(stacked bar plot)、lab.pos参数指定柱状图的数值标签的位置,lab.col参数指定数值标
LayaBox---TypeScript---Symbols
阿里巴巴 CTO 程立:开源是基础软件的源头!









