uniapp封裝接口


一、新建文件夾及文件

 

 

二、request.js文件內封裝全局請求

const token = uni.getStorage('token')  // 獲取token值

const baseUrl = "http://**.***.***.***:5000" 

export default(url, method, params) => { // 傳參: 接口url,  method類型, params參數
    return new Promise((resolve, reject) => {
        uni.request({
            method: method,
            url: baseUrl + url,
            data: params,
            header: {
                token: token
            }
        })
        .then((response) => {
            // 請求成功后的處理
            if (response.data.retCode === 0) {
                resolve(response.data.strResult) // 返回內容根據后端返回數據決定
            } else {
                console.log('請求數據不存在')
            }
        }).catch((reject) => {
            // 請求失敗后的處理
            console.log('請求失敗')
        })
    })
    
}

 

 

三、api.js封裝各個接口

 

 

 

四、main.js引入api文件

 

 

 

五、接口調用

 

 調用格式:

this.$api.接口名稱(parmas參數).then().catch()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM