当前位置:网站首页>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});}}边栏推荐
- 第十六章 协程
- Using the TCP protocol, will there be no packet loss?
- 程序员的浪漫七夕
- 关于缓存数据的探讨
- Geoffery Hinton:深度学习的下一个大事件
- 全新荣威RX5,27寸大屏吸引人,安全、舒适一个不落
- The realization of the list
- armv7与armv8的区别(v8和w12的区别)
- 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
- 3D激光slam:LeGO-LOAM---地面点提取方法及代码分析
猜你喜欢
随机推荐
LayaBox---TypeScript---JSX
云原生应用平台的核心模块有哪些
软件工程国考总结——选择题
iNFTnews | Seeing the two sides of the metaverse, what is the true Internet and the Internet of value?
QT专题:组合会话框和文本编辑器
js防抖函数和函数节流的应用场景
为什么要使用BGP?
Supervised learning of Li Hang's "Statistical Learning Methods" Notes
迭代器失效问题
只问耕耘,不问收获,其实收获却在耕耘中
第十七章 Excel操作
未知内容监控
8月份的.NET Conf 活动 专注于 .NET MAUI
程序员的浪漫七夕
c#反射和特性
LayaBox---TypeScript---JSX
MSYS2 QtCreator Clangd 代码分析找不到 mm_malloc.h的问题补救
LayaBox---TypeScript---装饰器
LayaBox---TypeScript---声明合并
wireshark的安装教程(暖气片安装方法图解)









