Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

Whether front-end or back-end in daily development, the project will generally distinguish between development, testing, production of these three environments (and some pre-release, grayscale release), so write the configuration information of different environments in advance, it is simply once and for all

Wechat applets

wx.getAccountInfoSync()

The Object Wx.getAccountInfoSync () Api can be used in wechat applet development

In miniProgram. EnvVersion, the valid value is

The trial is in the official releaseCopy the code

Using this API, you can distinguish between the three environments and print the following

console.log(wx.getAccountInfoSync()) // { // miniProgram: { // appId: "************" // envVersion: "Develop" // version: "" // The online applets version can be obtained only from the official applets, but cannot be obtained from the development and experience versions. / / / /}}Copy the code

The item fetch request address can be distinguished by this value

Const env = wx.getAccountInfoSync().miniprogram.envversion const baseApi = { "Https://xxx.com", / / experience trials: "https://xxx.com", / / final release: "https://xxx.com"}; const api = baseApi[env] export default api;Copy the code

__wxConfig

There is also a hidden internal variable ** __wxConfig** which contains a lot of information about the following

console.log(__wxConfig) // { // envVersion: "develop", // accountInfo: { // appId: "*****" // icon: "Http:// * * * * *" / / nickname: "* * *" / /}, / / brand: '* *', / / mobile phone brands (preview and formal environment is running on the phone) / / platform: "Devtools" / / the current running system android | mountain. / /... / /}Copy the code

You can see that there is also the variable envVersion that contains the current environment

Env = wx.getAccountInfoSync().miniProgram.envversion

const env = __wxConfig.envVersion
Copy the code

uniapp

Please go to the official documentation