请求封装

// const app = getApp()
let isDev=true;//是否开发环境
let host = 'https://xs2b.kktijian.com/api'
let fileHost='https://xs2b.kktijian.com'
if (isDev) {
   host = 'http://172.18.185.21:9036/api';
   fileHost='http://172.18.185.21:9036'
}

const request = (url, options) => {
    // 请求之前提示加载中
    //console.log(wx.getStorageSync('output'))
    wx.showLoading({ title: '加载中...' })
    return new Promise((resolve, reject) => {
        wx.request({
            //   url: `${app.globalData.baseURL}${url}`,
            url: host + url,
            method: options.method,
            data: options.method === 'GET' ? options.data : JSON.stringify(options.data),
            header: {
                'Content-Type': 'application/json; charset=UTF-8',
                'Access-Token': wx.getStorageSync('output')
            },
            success: resolve,
            fail: reject,
            complete() {
                wx.hideLoading()
            }
        })
    })
}

const get = (url, options = {}) => {
    return request(url, { method: 'GET', data: options })
}

const post = (url, options) => {
    return request(url, { method: 'POST', data: options })
}

const put = (url, options) => {
    return request(url, { method: 'PUT', data: options })
}

// 不能声明DELETE(关键字)
const remove = (url, options) => {
    return request(url, { method: 'DELETE', data: options })
}

module.exports = {
    get,
    post,
    put,
    remove,
    host,
    fileHost
}

使用:
http.get('/Org/GetOwnOrgFiles', { pageIndex: 1, pageSize: 1000 }).then(res=>{})

posted on 2022-08-08 17:17  马玉豪  阅读(56)  评论(0)    收藏  举报

导航