九色国产,午夜在线视频,新黄色网址,九九色综合,天天做夜夜做久久做狠狠,天天躁夜夜躁狠狠躁2021a,久久不卡一区二区三区

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
uni-app開發(fā)經(jīng)驗(yàn)分享十: 封裝request請求

http.js

//封裝requset,uploadFile和downloadFile請求,新增get和post請求方法

let http = {
    'setBaseUrl': (url) => {
        if (url.charAt(url.length - 1) === "/") {
            url = url.substr(0, url.length - 1)
        }
        http.baseUrl = url;
    },
    'header': {},
    'beforeRequestFilter': (config) => { return config },
    'beforeResponseFilter': (res) => { return res },
    'afterResponseFilter': (successResult) => { },
    'get': get,
    'post': post,
    'request': request,
    'uploadFile': uploadFile,
    'downloadFile': downloadFile
}
 
 
function init(con) {
    //url
    let url = http.baseUrl;
    if (url && con.url && !con.url.match(/^(http|https):\/\/([\w.]+\/?)\S*$/)) {
        if (con.url.charAt(0) !== "/") {
            con.url = "/" + con.url;
        }
        con.url = url.concat(con.url);
    }
    //header
    if (http.header != undefined && http.header != null) {
        if (!con.header) {
            con.header = http.header;
        } else {
            Object.keys(http.header).forEach(function (key) {
                con.header[key] = http.header[key]
            });
        }
    }
}
 
function request(con) {
    init(con);
    let config = {
        url: con.url ? con.url : http.baseUrl,
        data: con.data,
        header: con.header,
        method: con.method ? con.method : 'GET',
        dataType: con.dataType ? con.dataType : 'json',
        responseType: con.responseType ? con.responseType : 'text',
        success: con.success ? (res) => {
            http.afterResponseFilter(con.success(http.beforeResponseFilter(res)));
        } : null,
        fail: con.fail ? (res) => {
            con.fail(res);
        } : null,
        complete: con.complete ? (res) => {
            con.complete(res);
        } : null
    }
    return uni.request(http.beforeRequestFilter(config));
}
 
function get(url, con, success) {
    let conf = {};
    if (con && typeof con == 'function') {
        if (success && typeof success == 'object') {
            conf = success;
        }
        conf.success = con
    }else{
        if (con && typeof con == 'object') {
            conf = con;
        }
        conf.success = success;
    }
 
    if (url) {
        conf.url = url
    }
    conf.method = "GET";
    return request(conf);
}
 
function post(url, data, con, success) {
    let conf = {};
    if (con && typeof con == 'function') {
        if (success && typeof success == 'object') {
            conf = success
        }
        conf.success = con;
    } else {
        if (con && typeof con == 'object') {
            conf = con;
        }
        conf.success = success;
    }
    if (url) {
        conf.url = url
    }
    if (data) {
        conf.data = data
    }
    conf.method = "POST";
    return request(conf);
}
 
function uploadFile(con) {
    init(con);
 
    let config = {
        url: con.url ? con.url : http.baseUrl,
        files: con.files,
        filesType: con.filesType,
        filePath: con.filePath,
        name: con.name,
        header: con.header,
        formData: con.formData,
        success: con.success ? (res) => {
            http.afterResponseFilter(con.success(http.beforeResponseFilter(res)));
        } : null,
        fail: con.fail ? (res) => {
            con.fail(res);
        } : null,
        complete: con.complete ? (res) => {
            con.complete(res);
        } : null
    }
    return uni.uploadFile(http.beforeRequestFilter(config));
}
 
function downloadFile(con) {
    init(con);
 
    let config = {
        url: con.url ? con.url : http.baseUrl,
        header: con.header,
        success: con.success ? (res) => {
            http.afterResponseFilter(con.success(http.beforeResponseFilter(res)));
        } : null,
        fail: con.fail ? (res) => {
            con.fail(res);
        } : null,
        complete: con.complete ? (res) => {
            con.complete(res);
        } : null
    }
    return uni.downloadFile(http.beforeRequestFilter(config));
}
 
 
export default http

可以設(shè)置全局的url訪問地址(會拼接請求的url成完整的url,所以在寫url時(shí)只需要"/xxx"),也可以在請求時(shí)設(shè)置具體url(以http或https開頭)

可以設(shè)置全局的header,如果請求時(shí)有header參數(shù),會加上全局的header

可以設(shè)置攔截器,有發(fā)送請求前的攔截器beforeRequestFilter,參數(shù)是包含已經(jīng)拼接完的url和header的請求參數(shù),注意要返回;執(zhí)行成功回調(diào)函數(shù)前的攔截器beforeResponseFilter,參數(shù)是回調(diào)成功函數(shù)的參數(shù),注意要返回;執(zhí)行成功回調(diào)函數(shù)后的攔截器afterResponseFilter,參數(shù)為成功回調(diào)函數(shù)的返回值。

具體例子

my.js

import http from './http'
 
const AUTH_TOKEN = "X-Auth-Token";
 
http.setBaseUrl("http://localhost:8081");
http.header['comp'] = "cjx913"
if (uni.getStorageSync(AUTH_TOKEN)) {
    http.header[AUTH_TOKEN] = uni.getStorageSync(AUTH_TOKEN);
}
 
http.beforeResponseFilter = function (res) {
    //X-Auth-Token
    if (res.header) {
        var respXAuthToken = res.header[AUTH_TOKEN.toLocaleLowerCase()];
        if (respXAuthToken) {
            uni.setStorageSync(AUTH_TOKEN, respXAuthToken);
            http.header[AUTH_TOKEN] = respXAuthToken;
        }
    }
    return res;
}
 
let my = {
    'http': http
}
export default my

main.js

import Vue from 'vue'
import App from './App'
 
Vue.config.productionTip = true
 
App.mpType = 'app'
 
import fly from './fly/fly'
Vue.prototype.$fly = fly
 
import my from './my/my'
var http = my.http;
 
Vue.prototype.$http = http
 
import store from './store'
Vue.prototype.$store = store
 
 
 
const app = new Vue({
  ...App
})
app.$mount()

index.js

<script>
export default {
data() {
return {
title: 'Hello',
name:'cjx913'
}
},
onLoad() {

},
methods: {
session:function(){
// this.$fly.get('/session')
// .then(function (response) {
// console.log("hello");
// console.log(response.data);
// console.log(response);
// })
// .catch(function (error) {
// console.log(error);
// });
 
// this.$http.request({
// url:"session",
// success:(res)=>{
// console.log(res);
// }
// })
// this.$http.get("/session",function(res){
// console.log(res);
// }
// )
this.$http.get("/session",{
success:function(res){
console.log(res);
}
}
)
}
}
}
</script>

上述是簡單設(shè)置baseUrl,header和通過攔截器攔截response的X-Auth-Token,并讓請求帶上X-Auth-Token

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
uniapp無痛刷新token
微信小程序上傳圖片(附后端代碼)
SAP Spartacus UI 通過 HTTP Interceptor 給請求添加 Authorization 字段的源代碼分析
php 下載遠(yuǎn)程圖片 的幾種方法
微信小程序https請求wx.request詳細(xì)教程
haproxy 命令配置實(shí)例
更多類似文章 >>
生活服務(wù)
熱點(diǎn)新聞
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服