<template>
	<view>
		<button @click="isInstallMp(appid, wgtFile)">打开小程序</button>
	</view>
</template>

<script>
	const uniMP = uni.requireNativePlugin('uniMP')
	export default {
		data() {
			return {
				appid: '__UNI__B9BF3C2',
				wgtFile: '/static/wgt/__UNI__B9BF3C2.wgt'
			};
		},
		methods: {
			// 检测是否安装小程序
			isInstallMp(appid, wgtFile) {
				return new Promise((resolve, reject) => {
					uniMP.getUniMPVersion(appid, ret => {
						this.setMenu(appid)
						if (0 != ret.code) {
							// 本地安装
							this.installMP(appid, wgtFile)
								.then(() => {
									this.openUniMP(appid);
									resolve();
								})
						} else {
							this.openUniMP(appid);
							resolve()
						}

					});
				})
			},
			// 安装小程序
			installMP(appid, wgtFile) {
				return new Promise(resove => {
					uniMP.installUniMP({
						appid,
						wgtFile
					}, ret => {
						console.log('installUniMP: ' + JSON.stringify(ret));
						resove(ret)
					});
				});
			},
			// 打开小程序
			openUniMP(appid) {
				return new Promise(resolve => {
					uniMP.openUniMP({
						appid,
					}, ret => {
						console.log('openUniMP: ' + JSON.stringify(ret));
						resolve(ret)
					});
				})
			},
			// 设置菜单
			setMenu(appid) {
				return new Promise(resolve => {
					uniMP.setDefaultMenuItems({
							appid,
							items: [{
									title: '分享',
									id: 'share'
								},
								{
									title: '设置',
									id: 'set'
								}
							]
						},
						ret => {
							if (ret.id == 'share') {
								// openPay()
								console.log('点击了 分享');

							} else if (ret.id == 'set') {
								console.log('点击了 设置');
							}
							resolve(ret)
						}

					);
				})
			}
		}
	}
</script>

<style lang="scss">

</style>