uniapp下uni.request请求的封装

时间:2021-04-13 18:14:35

//核心代码,需要使用vue.use全局注册,只需参照博主的封装axios

// import {apiServer} from ‘./../../common.js‘
class http {
    static request(url, params = {}, method = "POST", contentType = "application/x-www-form-urlencoded") {
        return new Promise((resolve, reject) => {
            uni.request({
                url, //仅为示例,并非真实接口地址。
                data: params,
                header: {
                    ‘Content-Type‘: contentType
                },
                method,
                success: (res) => {
                    resolve(res.data.data);
                }
            });
        })
    }
}
export default http