uniapp网络异步请求封装

uniapp网络异步请求封装

api.js

const baseUrl = '';

function request(obj) {
	let url = baseUrl + obj.url;
	let method = obj.method || 'GET';
	let data = obj.data || {}
	let success = obj.success || {}; //成功后的回调
	// let header = obj.header || {}
	let token = uni.getStorageSync('token') || ''
	
	return new Promise((reslove, rej) => {
		uni.request({
			url: url,
			data: data,
			method: method,
			header: {
				'token': token
			},
			success: (res) => {
				if (res.statusCode === 200) {
					reslove(res.data)

				} else {
					uni.showToast({
						position: top,
						icon: 'none',
						title: res.statusCode,
						duration: 2000
					})

				}
			},
			fail: (err) => {
				console.log(err);
				rej(err)
			}
		})
	})

}

export default request

apiList

import request from './api.js'

export default 

	// 注册
	DoRegister(data) {
		return request({
			url: '/register',
			method: 'post',
			data
		})
	}
}

使用

import apiList from 'apiList.js'

apiList.DoRegister(data).then(res => {
	
})

你可能感兴趣的:(uniapp,javascript,vue.js,html,uni-app,app)