方法一、将公共部分提取出来定义为baseURL变量

简化url,把里面公共部分提取出来。如https://api-hmugo-web.itheima.net/api/public/v1/categories提取出来后变成/categories,公共部分为https://api-hmugo-web.itheima.net/api/public/v1

 

 方法二、公共部分提取出来放到app.js的globalData中

globalData: {
    // serverApi: "http://c15112b047.iok.la",
    // staticApi: "http://c15112b047.iok.la/uploadPath",
    serverApi: "http://localhost:8008",
    staticApi: "http://localhost:8008/uploadPath",
    userInfo: null,
    token: null
  }
function get(url, callback) {
    wx.request({
        method: 'GET',
        url: app.globalData.serverApi + url,
        header: {
            'content-type': 'application/json',
            'Authorization': app.globalData.token
        },
        success(res) {
            callback(res);
        }
    })
}

 

posted on 2020-11-17 17:44  周文豪  阅读(929)  评论(0)    收藏  举报