uniapp的ajax封装请求

uniapp的ajax封装请求

const baseUrl = 'http://172.16.10.5:8802';
const httpRequest = (opts, data) => {
    let httpDefaultOpts = {
        url: baseUrl + opts.url,
        data: data,
        method: opts.method,
        header: opts.method == 'get' ? {
            'X-Requested-With': 'XMLHttpRequest',
            "Accept": "application/json",
            "Content-Type": "application/json; charset=UTF-8"
        } : {
            'X-Requested-With': 'XMLHttpRequest',
            "Content-Type": "application/json; charset=UTF-8"
            // 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        },
        dataType: 'json',
    }
    let promise = new Promise(function(resolve, reject) {
        uni.request(httpDefaultOpts).then(
            (res) => {
                resolve(res[1])
            }
        ).catch(
            (response) => {
                reject(response)
            }
        )
    })
    return promise
};
//带Token请求
const httpTokenRequest = (opts, data) => {
    let token = uni.getStorageSync('userToken');
    uni.getStorage({
        key: 'token',
        success: function(ress) {
            token = ress.data
        }
    });
    //此token是登录成功后后台返回保存在storage中的
    let httpDefaultOpts = {
        url: baseUrl + opts.url,
        data: data,
        method: opts.method,
        header: opts.method == 'get' ? {
            'Authorization': token,
            'X-Requested-With': 'XMLHttpRequest',
            "Accept": "application/json",
            "Content-Type": "application/json; charset=UTF-8"
            // "Content-Type": "application/json; charset=UTF-8"
        } : {
            'Authorization': token,
            'X-Requested-With': 'XMLHttpRequest',
            "Content-Type": "application/json; charset=UTF-8"
            // 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        },
        dataType: 'json',
    }
    let promise = new Promise(function(resolve, reject) {
        uni.request(httpDefaultOpts).then(
            (res) => {
                // console.log(3333333333333333333,res)
                resolve(res[1])
            }
        ).catch(
            (response) => {
                reject(response)
            }
        )
    })
    return promise
};

export default {
    baseUrl,
    httpRequest,
    httpTokenRequest
}

组件使用

//引入组件
    import http from '@/common/api/api.js'
//使用方法
        // 详情页页面
            getDetials() {
                let opts = {
                    url: '/applet/findDeviceId',
                    method: 'get'
                };
                let param = {
                    deviceName: this.deviceName,
                    openid: this.openid,
                    pageNo: this.pageNo,
                    pageSize: this.pageSize,
                    deviceType: this.deviceType
                };
                http.httpRequest(opts, param).then(res => {
                    console.log(res, "2222222");
                    // _this.isRotate = false
                    uni.hideLoading()
                    if (res.data.code == 200) {
                        // console.log(res.data.result)
                        this.current = res.data.result.current
                        this.total = res.data.result.total
                        this.tableList = res.data.result.records
                    } else {
                        uni.showToast({
                            icon: 'none',
                            position: 'bottom',
                            title: res.data.message
                        });
                    }
                })
            },

 

posted @ 2020-11-03 10:53  DreamTraveler  阅读(2087)  评论(0编辑  收藏  举报
1 2 3
4