当前位置:网站首页>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});}}
边栏推荐
- R语言ggplot2可视化:使用ggpubr包的ggtexttable函数可视化表格数据(直接绘制表格图或者在图像中添加表格数据)、使用tbody_add_border为表格中的表头添加外侧框线
- 第十五章 多线程
- The k-nearest neighbor method in the notes of Li Hang's "Statistical Learning Methods"
- currentstyle 织梦_dede currentstyle属性完美解决方案
- 零代码工具推荐---HiFlow
- R语言ggplot2可视化:使用ggpubr包的ggbarplot函数可视化水平柱状图(条形图)、使用orientation参数设置柱状图转置为条形图
- 牛客刷题——剑指offer(第三期)
- 斯皮尔曼相关系数
- 如何选择一块真正“好用的、性能高”的远程控制软件
- logo 图标(php图片加文字水印)
猜你喜欢
随机推荐
Supervised learning of Li Hang's "Statistical Learning Methods" Notes
阿里CTO程立:阿里巴巴开源的历程、理念和实践
R language ggplot2 visualization: use the ggbarplot function of the ggpubr package to visualize the horizontal column chart (bar chart), use the orientation parameter to set the column chart to be tra
R language time series data arithmetic operation: use the log function to log the time series data, and use the diff function to calculate the successive difference of the logarithmic time series data
DVWA Clearance Log 2 - Command Injection
【新版干货书】深度伪造 (DeepFakes):创造,检测和影响
全新荣威RX5,27寸大屏吸引人,安全、舒适一个不落
Verilog's random number system task----$random
DirectX修复工具增强版「建议收藏」
练习16-两道模拟题
Pytorch的LSTM参数解释
This article takes you to understand the commonly used models and frameworks of recommender systems
太帅了!我用炫酷大屏展示爬虫数据!
Spearman's correlation coefficient
用正向迭代器封装实现反向迭代器
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
R语言时间序列数据的平滑:使用KernSmooth包的dpill函数和locpoly函数对时间序列数据进行平滑以消除噪声
如何搭建威纶通触摸屏与S7-200smart之间无线PPI通信?
currentstyle 织梦_dede currentstyle属性完美解决方案
云原生应用平台的核心模块有哪些