uni-app 請求 uni.request封裝使用


對uni.request的一些共同參數進行簡單的封裝,減少重復性數據請求代碼。方便全局調用。

先在目錄下創建 utils 和 common 這2個文件夾

 

utils 是存放工具類的,common 用來放置常用方法的

之后在utils 中創建 requset.js 用來放置 uni.request 的請求方法,在對其進行簡單的封裝。

requset.js 代碼
import operate from '../common/operate.js'
// vuex 的使用  詳情參考官網 https://uniapp.dcloud.io/vue-vuex
import store from '../store/index.js'  

export default class Request {
    http(param) {
        // 請求參數
        var url = param.url,
            method = param.method,
            header = {},
            data = param.data || {},
            token = param.token || "",
            hideLoading = param.hideLoading || false;

        //拼接完整請求地址
        var requestUrl = operate.api + url;

        //請求方式:GET或POST(POST需配置
        // header: {'content-type' : "application/x-www-form-urlencoded"},)
        if (method) {
            method = method.toUpperCase(); //小寫改為大寫
            if (method == "POST") {
                header = {
                    'content-type': "application/x-www-form-urlencoded"
                };
            } else {
                header = {
                    'content-type': "application/json"
                };
            }
        }

        //加載圈
        if (!hideLoading) {
            uni.showLoading({
                title: '加載中...'
            });
        }

        // 返回promise
        return new Promise((resolve, reject) => {
            // 請求
            uni.request({
                url: requestUrl,
                data: data,
                method: method,
                header: header,
                success: (res) => {
                    // 判斷 請求api 格式是否正確
                    if (res.statusCode && res.statusCode != 200) {
                        uni.showToast({
                            title: "api錯誤" + res.errMsg,
                            icon: 'none'
                        });
                        return;
                    }
                    // code判斷:200成功,不等於200錯誤
                    if (res.data.code) {
                        if (res.data.code != '200') {
                            uni.showToast({
                                title: "" + res.data.msg,
                                icon: 'none'
                            });
                            return;
                        }
                    } else {
                        uni.showToast({
                            title: "code!=200" + res.data.msg,
                            icon: 'none'
                        });
                        return;
                    }
                    // 將結果拋出
                    resolve(res.data)
                },
                //請求失敗
                fail: (e) => {
                    uni.showToast({
                        title: "" + e.data.msg,
                        icon: 'none'
                    });
                    resolve(e.data);
                },
                //請求完成
                complete() {
                    //隱藏加載
                    if (!hideLoading) {
                        uni.hideLoading();
                    }
                    resolve();
                    return;
                }
            })
        })
    }
}

在common 中分別創建 operate.js 和 api.js

operate.js 用來放置請求接口 api 地址

export default {
    //接口
    api: "http://192.168.208.126:8080",
}

api.js 是用來調用我們封裝好的 uni.request ,並且統一管理請求接口,在后續開發中只需要在頁面中調用 api.js中請求即可

import Request from '@/utils/requset.js'
let request = new Request().http

//全局定義請求頭
export default {
    // 請求樣式
    classifyLeft: function(data) {
        return request({
            url: "/category/list", //請求頭
            method: "GET", //請求方式
            data: data, //請求數據
        })
    },
}
/*
請求樣式:
    自定義名字: function(data) {
        return request({
            url: "/banner", //請求頭
            method: "GET", //請求方式 
            data: data,    //請求數據
            token: token, // 可傳  
            hideLoading: false, //加載樣式
        })
    },
*/

api.js的調用可以在main.js 中進行全局調用,也可以在需要的頁面中調用,可根據實際情況來決定是否全局 調用。下面只介紹全局調用

在mian.js中導入api.js

1. 在main.js 中引入api.js
        import api from '@/common/api.js'
        Vue.prototype.$api = api
2. 在頁面中調用
        //不傳參數
        this.$api.sendRequest().then((res) => {
            console.log(res);
        })
        //傳參
        this.$api.sendRequest({參數}).then((res) => {
            console.log(res);
        })

項目地址:https://gitee.com/jielov/uni-app-tabbar


免責聲明!

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



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