当前位置:   article > 正文

uni-app的网络请求库封装及使用(同时支持微信小程序)

uni-app的网络请求库封装及使用(同时支持微信小程序)

其实uni-app中内置的uni.request()已经很强大了,简单且好用。为了让其更好用,同时支持拦截器,支持Promise 写法,特对其进行封装。同时支持H5和小程序环境,更好用啦。文中给出使用示例,可以看到使用变得如此简单。在此分享给有需要的小伙伴,需要的可以点击收藏。

前言

在uni-app中,常见的网络库主要是基于uni-app内置的uni.request()方法,这是一个统一的网络请求接口,支持HTTP和HTTPS协议,可以处理GET、POST等请求方法。这个API提供了基本的HTTP请求功能,可以满足大部分应用的网络通信需求。除此之外,由于uni-app是基于Vue.js的,所以也可以使用一些适用于前端的JavaScript网络库如axios 第三方库,支持Promise API,有丰富的拦截器、配置选项和错误处理。

为了兼容uni-app生态和微信小程序,推荐使用uni-app内置的uni.request()。

原因有以下几点:

集成性:uni.request()是uni-app框架的一部分,与uni-app的其他组件和服务紧密集成,使用起来更加方便,不需要额外安装和配置。

兼容性:uni.request()已经为uni-app的不同平台(包括iOS、Android、H5等)做了优化和适配,可以直接使用,而不需要担心跨平台兼容性问题。

简单易用:uni.request()的API设计简洁,易于理解和使用,对于大多数常规的网络请求任务,它提供了足够的功能。

性能:由于是原生实现,uni.request()通常会有更好的性能表现,特别是在处理数据量较大或需要高效网络交互的场景。

维护成本:使用uni.request(),开发者可以专注于业务逻辑,而不需要关注网络库的更新和维护。

如果你的项目中已经大量使用了axios,或者你需要利用axios提供的特定功能(如拦截器、取消请求、超时重试等),那么可以考虑继续使用axios。但需要注意的是,axios在uni-app中可能需要进行一些适配工作,尤其是小程序端。

网络库封装

原始的uni.request使用,举例如下:

  1. methods(){
  2. getSwiperList() { //方法名
  3. uni.request({
  4. url: "你的数据接口",
  5. success: (res) => { //如果成功了
  6. // 打印数据
  7. console.log(res);
  8. // 如果请求成功,就把数据转存起来,方便页面使用
  9. this.swipeList = res.data.message;
  10. },
  11. fail: (res) => {
  12. console.log('请求失败');
  13. }
  14. })
  15. }
  16. }

可以看出,虽然简单,但是不封装一下还是不够简洁。尤其是不支持Promise写法。

原生态写法太过于繁琐。假如我们一个项目有多个接口,那我们每一个接口,就要写一个uni.request({})方法,而每个方法就要包含url、data、method、header,等等,这样代码很明显就变得多而杂。而用了Promise写法,在编写代码时,我们就可以使用async和await写法来简化请求接口代码了。

封装后的使用,可以看出多么简单,示例如下:

  1. // api.js 获取当前正在热映电影
  2. export const getNowHot = async (start,count,city) => {
  3. try {
  4. console.log('getNowHot request');
  5. const response = await uni.$http.post('/movie/in_theaters',{
  6. apikey: uni.$apiKey,city:city,start:start,count:count});
  7. console.log(response);
  8. if (response.statusCode !== 200) {
  9. uni.showToast({
  10. title: '数据请求失败! ',
  11. duration: 1500,
  12. icon: 'none',
  13. });
  14. return [];
  15. }
  16. return response.data;
  17. } catch (error) {
  18. console.error('Network request failed:', error);
  19. uni.showToast({
  20. title: '网络请求失败! ',
  21. duration: 1500,
  22. icon: 'none',
  23. });
  24. return [];
  25. }
  26. };
  27. //index.vue 接口调用
  28. mounted() {
  29. console.log("mounted")
  30. //轮播图接口调用
  31. getSwiperList().then(item => {
  32. this.swiperList = item;
  33. });
  34. //获取豆瓣top250
  35. getTop250(0,5).then(item => {
  36. //this.swiperList = item;
  37. });
  38. // 获取最近热播
  39. getNowHot(0,2,"郑州").then(result => {
  40. //this.swiperList = item;
  41. console.log("getNowHot,result:");
  42. console.log(result);
  43. });
  44. }

上述示例为使用豆瓣影视的接口,获取郑州正在热映的电影。豆瓣接口curl测试如下:

curl --location --request POST 'https://api.douban.com/v2/movie/in_theaters?start=0&count=1' --data-urlencode 'apikey=xxxxxxxxx'

