欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

分析uniapp如何動(dòng)態(tài)獲取接口域名

 更新時(shí)間:2021年06月07日 09:10:08   作者:點(diǎn)幾  
本文主要介紹了uniapp如何動(dòng)態(tài)獲取接口域名,感興趣的同學(xué),可以參考下,并且試驗(yàn)一下。

背景

接口域名沒(méi)有寫(xiě)死,而是動(dòng)態(tài)獲取。具體實(shí)現(xiàn)就是 通過(guò)讀取一個(gè)靜態(tài)json文件,來(lái)設(shè)置真實(shí)的接口域名。好處是原域名有可能被封,這樣可以直接后臺(tái)操作修改該配置文件即可;不然的話,h5 項(xiàng)目可能還好說(shuō),app 的話必須重新發(fā)版。

代碼

// httpService.js 對(duì) uni.request 的封裝。

在數(shù)據(jù)請(qǐng)求入口處,統(tǒng)一先進(jìn)行 域名獲取,即 執(zhí)行 config.requestRemoteIp 方法

import config from '@/config'
import Vue from 'vue'
import cacheData from '@/service/cacheData.js'
const MockUtil = () => import('@/libs/mockUtil.js')
import Storage from '@/libs/storage.js'

class HttpRequest {

    /**
     * 讀取接口數(shù)據(jù)
     * @param options 請(qǐng)求信息
     * @param noMock  在整體使用mock數(shù)據(jù)的情形下,可 單獨(dú)設(shè)置 某個(gè)接口請(qǐng)求真實(shí)數(shù)據(jù)
     * @param cacheId
     * @returns {*}
     */
    async requestResolve(options, urlCustom = '', noMock = false, cacheId = null) {
        let remoteIP = await config.requestRemoteIp(); // 動(dòng)態(tài)設(shè)置接口請(qǐng)求域名
        
        if (process.env.NODE_ENV === 'development' && config.isMockApi && !noMock) {
            return this.getMockData(options)
        }
        if (cacheId && cacheData[cacheId]) {
            return this.testHttp(cacheData[cacheId])
        }
        return new Promise((resolve, reject) => {
            let baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro;
            options.url = baseUrl + options.url + `${urlCustom}`;
            uni.request(
                Object.assign({
                    success: (res) => {
                        if (res.statusCode != '200'){
                            uni.showToast({
                                title: '服務(wù)器錯(cuò)誤:'+res.statusCode,
                                icon: "none"
                            })
                            reject()
                        }
                        else if (res.data.code == 10001) {
                            Storage.removeToken();
                            let vue = new Vue();
                            vue.$store.dispatch('logout')
                            vue.$routeUtil.reLaunch('main');
                        }
                        
                        else if (res.data.code != 200) {
                            if (res.data.message) {
                                uni.showToast({
                                    icon: 'none',
                                    title: res.data.message
                                });
                            }
                            reject(res.data)
                        } else {
                            if (cacheId) {
                                cacheData[cacheId] = res.data.data
                            }
                            resolve(res.data.data)
                        }
                    },
                    fail: err => {
                        uni.showToast({
                            title: '服務(wù)器錯(cuò)誤',
                            icon: "none"
                        })
                    }
                }, options)
            );
        })
    }

    /**
     * mock數(shù)據(jù)按需導(dǎo)入
     * @param options
     * @returns {*}
     */
    async getMockData(options) {
        const Mock = await MockUtil()
        const MockUrl = Mock.default[options.url]
        if (typeof MockUrl !== 'function') {
            return this.testHttp(MockUrl)
        }
        if (options.method === 'post') {
            return this.testHttp(MockUrl(options.data, false))
        }
        return this.testHttp(MockUrl(options.params, true))
    }
    testHttp(data) {
        let pro = new Promise(function(resolve, reject) {
            setTimeout(function() {
                resolve(data)
            }, 50)
        })
        return pro
    }
}
export default new HttpRequest()

// config.js

const config = {
  isMockApi: false,
  // requestUrl: 'http://qiniu.eightyin.cn/teacherpath.json?time=' + Math.random().toString(36),
  requestUrl: 'http://qiniu.eightyin.cn/teacherpathtest.json?time=' + Math.random().toString(36),
  baseUrl: {
     
    dev: '',
    pro: ''
  },
  img: {
    ossDomain: ''
  },
  uuid: Math.random().toString(36).substring(3, 20),
  requestRemoteIp: () => {
      console.log('config:', config)
      if (config.RemoteIpInited)
        return Promise.resolve();
        return new Promise((resolve, reject) => {
            uni.request({
                url: config.requestUrl,
                success: (response) => {
                    //todo 測(cè)試
                    // config.baseUrl.pro = response.data.data.path;
                    config.baseUrl.dev = 'http://bayin5.mycwgs.com/';
                    config.img.ossDomain = response.data.data.ossDomain;
                    config.RemoteIpInited = true;
                    resolve()
                },
                fail: () => {
                    config.RemoteIpInited = true;
                    resolve()
                }
            })
        });
  }
}

export default config

以上就是分析uniapp如何動(dòng)態(tài)獲取接口域名的詳細(xì)內(nèi)容,更多關(guān)于uniapp動(dòng)態(tài)獲取接口域名的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論