微信小程序的哀求函数封装(ts版本,uniapp开发)

打印 上一主题 下一主题

主题 864|帖子 864|积分 2592

 主要封装函数代码:

  1. interface HttpOptions {
  2.     url: string;
  3.     method?: string;
  4.     headers?: { [key: string]: string };
  5.     data?: any;
  6. }
  7. class Http {
  8.     private timeout: number;
  9.     private baseUrl: string;
  10.     public constructor() {
  11.         this.timeout = 60 * 1000;
  12.         this.baseUrl = 'http://localhost:8088/';
  13.     }
  14.     //全局设置地址
  15.     public setBaseUrl(url: string) {
  16.         this.baseUrl = url;
  17.     }
  18.     //全局设置超时时间
  19.     public setTimeout(timeout: number) {
  20.         this.timeout = timeout;
  21.     }
  22.     //小程序发送请求
  23.     public async request(options: HttpOptions) {
  24.         options.url = this.baseUrl + options.url;
  25.         return this.completeRequest(options);
  26.     }
  27.     //小程序上传文件
  28.     public async uploadFile(options: HttpOptions) {
  29.         const { url, headers = { 'Content-Type': 'multipart/form-data' }, data } = options;
  30.         return new Promise((resolve, reject) => {
  31.             uni.uploadFile({
  32.                 url: this.baseUrl + url,
  33.                 header: headers,
  34.                 files: data,
  35.                 success: (res) => {
  36.                     resolve(res.data)
  37.                 },
  38.                 fail: (err) => {
  39.                     reject(err)
  40.                 }
  41.             })
  42.         })
  43.     }
  44.     //完整路经的请求
  45.     public async completeRequest(options: HttpOptions) {
  46.         const { url, method = 'GET', headers = { 'Content-Type': 'application/json' }, data } = options;
  47.         return new Promise((resolve, reject) => {
  48.             uni.request({
  49.                 url: url,
  50.                 timeout: this.timeout,
  51.                 method: method as 'GET' | 'POST',
  52.                 header: headers,
  53.                 data: data,
  54.                 success: (res) => {
  55.                     resolve(res.data)
  56.                 },
  57.                 fail: (err) => {
  58.                     reject(err)
  59.                 }
  60.             })
  61.         })
  62.     }
  63. }
  64. //导出实例和类
  65. export default new Http();
  66. export { Http };
复制代码
利用方法

步调一:在main文件中全局依靠注入

  1. import { createSSRApp } from "vue";
  2. import App from "./App.vue";
  3. //导入加载页面组件
  4. import loading from "./component/loading.vue";
  5. import drawer from "./component/drawer.vue";
  6. import wxShare from "./util/wxShare";
  7. //导入HTTP请求
  8. import http from "./util/http";
  9. export function createApp() {
  10.   const app = createSSRApp(App);
  11.   app.component("qgy-loading", loading);
  12.   app.component("qgy-drawer", drawer);
  13.   app.mixin(wxShare);
  14.   //全局挂载http
  15.   app.provide("$http", http);
  16.   return {
  17.     app,
  18.   };
  19. }
复制代码
步调二:在组件中导入

  1. import { inject, onMounted } from 'vue'
  2. import { Http } from '@/util/http';
  3. const http:Http|undefined = inject('$http');
复制代码
步调三:设置全局设置

  1. //设置全局配置
  2. http.setBaseUrl("http://localhost:8088/")
  3. http.setTimeout(60 * 1000)
复制代码
步调四:调用方法


  • request():正常发送哀求
  • uploadFile():上传文件
  • completeRequest():可以设置完备url的路径发送哀求

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

欢乐狗

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表