美文网首页
2021-12-07 uniapp区分打包环境

2021-12-07 uniapp区分打包环境

作者: jinya2437 | 来源:发表于2021-12-07 14:40 被阅读0次

package.json文件追加配置坏境

注意:若项目之前未使用npm管理依赖(项目根目录下无package.json文件),先在项目根目录执行命令初始化npm工程:

npm init -y

官网说明:https://uniapp.dcloud.io/frame?id=npm%E6%94%AF%E6%8C%81

//package.json追加配置环境
"uni-app" : {
    "scripts" : {
      "build:test" : {    //名称
        "title" : "build:test",
        "env" : {
          "UNI_PLATFORM" : "h5",
          "VUE_APP_BASE_URL":"http://130.0.0.146:8080"
        }
      },
      "build:pro" : { //名称
        "title" : "build:pro",
        "env" : {
          "UNI_PLATFORM" : "h5",
          "VUE_APP_BASE_URL":"http://120.0.4.37:8080"
        }
      }
    }
  }

官网说明:https://uniapp.dcloud.io/collocation/package

对应Hbuild工具栏发行菜单追加了打包环境,如下图所示:


image.png

应用

import Vue from 'vue'
import store from '@/store'

export default {
    common:{
//本地运行是获取不到【VUE_APP_BASE_URL 】,默认使用【http://1330.0.4.37:8080】
        baseUrl:process.env.VUE_APP_BASE_URL || 'http://1330.0.4.37:8080',
        data:{},
        method:'GET',
        dataType:'json',
        responseType:'',
    },
    request(options={}){
        let header = {
            'Content-Type':'application/json;charset=UTF-8',
            'Authorization':'Bearer '+ uni.getStorageSync('token'),
            'Accept-Language':'zh-CN'
        }
        options.url = this.common.baseUrl+'' + options.url
        options.header = {...header,...options.header}
        options.data = options.data || this.common.data
        options.method = options.method || this.common.method
        options.dataType = options.dataType || this.common.dataType
        options.responseType=options.responseType || this.common.responseType
        console.log('打包部署测试服后,会获取测试环境地址')
        console.log(process.env.VUE_APP_BASE_URL)
        return new Promise((res,rej)=>{
            uni.request({
                ...options,
                success: (result) => {
                    let data = result.data
                    res(data)
                },
                fail: (error) => {

                    console.log('error----------------')
                    console.log(error)

                    let  message  = error.errMsg;
                    if (message == "Network Error") {
                        message = "后端接口连接异常";
                    }
                    else if (message == 'request:fail timeout') {
                        message = "系统接口请求超时";
                    }
                    uni.showToast({
                        title: message || '请求失败',
                        duration: 1000,
                        icon:'none'
                    });
                    return rej()
                },
                complete: (e) => {
                    uni.hideLoading();
                }
            })
        })
    },
    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)
    },
    put(url,data={},options={}){
        options.url = url
        options.data = data
        options.method = 'PUT'
        return this.request(options)
    },
    del(url,data={},options={}){
        options.url = url
        options.data = data
        options.method = 'DEL'
        return this.request(options)
    }
}


相关文章

网友评论

      本文标题:2021-12-07 uniapp区分打包环境

      本文链接:https://www.haomeiwen.com/subject/zyxaxrtx.html