uni-app通过vue-cli搭建项目

989 阅读1分钟

配置多端环境

小程序开发,选用uni-app,可以在package.json中配置多环境

package.json

"uni-app": {
    "scripts": {
        "mp-dev": {
            "title": "微信小程序开发",
            "BROWSER": "",
            "env": {
                "UNI_PLATFORM": "mp-weixin",
                "UNI_TYPE": "development"
            }
        },
        "mp-test": {
            "title": "微信小程序测试",
            "BROWSER": "",
            "env": {
                "UNI_PLATFORM": "mp-weixin",
                "UNI_TYPE": "test"
            }
        },
        "mp-prod": {
            "title": "微信小程序生产",
            "BROWSER": "",
            "env": {
                "UNI_PLATFORM": "mp-weixin",
                "UNI_TYPE": "production"
            }
        }
    }
}

系统配置

根目录创建config/config.js

// 系统配置
const APPID = '小程序appId' // 小程序appId
const APPNAME = '小程序name'
const VER_SION = '1.0.0' // 版本号,用于日志区分

const IS_RELEASE = false //是否发布,发布表示体验码接口为生产地址且STORAGE_KEY有专属的标识 所以在发布上线之前需要修改为true,是为了区分测试生产体验版本和线上生产版本冲突
const RELEASE = IS_RELEASE ? '_p' : ''
const STORAGE_NAME = 'project' //storage 参数

const TRY_LOGIN_LIMIT = 1 //登录状态态重试次数,默认1次,可修改
const NODE_ENV = process.env.UNI_TYPE //环境变量


//请求地址
const CONFIGS = {
    development: {
        API_HOST: 'https://test.server.beigene.rnhos.com',
        IMG_PATH_HOST: 'https://diabetes-rn.oss-cn-shanghai.aliyuncs.com',
        STORAGE_NAME: STORAGE_NAME + RELEASE + '_dev_',
    },
    test: {
        API_HOST: 'https://test.server.beigene.rnhos.com',
        IMG_PATH_HOST: 'https://diabetes-rn.oss-cn-shanghai.aliyuncs.com',
        STORAGE_NAME: STORAGE_NAME  + RELEASE + '_test_',
    },
    production: {
        API_HOST: 'https://server.beigene.rnhos.com',
        IMG_PATH_HOST: 'https://diabetes-rn.oss-cn-shanghai.aliyuncs.com',
        STORAGE_NAME: STORAGE_NAME  + RELEASE + '_prod_',
    },
}

const IMG_FILE = '/baekje'
const config = CONFIGS[NODE_ENV]

//导出的数据
export const APP_ID = APPID
export const APP_NAME = APPNAME
export const VERSION = VER_SION //版本号
export const TRY_LOGIN_NUM_LIMIT = TRY_LOGIN_LIMIT //登录状态重试次数
export const ENV_PATH = NODE_ENV //env环境
export const API_PATH = config.API_HOST //最终的环境地址
export const PUBLIC_IMG_PATH = config.IMG_PATH_HOST + IMG_FILE //上传到阿里云的图片地址
export const UPLOAD_FILE_URL = config.API_HOST + '/sys/upload' //上传图片地址
export const STORAGE_KEY = config.STORAGE_NAME //storage 存储的前缀