uniapp封装Promise请求
创建文件./unit/http.js
// 封装promise网络请求
const BASE_URL = "http://www.51cdev.com/api/"
export const myRequest = (options ) => {
const {url,method,data,timeout,header} = options
// console.log(options);
return new Promise((resolve, reject) => {
uni.request({
header: header || {
"content-type": "application/x-www-form-urlencoded"
},
url: BASE_URL + url,
method: method || 'GET',
data: data || {},
timeout: timeout || 3000,
success: (res) => {
// const sign = uni.getStorageSync('sign')
if (res.statusCode !== 200) {
uni.showToast({
title: '请求数据失败',
icon: 'none',
duration: 1000
});
}
resolve(res)
},
fail: (err) => {
uni.showToast({
title: '请求数据失败',
icon: 'none',
duration: 1000
});
reject(err)
}
})
})
}main.js全局引入
import {myRequest} from './unit/http.js'
Vue.prototype.$myRequest = myRequest //自定义url求请简单调用
async getpageData(){
const res = await this.$myRequest({
url:'goods/pageData'
});
console.log(res );
}