当前位置:网站首页>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',
})
}
}
})
}
});
}
})
},
})边栏推荐
- ActiveMQ--CVE-2016-3088
- 最新数据挖掘赛事方案梳理!
- Mysql database design suggestions
- PHP synchronizes website content to hundreds of websites to improve SEO ranking
- Gbpnzd firm offer for 14 months, simulation for 19 months, test stable
- Ali visual AI training camp -day03- construction of electronic photo album (face and expression recognition)
- 最新數據挖掘賽事方案梳理!
- Vulnhub range - correlation:2
- rmi-registry-bind-deserialization
- R语言plotly可视化:plotly可视化二维直方图等高线图(Basic 2D Histogram Contour)
猜你喜欢

Google SEO external chain releases 50+ website platform sharing (e6zzseo)

Web components - Basics

PHP synchronizes website content to hundreds of websites to improve SEO ranking

Record Baidu search optimization thinking analysis

On Oracle full stack virtual machine -- graalvm
![QQ robot: self forbidden words management of group members [latest beta2 version]](/img/1b/7dcc8ed344c9f62870d76f16b99f1d.png)
QQ robot: self forbidden words management of group members [latest beta2 version]

QQ机器人:群成员自我禁言管理【最新beta2版本】

谈谈CNN中的位置和尺度问题

Leetcode-78-subset

PostgreSQL user role permissions
随机推荐
【历史上的今天】6 月 25 日:笔记本之父诞生;Windows 98 发布;通用产品代码首次商用
Network security detection and prevention test questions (4)
Install spoole
MySQL view explanation
The meanings of /32, /48, /64 in IPv6 addresses
Native JS array some method de duplication
Error record: preg_ match(): Compilation failed: range out of order in character class at offset 13
Idea common plug-ins
Mail monitoring cloud script execution progress
Applet password input box
User management and permissions
Tcp/ip test questions (I)
Google SEO external chain releases 50+ website platform sharing (e6zzseo)
Verification code native JS canvas
PostgreSQL change table owner
R language plot visualization: plot visualization of two-dimensional histogram contour (basic 2D histogram contour)
1、 Hikaricp source code analysis of connection acquisition process I
Gbpnzd firm offer for 14 months, simulation for 19 months, test stable
Many varieties of EA can be used
Ali visual AI training camp -day03- construction of electronic photo album (face and expression recognition)