uniapp----Request请求封装js
一、Request请求封装
1. 需要封装的js代码:
const BASE_URL = 'http://xxx.xxx.com/index.php/jsf/index/';
export const myRequest = (url, method, data = {
}, header = {
}) => {
uni.showLoading();
return new Promise((resolve, reject) => {
uni.request({
url: BASE_URL + url,
method: method || 'GET',
header: {
'content-type': 'application/x-www-form-urlencoded'
} || header,
data: data || {
},
success: (res) => {
uni.hideLoading();
let code = res.data.code;
if (code == 1) {
resolve(res.data)
} else {
uni.showToast({
title: res.data.msg,
icon: 'none'
})
func.alert(res.data.msg)
}
},
fail: (err) => {
uni.showToast({
title: '请求接口失败',
icon: 'none'
})
reject(err)
}
})
})
}
2.具体的引用:
2.1再main.js中定义到全局
import {
myRequest
} from './config/api'
Vue.prototype.$myRequest = myRequest
2.2在需要用的页面中进行如下引用
this.$myRequest('checkUserMemberType', 'POST', {
uid: this.uid,
}).then(v => {
})