当前位置:网站首页>API encapsulation of uniapp applet
API encapsulation of uniapp applet
2022-06-25 14:12:00 【Residual white x】
File structure :
1. stay api Create a new file in the folder https.js Request encapsulation
// Differentiate between development and production environments Address of the interface
const baseUrl = 'https://test/'; // Change this to your interface address
export function https(config) {
// Show loading
wx.showLoading({
title: ' Loading ...'
});
// Set request header
config.header = {
'content-type': 'application/json',
};
// Check the cache for token
const token = uni.getStorageSync('graduateToken');
if (token) {
config.header.Authorization = token
// let routes = getCurrentPages(); // Gets the currently opened page routing array
// let curRoute = routes[routes.length - 1].route // Get the current page route
// if (routes == 'pages/login/login') {
// uni.switchTab({
// url: '../index/index',
// });
// }
} else {
uni.redirectTo({
url: '/pages/login/login.vue'
})
}
config.url = baseUrl + config.url; // Request address
let promise = new Promise(function(resolve, reject) {
uni.request(config).then(res => {
wx.hideLoading() // hide loading
if (res[0]) {
uni.showToast({
title: " Data acquisition failed ",
icon: "none",
duration: 1500
})
resolve(false);
} else {
let data = res[1].data;
if (data.code == 1) {
resolve(data);
} else {
resolve(data);
}
}
}).catch(error => {
wx.hideLoading() // hide loading
reject(error);
})
})
return promise
}
2. New again api.js Encapsulate the request address
import { https } from './https.js'; // Introduce just packaged https
class api {
// Get a list of tuition fees
async studentUserMsgsGetTuitionALL(params) {
const res = await https({
url: 'api/v1/getTuitionAll', // Here is your request path
method: 'post',
data: params
})
return res
}
// Get a list of contracts
async bktstudentContract(params) {
const res = await https({
url: 'api/v1/index',
method: 'post',
data: params
})
return res
}
// View receipt
async studentUserGetReceipt(params) {
const res = await https({
url: 'api/v1/getReceipt',
method: 'post',
data: params
})
return res
}
}
export default new api()
3. stay main.js Global mount is introduced in
import api from './pages/api/api.js'
Vue.prototype.$api = api4. Page direct use
// Get a list of contracts
bktstudentContract() {
this.$api.bktstudentContract().then((res) => {
if (res.code == 200) {
this.contractList = res.data;
} else {
uni.showToast({
title: res.data,
icon: "none",
position: 'top',
duration: 2000
});
}
});
},
// It is written in this way :
// Get a list of receipts
studentUserGetReceipt() {
const params = {
student_msg_id: this.infoId,
};
this.$api.studentUserGetReceipt(params).then((res) => {
if (res.code == 200) {
this.receiptList = res.data;
} else {
uni.showToast({
title: res.data,
icon: "none",
position: 'top',
duration: 2000
});
}
});
},边栏推荐
- Mysql database compressed backup_ Summary of MySQL backup compression and database recovery methods
- [world history] Episode 1: people in the Stone Age
- Is it safe for Guosen Securities to open a stock account? Excuse me?
- Les neuf caractéristiques de la parole et les neuf temps en anglais
- Explain the possible memory leaks caused by the handler at one time and the solutions | the developer said · dtalk
- Typescript and go --- essence
- Syntax 'trap'
- Cool assembly tutorial - concise x86 assembly language tutorial (1)
- About reconnection of STM32 using lan8720a plug-in network cable
- As a software testing engineer, how do you think to ensure software quality?
猜你喜欢

Nr-arfcn and channel grid, synchronous grid and GSCN

专家建议|8大措施加速你的创新职业规划和成长

电脑必须打开的设置

第一次读 “Clean” 系列,并没有觉得这是一本多好的书

程序员为什么要软一点?

k线图24种经典图解(影线篇)

Getting started with numpy Library

Complete and detailed compilation of experimental reports

阻止深度神经网络过拟合(Mysteries of Neural Networks Part II)

How to solve SQL import
随机推荐
打新债是不是不安全
Sigmoid function sigmoid derivation
Kubernetes cluster construction of multiple ECS
Shell operator
专家建议|8大措施加速你的创新职业规划和成长
Test your earning power? What will you do in the future?
Deploy eve-ng with KVM virtualization
Today in history: Netease was founded; The first consumer electronics exhibition was held; The first webcast in the world
shell 数组
Settings the PC must be turned on
多台云服务器的 Kubernetes 集群搭建
Vscode--- format setting configuration
对白:推荐系统快速入门路线及各知识点总结
golang项目依赖管理工具go vendor,go mod
China has made major breakthroughs in battery technology. Japan, South Korea and the United States are lagging behind. China has consolidated its leading edge
Turtlebot+lms111+gmapping practice
腾讯云搭建Socks5多IP代理服务器实现游戏单窗口单IP完美搭建教程附带工具「建议收藏」
多台云服务器的 Kubernetes 集群搭建
数据库中显示error1822,error1824
[proteus simulation] 51 MCU +ds1302+lcd1602 display
