当前位置:网站首页>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',
})
}
}
})
}
});
}
})
},
})边栏推荐
- DARKHOLE 2
- PHP synchronizes website content to hundreds of websites to improve SEO ranking
- Is it safe for tongdaxin to open an account?
- 3、 Hikaricp source code analysis of connection acquisition process III
- The meanings of /32, /48, /64 in IPv6 addresses
- Detailed explanation of oauth2 - Introduction (I)
- Lilda Bluetooth air conditioning receiver helps create a more comfortable road life
- DataX script task development record
- Convert word to PDF through libreoffice
- Elastic high-performance computing on the cloud supports the rapid development of the life science industry, reducing costs and increasing efficiency
猜你喜欢

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

【历史上的今天】6 月 25 日:笔记本之父诞生;Windows 98 发布;通用产品代码首次商用

什么是算子?

Ali vision AI training camp-day01

Ali visual AI training camp -day03- construction of electronic photo album (face and expression recognition)
![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]

ECS 7-day practical training camp (Advanced route) -- day04 -- build a portal using ECs and polardb

ECS 7-day practical training camp (Advanced route) -- day03 -- ecs+slb load balancing practice

Ali visual AI training camp -day05- creativity day - your image recognition project

2020-12-09 laravel . Env file loading mechanism process
随机推荐
最新數據挖掘賽事方案梳理!
Tcp/ip test questions (III)
PHP synchronizes website content to hundreds of websites to improve SEO ranking
揭秘GES超大规模图计算引擎HyG:图切分
MySQL view explanation
User management and permissions
English name of each stage of software version
二、HikariCP获取连接流程源码分析二
Google SEO external chain releases 50+ website platform sharing (e6zzseo)
Genicam gentl standard ver1.5 (1)
Vulnhub range - the planes:venus
[today in history] June 25: the father of notebook was born; Windows 98 release; First commercial use of generic product code
Guangzhou Sinovel interactive VR panorama brings development to all walks of life
Mail monitoring cloud script execution progress
为什么生命科学企业都在陆续上云?
Read multiple associations from a field using delimiters in laravel
Lilda Bluetooth air conditioning receiver helps create a more comfortable road life
Android Development Notes - Quick Start (from sqllite to room licentiousness) 2
Vulnhub range - darkhole 1
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