小程序环境变量配置,区分开发、体验、线上
封装__wxConfig.envVersion,main.js中引入注册,全局使用;
和后端约定了在请求header
里除了token
,还要加上requestId
,为随机生成40位的字母,数字,-,三种,最后一位默认为4,为小程序标识
注意 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 let REQUEST_URL = "" ;function baseUrl ( ) { const envVersion = __wxConfig.envVersion; switch (envVersion) { case 'develop' : REQUEST_URL ="http://xxxx.com" ; break ; case 'trial' : REQUEST_URL = "http://xxxx.com" ; break ; case 'release' : REQUEST_URL = "https://xxxx.com" ; break ; default : REQUEST_URL = "正式域名地址" ; break ; } return REQUEST_URL; } baseUrl()
request公共方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 const randomString = (length ) => { let str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-' ; let result = '' ; for (var i = length; i > 0 ; --i) { result += str[Math .floor(Math .random() * str.length)] } return result } const getHeader = () => { const token = uni.getStorageSync('token' ) || '' ; return { token, 'Content-type' : 'application/json;charset=UTF-8' , 'requestId' : randomString(39 ) + '4' } }