0
点赞
收藏
分享

微信扫一扫

uni-app 服务端请求封装

蒸熟的土豆 2021-09-24 阅读 55
前端Vuemui

http.js

// created by wangyong for uni-app request 2019.11.22
const baseURL = 'http://localhost:8888/vec';
const http = (options) => {
    return new Promise((resolve, reject) => {
        uni.showLoading({
            title: '加载中...',
            mask: options.load || false // 默认遮罩出现可以继续操作
        });
        try{
            uni.request({
                url: (options.baseURL || baseURL) + options.url,
                method: options.method || 'POST', // 默认为POST请求
                data: options.data, //请求超时在manifest.json配置
                header: {
                    'v-token': '333333',
                    'Content-Type': options.header == 'form' ? 'application/x-www-form-urlencoded' : 'application/json'
                },
                success: res => {
                    resolve(res.data)
                },
                fail: (err) => {
                    reject(err.data);
                    console.log(err);
                    uni.showToast({
                        title: '请检查网络连接',
                        icon: 'none'
                    })
                    /*错误码处理
                    let code = err.data.code; 
                    switch (code) {
                        case 1000:
                            break;
                        default:
                            break;
                    } */
                },
                complete: () => {
                    uni.hideLoading();
                }
            });
        }catch(e){
            uni.hideLoading();
            uni.showToast({
                title: '服务端异常',
                icon: 'none'
            })
        }
        
    })
}

export default http

main.js 添加

import http from './common/http.js'

Vue.prototype.$HTTP = http

调用实例:

this.$HTTP({
  method: 'POST',
  url: '/api/news',
  data: {},
 // baseURL:'http://www.buwang.com'
 // header:'form' 
}).then((res) =>{
  console.log(res)
}
// (err)=>{ console.log(err) }
)
举报

相关推荐

0 条评论