Scenarios
You want to integrate the uni-app miniapp container into a native app developed based on uni-app. For more information about other scenarios, see Integrate a miniapp container into an Android app.
Solution
Download the native uni-app plug-in EMASUniAppPlugin.
Decompress the
EMASUniAppPlugin.zippackage and copy the decompressed folder to thenativepluginsfolder of your uni-app project.Open your uni-app project in HBuilderX. Click the
manifest.jsonfile of the uni-app project. To configure a native app plug-in, selectEMASUniAppPluginin thenativepluginsfolder as an on-premises plug-in.
Click the
manifest.jsonfile again and switch to the source code view. Find theapp-plusparameter and add theUniMPparameter to themodulesparameter.
Use the EMASUniAppPlugin plug-in and the uni-app miniapp container in your uni-app project.
// Obtain the uni-app miniapp container. const mp = uni.requireNativePlugin('uniMP'); // Obtain the EMASUniAppPlugin plug-in. var emasModule = uni.requireNativePlugin("EMASUniAppPlugin");
API reference for the EMASUniAppPlugin plug-in
configEmas
var ret = emasModule.configEmas({
'appKeyAndroid': 'xxx',
'appSecretAndroid': 'xxx',
'channelIdAndroid': 'xxx',
'mtopDomain': 'xxx',
'useHttp': 'false',
'appKeyIOS': 'xxx',
'appSecretIOS': 'xxx',
'channelIdIOS': 'xxx'
})Initializes the EMASUniAppPlugin plug-in. This method is a synchronous method.
Request parameters | |
appKeyAndroid | String: the AppKey of the Android app. You can obtain the AppKey of an app from the Enterprise Mobile Application Studio (EMAS) console. |
appSecretAndroid | String: the AppSecret of the Android app. You can obtain the AppSecret of an app from the EMAS console. |
channelIdAndroid | String: the channel ID of the Android app. You can obtain the channel ID of an app from the EMAS console. |
mtopDomain | String: the MTOP domain name of EMAS. |
useHttp | Boolean: specifies whether to use the HTTP protocol. |
appKeyIOS | String: the AppKey of the iOS app. You can obtain the AppKey of an app from the EMAS console. |
appSecretIOS | String: the AppSecret of the iOS app. You can obtain the AppSecret of an app from the EMAS console. |
channelIdIOS | String: the channel ID of the iOS app. You can obtain the channel ID of an app from the EMAS console. |
updateMiniApp
emasModule.updateMiniapp({
'appId': 'xxx',
}, (ret) => {
})Updates the version of a miniapp. If a miniapp has a new version, you can call this method to download and install the miniapp of the new version. This method is an asynchronous method.
Request parameters | |
appId | String: the ID of the miniapp. You can obtain the ID of a miniapp from the Application Open Platform console. For more information, see the Obtain the ID of a miniapp section of this topic. |
miniAppVersion | String: the version number of the miniapp. You can leave this parameter empty. If you set this parameter to null or leave this parameter empty, the specified miniapp is downloaded and installed. If a specific version number is specified, the system checks whether the miniapp is updated to the specified version. If the miniapp is not updated, no operation is performed. If the miniapp is updated, the miniapp of the specified version is downloaded and installed. |
publishId | String: the release ID of the miniapp. You can leave this parameter empty. |
Response parameters | |
code | Number: the error code. |
errorMsg | String: the error message. |
wgtPath | String: the path of the miniapp installation package. |
uniAppId | String: the ID of the miniapp. |
version | String: the version number of the miniapp. |
publishType | String: the release type of the miniapp. |
Example:
emasModule.updateMiniapp({
'appId': '151624424672398147591',
}, (ret) => {
if (ret.code === 0) {
// Install the miniapp by using the miniapp container.
mp.installUniMP({
appid: '__UNI__151624424672398147591',
wgtFile: ret.wgtPath
}, (r) => {
if (0 == r.code) {
uni.showToast({
title: 'The miniapp is installed.'
});
// After the miniapp is installed, open the miniapp.
mp.openUniMP({
appid: '__UNI__151624424672398147591'
}, (ret) => {
if (0 != ret.code) {
uni.showModal({
title: 'The miniapp failed to be started.',
content: JSON.stringify(ret),
showCancel: false
});
}
console.log('openUniMP: ' + JSON.stringify(ret));
});
} else {
uni.showModal({
title: 'The miniapp failed to be installed.',
content: JSON.stringify(r),
showCancel: false
});
}
console.log('installUniMP: ' + JSON.stringify(r));
});
}
})getMiniappList
emasModule.getMiniappList({
'appIdList': '[xxx]',
}, (ret) => {
})Queries miniapps. This method is an asynchronous method.
Request parameters | |
appIdList | String: the IDs of miniapps. You can obtain the ID of a miniapp from the Application Open Platform console. For more information, see the Obtain the ID of a miniapp section of this topic. |
Response parameters | |
code | Number: the error code. |
errorMsg | String: the error message. |
listData | String: the information about miniapps. |
Obtain the ID of a miniapp
Log on to the Application Open Platform console.
Select a workspace to go to the Overview page of the workspace.
NoteIf you have joined only one workspace, you are navigated to the Overview page of the workspace after you log on to the Application Open Platform console.
In the left-side navigation pane, click Mini Apps. The Miniapp List page appears.
On the Miniapp List page, find the miniapp whose ID you want to obtain and record the ID.
