uniApp小程序的api封装

1 篇文章 0 订阅
1 篇文章 0 订阅

文件结构:

1.在api文件夹里面新建文件 https.js 进行请求封装

// 区分开发环境和生产环境  接口地址
const baseUrl = 'https://test/'; //这里要改成你的接口地址

export function https(config) {
	//显示loading
	wx.showLoading({
		title: '加载中...'
	});
	//设置请求头
	config.header = {
		'content-type': 'application/json',
	};
	//检查缓存中有没有token
	const token = uni.getStorageSync('graduateToken');
	if (token) {
		config.header.Authorization = token
		// let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
		// let curRoute = routes[routes.length - 1].route //获取当前页面路由
		// if (routes == 'pages/login/login') {
		// 	uni.switchTab({
		// 		url: '../index/index',
		// 	});
		// }
	} else {
		uni.redirectTo({ 
			url: '/pages/login/login.vue'
		})
	}
	config.url = baseUrl + config.url; // 请求地址
	let promise = new Promise(function(resolve, reject) {
		uni.request(config).then(res => {
			wx.hideLoading() //隐藏loading
			if (res[0]) {
				uni.showToast({
					title: "数据获取失败",
					icon: "none",
					duration: 1500
				})
				resolve(false);
			} else {
				let data = res[1].data;
				if (data.code == 1) {
					resolve(data);
				} else {
					resolve(data);
				}
			}
		}).catch(error => {
			wx.hideLoading() //隐藏loading
			reject(error);
		})
	})
	return promise
}

2.再新建 api.js 进行请求地址封装

import { https } from './https.js'; // 引入刚刚封装好的https

class api {
	//获取学费列表
	async studentUserMsgsGetTuitionALL(params) {
		const res = await https({
			url: 'api/v1/getTuitionAll', //这里放你的请求路径
			method: 'post',
			data: params
		})
		return res
	}
	//获取合同列表
	async bktstudentContract(params) {
		const res = await https({
			url: 'api/v1/index',
			method: 'post',
			data: params
		})
		return res
	}
	//查看收据
	async studentUserGetReceipt(params) {
		const res = await https({
			url: 'api/v1/getReceipt',
			method: 'post',
			data: params
		})
		return res
	}

}
export default new api()

3.在 main.js 中全局引入挂载

import api from './pages/api/api.js'
Vue.prototype.$api = api

4.页面直接使用

			// 获取合同列表
			bktstudentContract() {
				this.$api.bktstudentContract().then((res) => {
					if (res.code == 200) {
						this.contractList = res.data;
					} else {
						uni.showToast({
							title: res.data,
							icon: "none",
							position: 'top',
							duration: 2000
						});
					}
				});
			},


            //传参的这样写:

			// 获取收据列表
			studentUserGetReceipt() {
				const params = {
					student_msg_id: this.infoId,
				};
				this.$api.studentUserGetReceipt(params).then((res) => {
					if (res.code == 200) {
						this.receiptList = res.data;
					} else {
						uni.showToast({
							title: res.data,
							icon: "none",
							position: 'top',
							duration: 2000
						});
					}
				});
			},

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值