接口封装

  1. //utils/http.js
  2. class Request {
  3. constructor(options = {}) {
  4. // 请求的根路径
  5. this.baseUrl = options.baseUrl || ''
  6. // 请求的 url 地址
  7. this.url = options.url || ''
  8. // 请求方式
  9. this.method = 'GET'
  10. // 请求的参数对象
  11. this.data = null
  12. // header 请求头
  13. this.header = options.header || {}
  14. this.beforeRequest = null
  15. this.afterRequest = null
  16. }
  17. // 添加对header的支持
  18. _mergeHeaders(customHeader = {}) {
  19. return Object.assign({}, this.header, customHeader); // 合并默认header和自定义header
  20. }
  21. get(url, data = {}) {
  22. this.method = 'GET'
  23. this.url = this.baseUrl + url
  24. this.data = data
  25. return this._()
  26. }
  27. post(url, data = {},header = {}) {
  28. this.method = 'POST'
  29. this.url = this.baseUrl + url
  30. this.data = data
  31. this.header = this._mergeHeaders(header) // 合并header
  32. return this._()
  33. }
  34. put(url, data = {}) {
  35. this.method = 'PUT'
  36. this.url = this.baseUrl + url
  37. this.data = data
  38. return this._()
  39. }
  40. delete(url, data = {}) {
  41. this.method = 'DELETE'
  42. this.url = this.baseUrl + url
  43. this.data = data
  44. return this._()
  45. }
  46. _() {
  47. // 清空 header 对象
  48. this.header = {}
  49. // 请求之前做一些事
  50. this.beforeRequest && typeof this.beforeRequest === 'function' && this.beforeRequest(this)
  51. // 发起请求
  52. return new Promise((resolve, reject) => {
  53. let weixin = wx
  54. // 适配 uniapp
  55. if ('undefined' !== typeof uni) {
  56. weixin = uni
  57. }
  58. weixin.request({
  59. url: this.url,
  60. method: this.method,
  61. data: this.data,
  62. header: this.header,
  63. success: (res) => { resolve(res) },
  64. fail: (err) => { reject(err) },
  65. complete: (res) => {
  66. // 请求完成以后做一些事情
  67. this.afterRequest && typeof this.afterRequest === 'function' && this.afterRequest(res)
  68. }
  69. })
  70. })
  71. }
  72. }
  73. export const $http = new Request()

如何使用

在main.js中引入该模块封装,并将其挂在全局的uni.$http上即可。如下:

  1. import App from './App'
  2. // #ifndef VUE3
  3. import Vue from 'vue'
  4. Vue.config.productionTip = false
  5. App.mpType = 'app'
  6. const app = new Vue({
  7. ...App
  8. })
  9. app.$mount()
  10. // #endif
  11. // #ifdef VUE3
  12. import { createSSRApp } from 'vue'
  13. export function createApp() {
  14. const app = createSSRApp(App)
  15. return {
  16. app
  17. }
  18. }
  19. // #endif
  20. import { $http } from './utils/http.js'
  21. uni.$http = $http
  22. // 配置请求根路径
  23. $http.baseUrl = 'https://api.douban.com/v2'
  24. uni.$apiKey = 'xxxxxxxxx'
  25. // 请求开始之前做一些事情
  26. $http.beforeRequest = function (options) {
  27. uni.showLoading({
  28. title: '数据加载中...',
  29. })
  30. }
  31. // 请求完成之后做一些事情
  32. $http.afterRequest = function () {
  33. uni.hideLoading()
  34. }

在其他文件夹,如api文件夹下,可以愉快的写接口啦,举例如下:

  1. // api/home.js
  2. export const getSwiperList = async () => {
  3. try {
  4. console.log('getSwiperList request');
  5. const response = await uni.$http.get('/api/v1/home/swiperdata');
  6. console.log(response.data.list);
  7. if (response.statusCode !== 200) {
  8. uni.showToast({
  9. title: '数据请求失败! ',
  10. duration: 1500,
  11. icon: 'none',
  12. });
  13. return [];
  14. }
  15. return response.data.list;
  16. } catch (error) {
  17. console.error('Network request failed:', error);
  18. uni.showToast({
  19. title: '网络请求失败! ',
  20. duration: 1500,
  21. icon: 'none',
  22. });
  23. return [];
  24. }
  25. };
  26. export const getTop250 = async (start,count) => {
  27. try {
  28. console.log('getTop250 request');
  29. const response = await uni.$http.post('/movie/top250', {apikey: uni.$apiKey,
  30. start:start,count:count},{'Content-Type': 'application/x-www-form-urlencoded'});
  31. console.log(response);
  32. if (response.statusCode !== 200) {
  33. uni.showToast({
  34. title: '数据请求失败! ',
  35. duration: 1500,
  36. icon: 'none',
  37. });
  38. return [];
  39. }
  40. return response.data.list;
  41. } catch (error) {
  42. console.error('Network request failed:', error);
  43. uni.showToast({
  44. title: '网络请求失败! ',
  45. duration: 1500,
  46. icon: 'none',
  47. });
  48. return [];
  49. }
  50. };
  51. // 获取当前正在热映电影
  52. export const getNowHot = async (start,count,city) => {
  53. try {
  54. console.log('getNowHot request');
  55. const response = await uni.$http.post('/movie/in_theaters',{
  56. apikey: uni.$apiKey,city:city,start:start,count:count});
  57. console.log(response);
  58. if (response.statusCode !== 200) {
  59. uni.showToast({
  60. title: '数据请求失败! ',
  61. duration: 1500,
  62. icon: 'none',
  63. });
  64. return [];
  65. }
  66. return response.data;
  67. } catch (error) {
  68. console.error('Network request failed:', error);
  69. uni.showToast({
  70. title: '网络请求失败! ',
  71. duration: 1500,
  72. icon: 'none',
  73. });
  74. return [];
  75. }
  76. };

写在最后

最后,附上完整的工程项目模版。为了更通用,这个是从我的业余项目(爱看电影app小程序) 中抽离出来的工程模版和示例。带网络库的封装和可用的豆瓣影视接口封装,以及个人中心页面。可以作为工程模版或小项目练手,分享给有需要的小伙伴。

资源下载地址:

https://download.csdn.net/download/qq8864/89377440

其他资源

豆瓣接口API使用_热映电影api接口怎么用-CSDN博客

https://feizhaojun.com/?p=3813

Movie API Doc | doubanapi

uniapp 请求解决跨域问题_uniapp跨域请求-CSDN博客

豆瓣API常用api总结实例_douban api-CSDN博客

组件使用的入门教程 | uni-app官网

微信小程序 --- wx.request网络请求封装-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/651911
推荐阅读
相关标签
  

闽ICP备14008679号