uni-app接口封装

本文介绍了如何在uni-app中进行接口请求的封装。首先在common文件夹下创建request.js,然后在各个页面上调用这些封装好的接口,实现统一的请求管理。
部署运行你感兴趣的模型镜像

1:common中创建request.js

const baseurl = 'http://kjs.demo.dopaik.cn';

const main = opts => {
	// opts.url = baseurl + opts.url;

	const regHost = /^(http|https|\\)/;
	// test() 方法用于检测一个字符串是否匹配某个模式.
	if (!regHost.test(opts.url)) {
		opts.url = baseurl + opts.url;
	}

	!opts.method && (opts.method = 'get');
	// toLocaleUpperCase() 方法用于把字符串转换为大写。
	opts.method = opts.method.toLocaleUpperCase();
	!opts.dataType && (opts.dataType = 'json');
	// return uni.request(opts);
	return request(opts);
}

const request = opts => {
	// Promise 是异步编程的一种解决方案,其实是一个构造函数,自己身上有all、reject、resolve这几个方法,原型上有then、catch等方法。
	return new Promise((resolve, reject) => {
		uni.request(opts).then(result => {
			resolve(result[1].data);
		}).catch(reject)
	});
}

export default main

2:页面上进行调用

import utils from '@/common/request.js';

//定义传进去的参数
    const opts = {
            url: '/api/commodity/query',
            data: {
                business_sign: this.business_sign
            }
        };
        //接口调用
        utils.main(opts).then(
            res => {
                this.listdata = res.data;
            },
            error => {
                console.log('失败');
            }
        );

您可能感兴趣的与本文相关的镜像

HunyuanVideo-Foley

HunyuanVideo-Foley

语音合成

HunyuanVideo-Foley是由腾讯混元2025年8月28日宣布开源端到端视频音效生成模型,用户只需输入视频和文字,就能为视频匹配电影级音效

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值