当前位置:网站首页>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});}}边栏推荐
- 周杰伦新歌发布,爬取《Mojito》MV弹幕,看看粉丝们都说的些啥!
- R语言ggplot2可视化:使用ggpubr包的ggbarplot函数可视化堆叠的柱状图(stacked bar plot)、lab.pos参数指定柱状图的数值标签的位置,lab.col参数指定数值标
- matlab-day02
- 零代码工具推荐---HiFlow
- js防抖函数和函数节流的应用场景
- Verilog's random number system task----$random
- 8月份的.NET Conf 活动 专注于 .NET MAUI
- Verilog的随机数系统任务----$random
- WPF 截图控件之文字(七)「仿微信」
- R语言ggpubr包的ggbarplot函数可视化分组柱状图、设置add参数为mean_se可视化不同水平均值的柱状图并为柱状图添加误差线(se标准误差)、position参数自定义分组柱状图分离
猜你喜欢

Linux system uninstall, install, upgrade, migrate clickHouse database

The realization of the list

带你认识40G单纤双向光模块-QSFP+ BiDi光模块

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

Turning and anti-climbing attack and defense

关于缓存数据的探讨

你认同这个观点吗?大多数企业的数字化都只是为了缓解焦虑

软件测试H模型

链表的实现

牛客网项目2.7开发注册功能 报错This application has no explicit mapping for /error......
随机推荐
基于列表的排队与叫号系统
后管实现面包屑功能
超赞!发现一个APP逆向神器!
身为程序猿——谷歌浏览器的这些骚操作你真的废吗!【熬夜整理&建议收藏】[通俗易懂]
MSYS2 QtCreator Clangd 代码分析找不到 mm_malloc.h的问题补救
List-based queuing and calling system
iNFTnews | Seeing the two sides of the metaverse, what is the true Internet and the Internet of value?
currentstyle 织梦_dede currentstyle属性完美解决方案
【术语科普】关于集成工作台那些难懂的词儿,看这篇秒懂!
行为型模式-模板方法模式
Event 对象,你很了解吗?
牛客网项目17节生成验证码 刷新验证码一直没反应
软件测试与质量 之白盒测试
【New Edition】DeepFakes: Creation, Detection and Influence
软件工程国考总结——选择题
阿里CTO程立:阿里巴巴开源的历程、理念和实践
LayaBox---TypeScript---高级类型
Facebook's automated data analysis solution saves worry and effort in advertising
未知内容监控
LayaBox---TypeScript---装饰器