站長資訊網(wǎng)
        最全最豐富的資訊網(wǎng)站

        uniapp如何添加請求攔截器

        uniapp添加請求攔截器的方法:1、定義LsxmRequest類并添加默認(rèn)配置、攔截器與請求方法;2、后續(xù)需要自定義config與獲取接口地址,在類中添加get和set方法;3、利用Symbol特性定義四個私有變量,防止變量污染。

        uniapp如何添加請求攔截器

        本教程操作環(huán)境:windows7系統(tǒng)、uni-app2.5.1版本,DELL G3電腦,該方法適用于所有品牌電腦。

        uniapp添加請求攔截器的方法:

        1、利用Symbol特性定義四個私有變量,防止變量污染

        const config = Symbol('config') const isCompleteURL = Symbol('isCompleteURL') const requestBefore = Symbol('requestBefore') const requestAfter = Symbol('requestAfter')

        2、定義LsxmRequest類并添加默認(rèn)配置、攔截器與請求方法

        class LsxmRequest {     //默認(rèn)配置     [config] = {         baseURL: '',         header: {             'content-type': 'application/json'         },         method: 'GET',         dataType: 'json',         responseType: 'text'     }     //攔截器     interceptors = {         request: (func) => {             if (func)              {                 LsxmRequest[requestBefore] = func             }              else              {                 LsxmRequest[requestBefore] = (request) => request             }         },         response: (func) => {             if (func)              {                 LsxmRequest[requestAfter] = func             }              else              {                 LsxmRequest[requestAfter] = (response) => response             }         }     }     static [requestBefore] (config) {         return config     }     static [requestAfter] (response) {         return response     }     static [isCompleteURL] (url) {         return /(http|https)://([w.]+/?)S*/.test(url)     }          request (options = {}) {         options.baseURL = options.baseURL || this[config].baseURL         options.dataType = options.dataType || this[config].dataType         options.url = LsxmRequest[isCompleteURL](options.url) ? options.url : (options.baseURL + options.url)         options.data = options.data         options.header = {...options.header, ...this[config].header}         options.method = options.method || this[config].method         options = {...options, ...LsxmRequest[requestBefore](options)}         return new Promise((resolve, reject) => {             options.success = function (res) {                 resolve(LsxmRequest[requestAfter](res))             }             options.fail= function (err) {                 reject(LsxmRequest[requestAfter](err))             }             uni.request(options)         })     }     get (url, data, options = {}) {         options.url = url         options.data = data         options.method = 'GET'         return this.request(options)     }     post (url, data, options = {}) {         options.url = url         options.data = data         options.method = 'POST'         return this.request(options)     } }

        3、后續(xù)需要自定義config與獲取接口地址,在類中添加get和set方法:

        setConfig (func) {         this[config] = func(this[config]) } getConfig() {     return this[config]; }

        4、用自定義插件注冊的方法將apis.js(后續(xù)在main.js中需要導(dǎo)入apis.js)中的接口賦到自定義的Vue原型變量$lsxmApi上,為了避免每個頁面都要引入一次,在每個頁面的beforeCreate生命周期混入。

        LsxmRequest.install = function (Vue) {     Vue.mixin({         beforeCreate: function ()          {             if (this.$options.apis)              {                 console.log(this.$options.apis)                 Vue._lsxmRequest = this.$options.apis             }         }     })          Object.defineProperty(Vue.prototype, '$lsxmApi', {         get: function ()          {             return Vue._lsxmRequest.apis         }     }) } export default LsxmRequest

        5、在config.js中實例化并自定義請求配置項(此處根據(jù)項目需要在頭部加入token)與攔截器

        import LsxmRequest from './LsxmRequest' const lsxmRequest = new LsxmRequest() // 請求攔截器 lsxmRequest.interceptors.request((request) => {     if (uni.getStorageSync('token')) {         request.header['token'] = uni.getStorageSync('token');     }     return request }) // 響應(yīng)攔截器 lsxmRequest.interceptors.response((response) => {     console.log('beforeRespone',response);     // 超時重新登錄     if(response.data.isOverTime){     uni.showModal({             title:'提示',             content:'您已超時,請重新登錄!',             showCancel:false,             icon:'success',             success:function(e){                 if(e.confirm){                     uni.redirectTo({                         url: '/pages/login/login'                     })                 }             }         });      }     else     {         return response;     } }) // 設(shè)置默認(rèn)配置 lsxmRequest.setConfig((config) => {     config.baseURL = 'http://xxxxx.com'          if (uni.getStorageSync('token')) {         config.header['token'] = uni.getStorageSync('token');     }     return config; }) export default lsxmRequest

        6、main.js中引入,將apis掛載到Vue上

        import LsxmRequest from './service/LsxmRequest.js' import apis from './service/apis.js' import lsxmRequest from './service/config.js' Vue.use(LsxmRequest) Vue.prototype.baseDomain = lsxmRequest.getConfig().baseURL App.mpType = 'app' const app = new Vue({     store,     apis,     ...App }) app.$mount()

        7、需要添加接口時,只需在apis.js中添加接口即可(后續(xù)可將apis.js中的接口按照功能拆分,模塊化管理)

        import lsxmRequest from './config.js' export default{   apis:{         //獲取驗證用戶令牌         getLoginToken(data){             return lsxmRequest.post('/xxx/xxx/getLoginToken', data)         },         //登錄         login(data){             return lsxmRequest.post('/xxx/xxx/login', data)         }         } }

        8、至此,頁面中即可使用

        this.$lsxmApi.getLoginToken({}).then((resToken) => {         console.log(resToken) }

        了解

        贊(0)
        分享到: 更多 (0)
        網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號
        主站蜘蛛池模板: 亚洲午夜久久久精品影院| 成人精品视频成人影院| www.精品| 99热门精品一区二区三区无码| 久久免费99精品国产自在现线| 四虎国产精品永久一区| 精品欧洲AV无码一区二区男男 | 欧美ppypp精品一区二区| 中文字幕亚洲精品| 日韩精品免费在线视频| 91麻豆精品视频在线观看| 亚洲精品无码乱码成人| 日韩欧美国产精品第一页不卡 | 日韩精品久久久久久久电影蜜臀| 久久精品国产欧美日韩| 国产免费久久精品丫丫| 成人免费精品网站在线观看影片| 青草青草久热精品视频在线网站| 国产精品高清一区二区人妖| 国产福利精品一区二区| 国内精品久久久久久久97牛牛| 精品人妻va出轨中文字幕| 精品综合久久久久久888蜜芽| 四虎国产精品永久在线观看 | 最新欧美性爱精品一区二区三区 | 精品久久无码中文字幕| 久久久久亚洲精品天堂| 欧美日韩人妻精品一区二区在线| 亚洲精品中文字幕乱码三区| 亚洲中文字幕久久精品无码APP| 亚洲国产精品乱码一区二区| 无码人妻一区二区三区精品视频 | 国产精品成人69XXX免费视频| 中文字幕亚洲精品资源网| 欧美精品免费线视频观看视频| 久久99精品久久久久久| 午夜精品久视频在线观看| 99久久精品费精品国产| 99久久精品无码一区二区毛片| 91麻豆精品一二三区在线| 国产人成精品综合欧美成人|