当前位置:网站首页>Mobile terminal - uniapp development record (public request encapsulation)
Mobile terminal - uniapp development record (public request encapsulation)
2022-07-03 04:44:00 【I'm not thin but funny】
Mobile terminal development needs to be compatible with all terminals , Technology stack selection vue2+uniApp;
1. Project initialization
have access to IDE(HBuilderX) To initialize the project , You can also use npm Commands to build implementations do not depend on uniapp To package and deploy ;【 See the official website for details. 】
2. The mobile terminal prevents the page from double clicking and getting larger , stay index.html Page using :
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
3. Mobile CSS, Use flex Layout
4. Request to packaging ,uniapp.request()
Public request encapsulation :
import $store from '../../store/index.js';
export default {
// Global configuration
common: {
// #ifdef H5
// Only in H5 Use
baseUrl: "",
// #endif
baseUrl: "",
header: {
'Content-Type': 'application/json;charset=UTF-8',
},
data: {
},
method: 'GET',
dataType: 'json', // Set to json, It will try to do one on the returned data JSON.parse
token: true
},
// Request returns Promise object
request(options = {
}) {
// Parameter processing integration
options.url = this.common.baseUrl + options.url
options.header = options.header || this.common.header
options.data = options.data || this.common.data
options.method = options.method || this.common.method
options.dataType = options.dataType || this.common.dataType
options.token = options.token == false ? false : this.common.token
// request
return new Promise((res, rej) => {
// Look before asking token identification , Login interface is not required token(token=false), This logo will token Verify filtering
if (options.token) {
let token = $store.state.token
// Go to header Add token
options.header["token"] = token
if (!token) {
uni.showToast({
title: ' Please login and authorize ',
icon: 'none'
});
// token Jump when it doesn't exist
uni.navigateTo({
url: '/pages/login/login',
});
return rej(" Please log in first ")
}
}
// Send a request
uni.request({
...options,
success: (result) => {
// Because the back-end return result does not agree, there is no direct return harm here
return res(result)
},
fail: (error) => {
uni.showToast({
title: error.errMsg || ' request was aborted ',
icon: 'none'
});
return rej(error)
}
})
})
},
// get request
get(url, data = {
}, options = {
}) {
options.url = url
options.data = data
options.method = 'GET'
return this.request(options)
},
// post request
post(url, data = {
}, options = {
}) {
options.url = url
options.data = data
options.method = 'POST'
return this.request(options)
},
// put request
put(url, data = {
}, options = {
}) {
options.url = url
options.data = data
options.method = 'PUT'
return this.request(options)
},
// delete request
delete(url, data = {
}, options = {
}) {
options.url = url
options.data = data
options.method = 'DELETE'
return this.request(options)
},
}
Public requests use :
main.js I quote
import $Http from './request.js'; // Encapsulate the request path
Vue.prototype.$Http = $Http
getData() {
this.$Http
.get(
" Yours url",
{
param:' Parameters to be passed ',
},
{
header: {
"Content-Type": "application/x-www-form-urlencoded",
},
}
)
.then((res) => {
console.log(res)
if (res.statusCode == 200) {
// What you need to do to succeed
}
});
},
5. Long press and click events , Tags make events mutually exclusive and independent ;
The main codes are as follows :
<template>
<view @longpress="longpress()" @click="click()" @touchend="touchend">
<text> Click on the trigger </text>
</view>
</template>
<script>
export default {
data() {
return {
islongPress: false // Long press to record variables
};
},
methods: {
longpress() {
console.log(' Long press event ');
this.islongPress = true;
uni.showModal({
title: ' operation ',
content: ' Need to cancel ?',
success: res => {
if (res.confirm) {
console.log(' The user clicks ok ');
} else if (res.cancel) {
console.log(' The user clicks cancel ');
}
}
});
},
},
click() {
if (this.islongPress) return;
// Click the event to jump to the video playing page
console.log();
},
touchend() {
// Delay execution to prevent click() Not yet judged islongPress The value of is set to fasle
setTimeout(() => {
this.islongPress = false;
}, 200);
}
}
</script>
Read more official website documents , All the documents you want , There are updates worth sharing in the future .
边栏推荐
- Function introduction of member points mall system
- 有道云笔记
- Number of uniform strings of leetcode simple problem
- MPM model and ab pressure test
- UiPath实战(08) - 选取器(Selector)
- Shell script Basics - basic grammar knowledge
- Leetcode simple question: check whether the array is sorted and rotated
- Market status and development prospect prediction of the global fire hose industry in 2022
- [XSS bypass - protection strategy] understand the protection strategy and better bypass
- How to choose cross-border e-commerce multi merchant system
猜你喜欢
Function introduction of member points mall system
When using the benchmarksql tool to test the concurrency of kingbasees, there are sub threads that are not closed in time after the main process is killed successfully
Web security - CSRF (token)
Prefix and (continuously updated)
The usage of micro service project swagger aggregation document shows all micro service addresses in the form of swagger grouping
JVM原理简介
X-ray normal based contour rendering
Use the benchmarksql tool to perform a data prompt on kingbases. The jdbc driver cannot be found
[tools run SQL blind note]
【工具跑SQL盲注】
随机推荐
2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank
Reptile exercise 02
After reviewing MySQL for a month, I was stunned when the interviewer of Alibaba asked me
IPhone x forgot the boot password
2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
A outsourcing boy's mid-2022 summary
带有注意力RPN和多关系检测器的小样本目标检测网络(提供源码和数据及下载)...
MediaTek 2023 IC written examination approved in advance (topic)
Kingbasees plug-in KDB of Jincang database_ date_ function
Human resource management system based on JSP
Matplotlib -- save graph
String matching: find a substring in a string
Network security textual research recommendation
Leetcode simple question: check whether two string arrays are equal
论文阅读_中文NLP_ELECTRA
[SQL injection] joint query (the simplest injection method)
When using the benchmarksql tool to test the concurrency of kingbasees, there are sub threads that are not closed in time after the main process is killed successfully
【PHP漏洞-弱类型】基础知识、php弱相等、报错绕过
4 years of experience to interview test development, 10 minutes to end, ask too
Career planning of counter attacking College Students