当前位置:网站首页>移动端——uniapp开发记录(公共请求request封装)
移动端——uniapp开发记录(公共请求request封装)
2022-07-03 04:37:00 【我不瘦但很逗】
移动端开发需要兼容各个端,技术栈选用vue2+uniApp;
1.项目初始化
可以使用IDE(HBuilderX)去初始化项目,也可以使用npm命令去构建实现不依赖uniapp去打包部署;【详细见官网】
2.移动端防止页面双击变大,在index.html页面使用:
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
3.移动端CSS,使用flex布局
4.请求封装,uniapp.request()
公共请求封装:
import $store from '../../store/index.js';
export default {
//全局配置
common: {
// #ifdef H5
//仅在H5使用
baseUrl: "",
// #endif
baseUrl: "",
header: {
'Content-Type': 'application/json;charset=UTF-8',
},
data: {
},
method: 'GET',
dataType: 'json', //设为 json,会尝试对返回的数据做一次 JSON.parse
token: true
},
//请求返回Promise对象
request(options = {
}) {
//参数处理整合
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
//请求
return new Promise((res, rej) => {
// 请求之前看下token标识,登录接口是不需要token(token=false),此标识将token验证过滤
if (options.token) {
let token = $store.state.token
// 往header头中添加token
options.header["token"] = token
if (!token) {
uni.showToast({
title: '请先登录并授权',
icon: 'none'
});
// token不存在时跳转
uni.navigateTo({
url: '/pages/login/login',
});
return rej("请先登录")
}
}
//发送请求
uni.request({
...options,
success: (result) => {
//因为后端返回结果不同意此处没法弄直接返回害
return res(result)
},
fail: (error) => {
uni.showToast({
title: error.errMsg || '请求失败',
icon: 'none'
});
return rej(error)
}
})
})
},
// get请求
get(url, data = {
}, options = {
}) {
options.url = url
options.data = data
options.method = 'GET'
return this.request(options)
},
// post请求
post(url, data = {
}, options = {
}) {
options.url = url
options.data = data
options.method = 'POST'
return this.request(options)
},
// put请求
put(url, data = {
}, options = {
}) {
options.url = url
options.data = data
options.method = 'PUT'
return this.request(options)
},
// delete请求
delete(url, data = {
}, options = {
}) {
options.url = url
options.data = data
options.method = 'DELETE'
return this.request(options)
},
}
公共请求使用:
main.js中引用
import $Http from './request.js'; //封装请求路径
Vue.prototype.$Http = $Http
getData() {
this.$Http
.get(
"你的url",
{
param:'需要传递的参数',
},
{
header: {
"Content-Type": "application/x-www-form-urlencoded",
},
}
)
.then((res) => {
console.log(res)
if (res.statusCode == 200) {
//成功需要做的事情
}
});
},
5.长按与单击事件,标记使事件变为互斥使事件独立不受影响;
主要代码如下:
<template>
<view @longpress="longpress()" @click="click()" @touchend="touchend">
<text>点击触发</text>
</view>
</template>
<script>
export default {
data() {
return {
islongPress: false //长按记录变量
};
},
methods: {
longpress() {
console.log('长按事件');
this.islongPress = true;
uni.showModal({
title: '操作',
content: '是否需要取消?',
success: res => {
if (res.confirm) {
console.log('用户点击确认');
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
},
},
click() {
if (this.islongPress) return;
//点击事件去跳转到视频播放页
console.log();
},
touchend() {
//延时执行为了防止 click() 还未判断 islongPress 的值就被置为 fasle
setTimeout(() => {
this.islongPress = false;
}, 200);
}
}
</script>
多看官网文档,你想要的文档都有,后续有值得分享的在更新吧。
边栏推荐
- 普通本科大学生活避坑指南
- 2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination
- C language series - Section 3 - functions
- 消息队列(MQ)介绍
- vulnhub HA: Natraj
- FFMpeg example
- Writing skills of multi plate rotation strategy -- strategy writing learning materials
- Network security textual research recommendation
- 金仓数据库KingbaseES 插件kdb_database_link
- [Thesis Writing] how to write the overall design of JSP tourism network
猜你喜欢
vulnhub HA: Natraj
Auman Galaxy new year of the tiger appreciation meeting was held in Beijing - won the double certification of "intelligent safety" and "efficient performance" of China Automotive Research Institute
Small sample target detection network with attention RPN and multi relationship detector (provide source code, data and download)
The simple problem of leetcode: dismantling bombs
消息队列(MQ)介绍
带有注意力RPN和多关系检测器的小样本目标检测网络(提供源码和数据及下载)...
Which Bluetooth headset is cost-effective? Four Bluetooth headsets with high cost performance are recommended
When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
Sdl2 + OpenGL glsl practice (Continued)
Games101 Lesson 9 shading 3 Notes
随机推荐
How do you use lodash linking function- How do you chain functions using lodash?
[set theory] binary relationship (special relationship type | empty relationship | identity relationship | global relationship | divisive relationship | size relationship)
AWS VPC
2022 P cylinder filling test content and P cylinder filling simulation test questions
Kingbasees plug-in KDB of Jincang database_ exists_ expand
FFMpeg filter
MC Layer Target
When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
多板块轮动策略编写技巧----策略编写学习教材
[set theory] binary relationship (definition field | value field | inverse operation | inverse synthesis operation | restriction | image | single root | single value | nature of synthesis operation)
Contents of welder (primary) examination and welder (primary) examination in 2022
Which Bluetooth headset is good about 400? Four Bluetooth headsets with strong noise reduction are recommended
使用BENCHMARKSQL工具对kingbaseES执行灌数据提示无法找到JDBC driver
FISCO bcos zero knowledge proof Fiat Shamir instance source code
Golang -- realize file transfer
The reason why the entity class in the database is changed into hump naming
怎么用Kotlin去提高生产力:Kotlin Tips
How to retrieve the password for opening word files
Reptile exercise 02
2022-02-12 (338. Bit count)