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之网络请求的更多相关文章

  1. IOS 京东相关app 出现“网络请求失败,请检查您的网络设置”的解决办法

    问题情况 在IOS系统下,下载安装或者更新新版的京东相关app之后,打开app直接就是“网络请求失败,请检查网络设置”,无论是数据连接还是wifi都试了,都是网络请求失败. 然而打开无线局域网-使用无 ...

  2. 分析移动端APP的网络请求抓包

    为了方便,本文以 iOS 系统来进行演示. 使用代理 移动操作系统中都有可以设定系统代理的设置,比如在 iOS 中可以通过 Settings->WLAN 看到很多 Networks,通过点击它们 ...

  3. nginx android app 慢网络请求超时

    最近遇到了android 在慢网络下面请求服务器报 java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by ...

  4. 使用Fiddler重定向App的网络请求

    前言 开发中手机app只能访问生产环境地址,所以给开发调试带来不便,可以通过Fiddler的代理和urlreplace方式解决. 步骤 1.开启Fiddler的代理模式Tools->Teleri ...

  5. Swift - 网络请求报App Transport Security has blocked a cleartext错

    使用Xcode7编写iOS9应用时,如果获取http://数据时会报如下错误: App Transport Security has blocked a cleartext HTTP (http:// ...

  6. 2020,最新APP重构:网络请求框架

    在现在的app,网络请求是一个很重要的部分,app中很多部分都有或多或少的网络请求,所以在一个项目重构时,我会选择网络请求框架作为我重构的起点.在这篇文章中我所提出的架构,并不是所谓的 最好 的网络请 ...

  7. 使用charles proxy for Mac来抓取手机App的网络包

    之前做Web项目的时候,经常会使用Fiddler(Windows下).Charles Proxy(Mac下)来抓包,调试一些东西:现在搞Android App开发,有时候也需要分析手机App的网络请求 ...

  8. 使用Charles对Android App的https请求进行抓包

    本文背景 公司新项目要求抓取目前市面上一些热门App的数据,经过研究发现很多App的网络请求都使用https进行数据传输,这样问题就来了,http使用明文传输所有请求都能拦截到,而https请求无法拦 ...

  9. uni-app 网络请求

    uni.request发起网络请求 url 开发者服务器接口地址 data 请求的参数 header method dataType responseType 设置响应的数据类型 statusCode ...

  10. 网络请求报错: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文 ...

随机推荐

  1. 洛谷 P 5 3 0 4 [GXOI/GZOI2019]旅行者

    题目描述 J 国有 n 座城市,这些城市之间通过 m 条单向道路相连,已知每条道路的长度. 一次,居住在 J 国的 Rainbow 邀请 Vani 来作客.不过,作为一名资深的旅行者,Vani 只对 ...

  2. DAY 5 搜索

    搜索 开篇: mayan游戏(noip2011 day1 T3) 这道题就是个码量题,老师讲题时淡淡的说写完前两题就花一个半小时了,最后一题不快点打会调不出来,对于一个三个半小时就写两题的蒟蒻来说这. ...

  3. TCP协议--TCP三次握手和四次挥手

    TCP三次握手和四次挥手 TCP有6种标示:SYN(建立联机) ACK(确认) PSH(传送) FIN(结束) RST(重置) URG(紧急) 一.TCP三次握手   第一次握手 客户端向服务器发出连 ...

  4. 简述JMM

    一.很多初学者分不清JMM和JVM的内存模型,本篇只是简要的谈一谈什么是JMM,并不深入探讨. 示意图A: 在多线程操纵共享资源时,并不是对资源本身进行的操作,而是将共享资源的副本复制了一份到自己的私 ...

  5. Java基础系列5:深入理解Java异常体系

    该系列博文会告诉你如何从入门到进阶,一步步地学习Java基础知识,并上手进行实战,接着了解每个Java知识点背后的实现原理,更完整地了解整个Java技术体系,形成自己的知识框架. 前言: Java的基 ...

  6. EFK教程 - ElasticSearch高性能高可用架构

    通过将elasticsearch的data.ingest.master角色进行分离,搭建起高性能+高可用的ES架构 作者:"发颠的小狼",欢迎转载与投稿 目录 ▪ 用途 ▪ 架构 ...

  7. ArcGIS API For Javascript:热力图不同级别下的优化方法

    我们在地图缩放的不同级别下,热力图的显示效果会不同,由于点密度与模糊参数默认是固定的,因此需要对参数进行动态修改,以满足不同缩放级别下可以得到较好的显示效果. 思路是监听地图缩放级别,将地图缩放级别作 ...

  8. python:collections模块

    Counter类 介绍:A counter tool is provided to support convenient and rapid tallies 构造:class collections. ...

  9. oracle日期时间范围查询

    Oracle的日期时间范围查询 字段为:字符串类型(char),长度为:10 SELECT * FROM testdatetime t WHERE = AND t.createdate >= ' ...

  10. ZeroC ICE的远程调用框架 Callback(一)-AMI异步方法调用框架

    Ice框架提供了不少回调设施,其中一些是使用Ice远程调用进行ami模式或amd模式的支撑.本篇来看一下用于代理端的回调设施. Ice代码中有好几个Callback相关命名的基类,并且slice还会为 ...