uni-app之网络请求
uni-app之网络请求
一,介绍
uni.request(OBJECT),发起网络请求,以下主要是一些特殊的参数说明,详细的可查看uni-app官网。
1,method的有效值必须是大写,默认GET方式;
2,success 返回参数说明
| 参数 | 类型 | 说明 |
|---|---|---|
| data | Object/String/ArrayBuffer | 开发者服务器返回的数据 |
| statusCode | Number | 开发者服务器返回的 HTTP 状态码 |
| header | Object | 开发者服务器返回的 HTTP Response Header |
3,传参数据说明
最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String。转换规则如下:
- 对于
GET方法,会将数据转换为 query string。例如{ name: 'name', age: 18 }转换后的结果是name=name&age=18。 - 对于
POST方法且header['content-type']为application/json的数据,会进行 JSON 序列化。 - 对于
POST方法且header['content-type']为application/x-www-form-urlencoded的数据,会将数据转换为 query string。
- 对于
二,网络请求封装
第一步:参数统一初始化配置
import { configBaseUrl, GET } from "./const"
config: {
baseUrl: configBaseUrl,
header: {
'Content-Type': 'application/json;charset=UTF-8',
},
data: {},
method: GET,
dataType: "json", /* 如设为json,会对返回的数据做一次 JSON.parse */
responseType: "text",
success() { },
fail() { },
complete() { }
},
第二步:拦截器定义
interceptor: {
request: null,
response: null
},
第三步:处理传入的参数
options.baseUrl = options.baseUrl || this.config.baseUrl
options.dataType = options.dataType || this.config.dataType
options.url = options.baseUrl + options.url
options.data = options.data || {}
options.method = options.method.toUpperCase() || this.config.method
// 请求头部类型提供自定义
const contentType = options.contentType;
delete options.contentType;// // 'Content-Type': 'application/x-www-form-urlencoded'
const headers = contentType ? { 'Content-Type': contentType } : this.config.baseUrl; //默认 application/json;charset=UTF-8 json数据传参
options.header = Object.assign({}, options.header, headers);
第四步:发送网络请求
使用Promise方法,方便调用获取返回的参数;并做统一的处理以及日志记录。
return new Promise((resolve, reject) => {
let _config: any = null
options.complete = (response: any) => {
let statusCode = response.statusCode
response.config = _config
if (process.env.NODE_ENV === 'development') {
if (statusCode === 200) {
console.log("【" + _config.requestId + "】 结果:" + JSON.stringify(response.data))
}
}
if (this.interceptor.response) {
// @ts-ignore
let newResponse = this.interceptor.response(response)
if (newResponse) {
response = newResponse
}
}
// 统一的响应日志记录
_reslog(response)
if (statusCode === 200) { //成功
const result = _getResult(response.data);//网络请求成功后数据处理
resolve(result);
} else {
reject(response)
}
}
_config = Object.assign({}, this.config, options)
_config.requestId = new Date().getTime()
if (this.interceptor.request) {
// @ts-ignore
this.interceptor.request(_config)
}
// 统一的请求日志记录
_reqlog(_config)
uni.request(_config);
});
第五步:再次封装,习惯使用
/**
* 服务统一处理 添加拦截器 并抛出使用
*/
function requestApi(url: String, options: any) {
if (!options) {
options = {}
}
/**
* @description: 响应拦截器
* @param {object} 当前请求成功回调数据
* @return 不return对象,则不返回数据
*/
// @ts-ignore
httpService.interceptor.response = (response: any) => {
console.log('个性化response....', JSON.stringify(response))
//判断返回状态 执行相应操作
return response;
}
/**
* @description: 请求拦截器
* @param {object} 当前请求配置参数
* @return 不return对象,则不发送当前请求
*/
// @ts-ignore
httpService.interceptor.request = (config: any) => {
console.log('config....', JSON.stringify(config))
//获取配置信息 统一添加配置与判断
return config;
}
options.url = url
return httpService.request(options);
}
第六步:完整代码
import { configBaseUrl, GET } from "./const";
const httpService = {
config: {
baseUrl: configBaseUrl,
header: {
'Content-Type': 'application/json;charset=UTF-8',
},
data: {},
method: GET,
dataType: "json", /* 如设为json,会对返回的数据做一次 JSON.parse */
responseType: "text",
success() { },
fail() { },
complete() { }
},
interceptor: {
request: null,
response: null
},
request(options: any) {
if (!options) {
options = {}
}
options.baseUrl = options.baseUrl || this.config.baseUrl
options.dataType = options.dataType || this.config.dataType
options.url = options.baseUrl + options.url
options.data = options.data || {}
options.method = options.method.toUpperCase() || this.config.method
// 请求头部类型提供自定义
const contentType = options.contentType;
delete options.contentType;// // 'Content-Type': 'application/x-www-form-urlencoded'
const headers = contentType ? { 'Content-Type': contentType } : this.config.baseUrl; //默认 application/json;charset=UTF-8 json数据传参
options.header = Object.assign({}, options.header, headers);
// 加密数据
// 数据签名
/*
_token = {'token': getStorage(STOREKEY_LOGIN).token || 'undefined'},
_sign = {'sign': sign(JSON.stringify(options.data))}
options.header = Object.assign({}, options.header, _token,_sign)
*/
return new Promise((resolve, reject) => {
let _config: any = null
options.complete = (response: any) => {
let statusCode = response.statusCode
response.config = _config
if (process.env.NODE_ENV === 'development') {
if (statusCode === 200) {
console.log("【" + _config.requestId + "】 结果:" + JSON.stringify(response.data))
}
}
if (this.interceptor.response) {
// @ts-ignore
let newResponse = this.interceptor.response(response)
if (newResponse) {
response = newResponse
}
}
// 统一的响应日志记录
_reslog(response)
if (statusCode === 200) { //成功
const result = _getResult(response.data);//网络请求成功后数据处理
resolve(result);
} else {
reject(response)
}
}
_config = Object.assign({}, this.config, options)
_config.requestId = new Date().getTime()
if (this.interceptor.request) {
// @ts-ignore
this.interceptor.request(_config)
}
// 统一的请求日志记录
_reqlog(_config)
uni.request(_config);
});
},
}
/**
* 服务统一处理 添加拦截器 并抛出使用
*/
function requestApi(url: String, options: any) {
if (!options) {
options = {}
}
/**
* @description: 响应拦截器
* @param {object} 当前请求成功回调数据
* @return 不return对象,则不返回数据
*/
// @ts-ignore
httpService.interceptor.response = (response: any) => {
console.log('个性化response....', JSON.stringify(response))
//判断返回状态 执行相应操作
return response;
}
/**
* @description: 请求拦截器
* @param {object} 当前请求配置参数
* @return 不return对象,则不发送当前请求
*/
// @ts-ignore
httpService.interceptor.request = (config: any) => {
console.log('config....', JSON.stringify(config))
//获取配置信息 统一添加配置
return config;
}
options.url = url
return httpService.request(options);
}
/**
* 请求接口日志记录
*/
function _reqlog(req: any) {
if (process.env.NODE_ENV === 'development') {
console.log("【" + req.requestId + "】 地址:" + req.url)
if (req.data) {
console.log("【" + req.requestId + "】 请求参数:" + JSON.stringify(req.data))
}
}
// 调接口异步写入日志数据库
}
/**
* 响应接口日志记录
*/
function _reslog(res: any) {
let _statusCode = res.statusCode;
if (process.env.NODE_ENV === 'development') {
console.log("【" + res.config.requestId + "】 地址:" + res.config.url)
if (res.config.data) {
console.log("【" + res.config.requestId + "】 请求参数:" + JSON.stringify(res.config.data))
}
console.log("【" + res.config.requestId + "】 响应结果:" + JSON.stringify(res))
}
// 除了接口服务错误外,其他日志调接口异步写入日志数据库
switch (_statusCode) {
case 200:
break;
case 401:
break;
case 404:
ToastWarn('找不了资源文件!')
break;
default:
ToastWarn('服务器异常!')
break;
}
}
/**
* 结果统一处理
*/
function _getResult(res: any) {
if (res.httpCode === 200) {
return { result: res.data };
}
}
export default requestApi;
三,使用封装方法(vue项目皆可按如下封装调用)
3.1,store中调用
第一步:引入接口
import { login } from '../../services/api/user'
第二步:在store中使用
login({ commit }, userInfo) {//store actions中的方法
const { username, password } = userInfo
return new Promise((resolve, reject) => {
login({ username: username.trim(), password: password }).then(response => {//登录接口
const { result } = response
console.log('result===', result)
resolve(result)
}).catch(error => {
reject(error)
})
})
}
第三步:在页面中调用actions方法
this.$store.dispatch('user/login', { username: this.username, password: this.password })
.then(() => {
ToastSuccess('登陆成功');
uni.switchTab({
url: '../index/index',
})
}).catch(() => {
ToastWarn('登陆失败');
});
3.2,封装统一的api入口
第一步:封装接口服务统一入口
在services中添加文件index.ts,添加如下代码
// https://webpack.js.org/guides/dependency-management/#requirecontext
const apisFiles = require.context('./api', false, /\.ts$/);
// you do not need `import user from './api/user'`
// it will auto require all api from api file
const apis = apisFiles.keys().reduce((apis, apiPath) => {
// set './user.ts' => 'user'
// const moduleName = apiPath.replace(/^\.\/(.*)\.\w+$/, '$1');//文件名
const value = apisFiles(apiPath);
apis = Object.assign({}, apis, value);//将所有可枚举属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。
return apis;
}, {});
export default {
...apis,
}
第二步:使用接口服务
添加api文件夹,并添加ts文件,名字可随意命名
import requestApi from '@/utils/request';
import { POST } from '@/utils/const';
/*
***登录***
*/
export function login(data: any) {
return requestApi('/api/v1/yingqi/user/login', {
method: POST,
data: {
loginCode: data.username,
password: data.password
},
});
}
第三步:统一接口入口
在main.js添加如下代码即可
import servicesApi from '@/services'; Vue.prototype.$servicesApi = servicesApi
第四步:页面中使用
this.$servicesApi.getSingleDataById().then((response: any) => {
const { result } = response
console.log('result===', result)
}).catch((error: any) => {
console.log('error===', error)
})
其他参数配置文件
export const GET: String = 'GET';
export const POST: String = 'POST';
export const PUT: String = 'PUT';
export const PATCH: String = 'PATCH';
export const DELETE: String = 'DELETE';
export const UPDATE: String = 'UPDATE';
//************************************************【 请求基础路径 】*************************************************
export const configBaseUrl: String = process.env.NODE_ENV === 'development' ?
'http://127.0.0.1:8880' // 开发环境
: 'http://x.x.x.x:8880' // 生产环境
;
下一章->uni-app,vue,react,Trao之缓存类封装
uni-app之网络请求的更多相关文章
- IOS 京东相关app 出现“网络请求失败,请检查您的网络设置”的解决办法
问题情况 在IOS系统下,下载安装或者更新新版的京东相关app之后,打开app直接就是“网络请求失败,请检查网络设置”,无论是数据连接还是wifi都试了,都是网络请求失败. 然而打开无线局域网-使用无 ...
- 分析移动端APP的网络请求抓包
为了方便,本文以 iOS 系统来进行演示. 使用代理 移动操作系统中都有可以设定系统代理的设置,比如在 iOS 中可以通过 Settings->WLAN 看到很多 Networks,通过点击它们 ...
- nginx android app 慢网络请求超时
最近遇到了android 在慢网络下面请求服务器报 java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by ...
- 使用Fiddler重定向App的网络请求
前言 开发中手机app只能访问生产环境地址,所以给开发调试带来不便,可以通过Fiddler的代理和urlreplace方式解决. 步骤 1.开启Fiddler的代理模式Tools->Teleri ...
- Swift - 网络请求报App Transport Security has blocked a cleartext错
使用Xcode7编写iOS9应用时,如果获取http://数据时会报如下错误: App Transport Security has blocked a cleartext HTTP (http:// ...
- 2020,最新APP重构:网络请求框架
在现在的app,网络请求是一个很重要的部分,app中很多部分都有或多或少的网络请求,所以在一个项目重构时,我会选择网络请求框架作为我重构的起点.在这篇文章中我所提出的架构,并不是所谓的 最好 的网络请 ...
- 使用charles proxy for Mac来抓取手机App的网络包
之前做Web项目的时候,经常会使用Fiddler(Windows下).Charles Proxy(Mac下)来抓包,调试一些东西:现在搞Android App开发,有时候也需要分析手机App的网络请求 ...
- 使用Charles对Android App的https请求进行抓包
本文背景 公司新项目要求抓取目前市面上一些热门App的数据,经过研究发现很多App的网络请求都使用https进行数据传输,这样问题就来了,http使用明文传输所有请求都能拦截到,而https请求无法拦 ...
- uni-app 网络请求
uni.request发起网络请求 url 开发者服务器接口地址 data 请求的参数 header method dataType responseType 设置响应的数据类型 statusCode ...
- 网络请求报错:The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.
iOS9引入了新特性App Transport Security (ATS).详情:App Transport Security (ATS) 如果你想设置不阻止任何网络,只需要在info.plist文 ...
随机推荐
- EffectiveJava-3
一.如果其他类型更适合,则尽量避免使用字符串 1. 字符串不适合代替枚举类型 2. 字符串不适合代替聚合类型,例如: String compoundKey = className+ "#&q ...
- 游戏辅助外gua篇:如何Dump内存获得游戏的辅助
转载请标明出处: https://dujinyang.blog.csdn.net/article/category/9267855 本文出自:[奥特曼超人的博客] 本篇邀请了 "阿七&quo ...
- python之小木马(文件上传,下载,调用命令行,按键监控记录)
window版 服务端: 开启两个线程,一个用来接收客户端的输入,一个用来监控服务端键盘的记录 客户端: get 文件(下载)put 文件(上传) window下cmd命令执行结果会直接打印出来,ke ...
- acm经验(转)
先简单介绍一下自己: 高中在OI打过一段时间酱油,大一后暑假进入ACM集训队,到大三寒假,总共一年半的ACM生涯. 总共参加了四场比赛:区域赛一银(2013长春)一铜(2013杭州)一铁(2012金华 ...
- [LINQ2Dapper]最完整Dapper To Linq框架(三)---实体类关系映射
此特性需要安装Kogel.Dapper.Mssql或者Oracle 3.06及以上版本,实体类层需要安装Kogel.Dapper.Extension 3.06及以上版本 目录 [LINQ2Dapper ...
- containerd 与安全沙箱的 Kubernetes 初体验
作者 | 易立 阿里云资深技术专家 containerd 是一个开源的行业标准容器运行时,关注于简单.稳定和可移植,同时支持 Linux 和 Windows. 2016 年 12 月 14 日,Do ...
- mybatis什么时候必须指定jdbcType
#{property,javaType=int,jdbcType=NUMERIC}如果一个列允许 null 值,并且会传递值 null 的参数,就必须要指定 JDBC Type
- Fuzzy模糊推导(Matlab实现)
问题呈述 在模糊控制这门课程中,学到了与模糊数学及模糊推理相关的内容,但是并不太清楚我们在选择模糊规则时应该如何处理,是所有的规则都需要由人手工选择,还是仅需要选择其中的一部分就可以了.因此,在课程示 ...
- [git]关于github的一些用法笔记(入门)
本视频来自于观看尚硅谷B站教学:https://www.bilibili.com/video/av10475153?from=search&seid=9735863941344749813 而 ...
- 苹果客户端input时页面自动放大的问题
一.问题: 最近在用vue测试的时候发现,安卓端在输入框input时不存在页面自动缩放的问题,苹果客户端认为是考虑到用户的体验效果,才出现输入框自动放大的功能.但也收到了不少用户反馈体验效果不周. 二 ...