当前位置:网站首页>Applet request interface encapsulation
Applet request interface encapsulation
2022-06-25 19:41:00 【Bin daotianxia】
1. Create a new one req.js Storage packaging
2. Page call req.js encapsulation
I judged code==2 It's time to jump to the login interface .
code==1 Pop up error messages
code==0 The interface runs normally . Remember resolve(res); important !
req.js file
const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const FORM = 'FORM';
const DELETE = 'DELETE';
const baseURL = 'https://test. Your interface URL .com/index/';
const app = getApp();
function request(url, data,cb) {
return new Promise(function(resolve, reject) {
// Common parameters
const uid = wx.getStorageSync('uid');
const api_data = {uid: uid}
wx.showLoading({
title: ' Loading ',
});
let header = {
'content-type': 'application/json',
};
// Merge parameter objects
var tmp = Object.assign(data , api_data)
wx.request({
url: baseURL + url,
method: 'POST',
data: JSON.stringify(tmp),
header: header,
success(res) {
wx.hideLoading()
// The request is successful
if (res.data.code == 2) {// Authorize to log in
wx.showToast({
title: res.data.msg,
icon:'none',
})
wx.navigateTo({
url: '../../pages/login/index',
})
} else if(res.data.code == 1){
wx.showToast({
title: res.data.msg,
icon:'none',
})
}else if(res.data.code == 0){
resolve(res);
}else{
wx.showToast({
title: ' request was aborted ',
icon:'none',
})
}
},
fail(err) {
setTimeout(function () {
wx.hideLoading()
}, 1000)
wx.showToast({
title:' Network error , Please refresh and retry ',
icon:'none',
})
}
})
})
}
export { request };3. Using encapsulation
Head introduction :
var app = getApp();
var request = require('../../utils/req.js');
Use This one in the middle request.req....... Copy , Write your interface suffix address , Parameters . Get it done 、
hotIndex(){
var that =this;
request.request('Index/hotIndex',{
page:1,
limit:4,
}).then(res=>{
if(res.data.code==0){
that.setData({
hotIndexList:res.data.data
})
}
})
},4. A wechat login file for the first step is attached
In the package code==2 Will automatically jump to this file . To authorize login . After authorization , Go back to the previous level .
login/index.wxml
<button class="shouquan" open-type="getUserInfo"bindgetuserinfo="onGotUserInfo"> Authorized login </button>var app = getApp();
Page({
data: {
getpage: '',
},
onLoad: function (options) {
var that =this;
},
// Click to authorize without authorization
onGotUserInfo: function (e) {
var that = this;
wx.login({
success: res => {
var code = res.code;
// Get user information ( To the backstage )
wx.getUserInfo({
success: function (res) {
wx.request({
url: app.globalData.api_url+'wxchat/authorization',
method: 'GET',
header: {
'content-type': 'application/json' // The default value is
},
data: {
code: code,
encryptedData: res.encryptedData,
iv: res.iv
},
success: function (result) {
if(result.data.code==0){
wx.setStorageSync('uid', result.data.data.id);
wx.setStorageSync('userInfo', result.data.data);
wx.navigateBack({
delta: 1,
})
}else{
wx.showToast({
title: result.data.msg,
icon: 'none',
})
}
}
})
}
});
}
})
},
})边栏推荐
- Ali visual AI training camp -day05- creativity day - your image recognition project
- Guangzhou Sinovel interactive VR panorama brings development to all walks of life
- QQ机器人:群成员自我禁言管理【最新beta2版本】
- Install spoole
- The native JS mobile phone sends SMS cases. After clicking the button, the mobile phone number verification code is sent. The button needs to be disabled and re enabled after 60 seconds
- Leetcode-101-symmetric binary tree
- SEO outsourcing reliable company, enterprise SEO outsourcing company which reliable?
- PHP database connection version1.1
- 什么是算子?
- Shell jump loop shift parameter left use of function
猜你喜欢

Vulnhub range - the planes:venus

What should I pay attention to in GoogleSEO content station optimization?

Applet wx Request encapsulation

2020-12-09 laravel . Env file loading mechanism process

Cutting feet to fit shoes - talking about the ramp reconstruction on the track

PostgreSQL user role permissions

Mail monitoring cloud script execution progress

Combing the latest Data Mining Event Scheme!

最新数据挖掘赛事方案梳理!

广州华锐互动打造VR展厅全景在线虚拟展厅
随机推荐
6、 Configuration resolution of hikariconfig
Ali vision AI training camp-day01
什么是算子?
Detailed explanation of oauth2 - Introduction (I)
五、HikariCP源码分析之初始化分析二
Go language installation and uninstallation
广州华锐互动VR全景为各行各业带来发展
DataX script task development record
PostgreSQL division considerations
PHP little knowledge record
Mail monitoring cloud script execution progress
User management and permissions
LeetCode-101-对称二叉树
Vulnhub range the planes: mercury
JVM|运行时数据区(堆空间)
Mysql database design suggestions
PostgreSQL change table owner
三、HikariCP获取连接流程源码分析三
二、HikariCP獲取連接流程源碼分析二
Can GoogleSEO only do content without external chain? (e6zzseo)