当前位置: 首页 > article >正文

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

 主要封装函数代码:

interface HttpOptions {
    url: string;
    method?: string;
    headers?: { [key: string]: string };
    data?: any;
}

class Http {

    private timeout: number;
    private baseUrl: string;

    public constructor() { 
        this.timeout = 60 * 1000;
        this.baseUrl = 'http://localhost:8088/';
    }

    //全局设置地址
    public setBaseUrl(url: string) {
        this.baseUrl = url;
    }

    //全局设置超时时间
    public setTimeout(timeout: number) {
        this.timeout = timeout;
    }

    //小程序发送请求
    public async request(options: HttpOptions) {
        options.url = this.baseUrl + options.url;
        return this.completeRequest(options);
    }

    //小程序上传文件
    public async uploadFile(options: HttpOptions) {
        const { url, headers = { 'Content-Type': 'multipart/form-data' }, data } = options;
        return new Promise((resolve, reject) => {
            uni.uploadFile({
                url: this.baseUrl + url,
                header: headers,
                files: data,
                success: (res) => {
                    resolve(res.data)
                },
                fail: (err) => {
                    reject(err)
                }
            })
        })
    }

    //完整路经的请求
    public async completeRequest(options: HttpOptions) {
        const { url, method = 'GET', headers = { 'Content-Type': 'application/json' }, data } = options;
        return new Promise((resolve, reject) => {
            uni.request({
                url: url,
                timeout: this.timeout,
                method: method as 'GET' | 'POST',
                header: headers,
                data: data,
                success: (res) => {
                    resolve(res.data)
                },
                fail: (err) => {
                    reject(err)
                }
            })
        })
    }
}

//导出实例和类
export default new Http();

export { Http };

使用方法

步骤一:在main文件中全局依赖注入
import { createSSRApp } from "vue";
import App from "./App.vue";
//导入加载页面组件
import loading from "./component/loading.vue";
import drawer from "./component/drawer.vue";
import wxShare from "./util/wxShare";
//导入HTTP请求
import http from "./util/http";
export function createApp() {
  const app = createSSRApp(App);
  app.component("qgy-loading", loading);
  app.component("qgy-drawer", drawer);
  app.mixin(wxShare);
  //全局挂载http
  app.provide("$http", http);
  return {
    app,
  };
}
步骤二:在组件中导入
import { inject, onMounted } from 'vue'
import { Http } from '@/util/http';

const http:Http|undefined = inject('$http');
步骤三:设置全局配置
//设置全局配置
http.setBaseUrl("http://localhost:8088/")
http.setTimeout(60 * 1000)
步骤四:调用方法
  1. request():正常发送请求

  2. uploadFile():上传文件

  3. completeRequest():可以配置完整url的路径发送请求


http://www.kler.cn/a/549278.html

相关文章:

  • Deep seek学习日记1
  • 8. Docker 常规安装简介(安装 Tomcat ,安装 MySQL,安装Redis 同时指定安装的版本)
  • WebMvcConfigurer 介绍
  • 有关计算机的英语单词、短语、句子
  • Python 文本探秘:正则表达式的易错迷宫穿越 -- 7. 正则表达式
  • Redis初阶笔记
  • Qt的QListWidget样式设置
  • Unity中如何判断URL是否为RTSP或RTMP流
  • C语言----共用体
  • Linux基础21-C语言篇之流程控制Ⅱ【入门级】
  • 基于SSM+uniapp的鲜花销售小程序+LW示例参考
  • Qt笔记31-69
  • 使用 GPT-SoVITS 克隆声音,很详细
  • 计算机视觉-尺度不变区域
  • vue3+element-plus中的el-table表头和el-table-column内容全部一行显示完整(hook函数)
  • Field ‘id‘ doesn‘t have a default value
  • Java 实现 Redis中的GEO数据结构
  • AtCoder Beginner Contest 393 —— E - GCD of Subset 补题 + 题解 python
  • Ubuntu添加桌面快捷方式
  • 深度学习在天文观测中的应用:解锁宇宙的奥秘