封装uni-app的须要token的要求

config.js

const app = {
    apiUrl: 'http://216:18080/webcase/', //要求的地点
    baseRequest(obj) {
        try {
            const userToken = uni.getStorageSync('userToken');
            if (userToken) {
                if (obj.header) {
                    obj.header["token"] = userToken;
                } else {
                    obj.header = { "token": userToken };
                }
                obj.url = this.apiUrl + obj.url;
                uni.request(obj)
            }
            else{
                console.log("猎取不到userToken")
                
            }
        } catch (e) {
            console.log(e)
            console.log("猎取不到userToken")
        } 
    },
}
export default app;

须要运用的页面

    import app from "@/api/apiConfig"
            getUserStatus() {
                app.baseRequest({
                    url: 'getUserStatus',
                    method: 'POST',
                    success: (res) => {
                        // 用户状况存到缓存中去
                        try{
                            uni.setStorageSync('userStatus', res.data.data.type)
                            uni.setStorageSync('userAgentInfo',res.data.data)
                        }catch(e){
                            //TODO handle the exception
                        }
                        if(res.data.data.type == 2) {
                            this.getLawInfo()
                        } else {
                            this.yhju= false
                        }
                    }
                })
            }
    原文作者:houqq
    原文地址: https://segmentfault.com/a/1190000019083308
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