import { getGXconfig } from "@/api/uniMP.js"; const mp = uni.requireNativePlugin('uniMP'); let appid = ''; // 应用id let wgtFile = ''; // 应用文件地址 // 比较版本号大小 function compareVersions(version1, version2) { const arr1 = version1.split('.').map(Number); const arr2 = version2.split('.').map(Number); for (let i = 0; i < Math.max(arr1.length, arr2.length); i++) { const num1 = i < arr1.length ? arr1[i] : 0; const num2 = i < arr2.length ? arr2[i] : 0; if (num1 > num2) { return 1; } else if (num1 < num2) { return -1; } } return 0; } // 加载小程序 const loadMP = async (id) => { appid = id; uni.showLoading({ title: '加载中', mask: true }) let info = await getGXconfig(); console.log('最新版本', info.data); console.log(mp, uni); // return ; mp.getUniMPVersion(id, (ret) => { console.log('当前版本', ret); if (0!=ret.code||compareVersions(info.data.version, ret.versionInfo.name) == 1||true) { uni.downloadFile({ url: info.data.version_info?.dow_url, success(res) { wgtFile = res.tempFilePath; console.log('下载完成', wgtFile); installMP(); } }) } else { open() } }); }; // 小程序版本信息 const getVersion = (id) => { appid = id; return new Promise((resolve, reject) => { mp.getUniMPVersion(appid, (ret) => { console.log('供销', ret); }); }) } // 安装小程序 const installMP = () => { mp.getUniMPVersion(appid, (ret) => { console.log('安装:供销', ret); doInstallMP(); // if (0 != ret.code) { //获取失败时安装应用 // doInstallMP(); // } else { // uni.showModal({ // title: '提示', // content: 'uni小程序已安装,是否覆盖?', // success: res => { // res.confirm && doInstallMP(); // } // }); // } console.log('getUniMPVersion: ' + JSON.stringify(ret)); }); }; const doInstallMP = () => { mp.installUniMP({ appid: appid, wgtFile: wgtFile }, (r) => { if (0 == r.code) { // uni.showToast({ // title: '安装成功' // }); console.log('小程序安装成功'); open(); } else { uni.hideLoading(); uni.showModal({ title: '安装失败', content: JSON.stringify(r), showCancel: false }); } console.log('安装供销: ' + JSON.stringify(r)); }); }; const open = (id = null) => { mp.openUniMP({ appid: id || appid, extraData: { uniMP: true } }, (ret) => { uni.hideLoading(); if (0 != ret.code) { uni.showModal({ title: '启动失败', content: JSON.stringify(ret), showCancel: false }); } console.log('openUniMP: ' + JSON.stringify(ret)); }); } export default { loadMP, installMP, doInstallMP, getVersion, open }