The uniapp applet, according to the environment version of the applet, controls the display and hiding of the display page function buttons

Requirements: Control the display and hiding of a function button on the control page according to the applet environment;

The following is the relevant code of the official documentation and function implementation:

To achieve the above needs, uni.getAccountInfoSync() is used:
uni.getAccountInfoSync() is a synchronization method provided by Uniapp, which is used to obtain the account information of the applet. It can obtain some basic information of the current applet, such as the AppId of the applet, the type of the applet, the version number of the applet, and so on.

Official document link https://uniapp.dcloud.net.cn/api/other/getAccountInfoSync.html#getaccountinfosync
insert image description here

insert image description here

const accountInfo = uni.getAccountInfoSync();
console.log(accountInfo.miniProgram.appId); // 小程序 appId
console.log(accountInfo.plugin.appId); // 插件 appId
console.log(accountInfo.plugin.version); // 插件版本号, 'a.b.c' 这样的形式

Example of use in a project:

Through the v-if control button, or the display and hiding of a certain functional module,注意隐藏和显示后的页面样式

<view class="nav-item" @click="modifyPassWord" v-if="vertifyIsShow">
	<image src="../../static/home/modifyPassWord.png" class="nav-img"></image>
	<text class="nav-text">重置密码</text>
</view>

In the onLoad() or created() method of the page, use the uni.getAccountInfoSync() API to get the current Mini Program environment version

created() {
    
    
	this.vertifyIsShow = false; // 初始化控制按钮隐藏
	const envVersion = uni.getAccountInfoSync().miniProgram.envVersion;
	if (envVersion === 'develop') {
    
    
	  // 开发版环境
	  this.vertifyIsShow = true
	} else if (envVersion === 'trial') {
    
    
		// 体验版环境
		this.vertifyIsShow = true
	} else if (envVersion === 'release') {
    
    
		// 正式版环境
		this.vertifyIsShow = false
	} else {
    
    
	  // 无法确定环境
	  this.vertifyIsShow = false
	}
},

Guess you like

Origin blog.csdn.net/m0_47791238/article/details/132007110