首页 > 其他分享 >请求封装

请求封装

时间:2022-09-21 10:58:21浏览次数:61  
标签:封装 请求 url request options const data method

// const app = getApp()
let isDev=true;//是否开发环境
let host = 'https://xs2b.kktijian.com/api'
let fileHost='https://xs2b.kktijian.com'
let versions = __wxConfig.envVersion;//获取版本状态develop:开发版,trial:体验版,release:正式版

if (versions == 'develop') {
   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.post('/FileManage/FileDelete', {
                fileIDs: arr//参数
              }).then(res => {
              })

 

标签:封装,请求,url,request,options,const,data,method
From: https://www.cnblogs.com/Ma-YuHao/p/16714815.html

相关文章