lihaiCenter/utils/uniMP.js

421 lines
10 KiB
JavaScript
Raw Normal View History

2023-10-08 14:17:20 +08:00
import {
2023-11-22 11:47:40 +08:00
getGXconfig,
miniapp
2023-10-08 14:17:20 +08:00
} from "@/api/uniMP.js";
import {
2023-11-22 11:47:40 +08:00
HTTP_REQUEST_URL
2023-10-08 14:17:20 +08:00
} from '@/config/app';
const mp = uni.requireNativePlugin('uniMP');
import store from "@/store/modules/app.js"
2023-11-22 11:47:40 +08:00
import { Toast } from "../libs/uniApi";
2023-10-08 14:17:20 +08:00
let appid = ''; // 应用id
let wgtFile = ''; // 应用文件地址
let timer = null; // 加载计时器
// 比较版本号大小
function compareVersions(version1, version2) {
2023-11-22 11:47:40 +08:00
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;
2023-10-08 14:17:20 +08:00
}
2023-11-22 11:47:40 +08:00
// 加载中台小程序
const loadMPcenter = async (info) => {
console.log(info);
if(info.app_id[0]!='_'){
console.log(`app_id:${info.app_id}不合法`);
return Toast('APPID不合法');
}
appid = info.app_id;
try{
uni.showLoading({
title: '初始化中...',
mask: true
})
mp.getUniMPVersion(appid, (ret) => {
console.log('当前版本', ret);
let flag;
if (HTTP_REQUEST_URL == 'https://middle.lihaink.cn') {
flag = false
} else {
flag = true
}
if (0 != ret.code || compareVersions(info.app_version, ret.versionInfo.name) == 1 ||
flag ==
true) {
let count = 0;
timer = setInterval(() => {
if (count < 100) uni.showLoading({
title: `初始化中... ${count}%`,
mask: true
})
else uni.showLoading({
title: '初始化中...100%',
mask: true
})
}, 600)
let downloadTask = uni.downloadFile({
url: info.down_url,
success(res) {
wgtFile = res.tempFilePath;
console.log('初始化完成', wgtFile);
installMP();
},
fail(res) {
clearInterval(timer);
timer = null;
uni.hideLoading();
}
});
downloadTask.onProgressUpdate((res) => {
// console.log('初始化进度' + res.progress);
if (res.progress > count) count += 10;
if (count >= 90) {
clearInterval(timer);
timer = null;
}
});
} else {
open()
}
});
}catch(e){
uni.hideLoading()
}
};
2023-10-28 11:30:46 +08:00
// 加载供销小程序
2023-10-08 14:17:20 +08:00
const loadMP = async (id) => {
2023-11-22 11:47:40 +08:00
appid = id;
let info = await getGXconfig();
console.log('最新版本', info.data);
console.log(mp, uni);
// return ;
uni.showLoading({
title: '初始化中...',
mask: true
})
mp.getUniMPVersion(id, (ret) => {
console.log('当前版本', ret);
let flag;
if (HTTP_REQUEST_URL == 'https://shop.lihaink.cn') {
flag = false
} else {
flag = true
}
if (0 != ret.code || compareVersions(info.data.version, ret.versionInfo.name) == 1 || flag ==
true) {
let count = 0;
timer = setInterval(() => {
if (count < 100) uni.showLoading({
title: `初始化中... ${count}%`,
mask: true
})
else uni.showLoading({
title: '初始化中...99%',
mask: true
})
}, 600)
let downloadTask = uni.downloadFile({
url: info.data.version_info?.dow_url,
success(res) {
wgtFile = res.tempFilePath;
console.log('初始化完成', wgtFile);
setTimeout(() => {
installMP();
clearInterval(timer);
timer = null;
}, 700)
},
fail(res) {
clearInterval(timer);
timer = null;
uni.hideLoading();
}
});
downloadTask.onProgressUpdate((res) => {
// console.log('初始化进度' + res.progress);
if (res.progress > count) count += 10;
if (count >= 90) {
clearInterval(timer);
timer = null;
}
});
} else {
open()
}
});
2023-10-08 14:17:20 +08:00
};
//加载商城小程序
const loadMPx = async (id) => {
2023-11-22 11:47:40 +08:00
appid = id;
let info = await miniapp();
console.log('最新版本', info.data);
console.log(mp, uni);
// return ;
uni.showLoading({
title: '初始化中...',
mask: true
})
mp.getUniMPVersion(id, (ret) => {
console.log('当前版本', ret);
let flag;
if (HTTP_REQUEST_URL == 'https://shop.lihaink.cn') {
flag = false
} else {
flag = true
}
if (0 != ret.code || compareVersions(info.data.appInfo.version, ret.versionInfo.name) == 1 ||
flag ==
true) {
let count = 0;
timer = setInterval(() => {
if (count < 100) uni.showLoading({
title: `初始化中... ${count}%`,
mask: true
})
else uni.showLoading({
title: '初始化中...99%',
mask: true
})
}, 600)
let downloadTask = uni.downloadFile({
url: info.data.appInfo.dow_url,
success(res) {
wgtFile = res.tempFilePath;
console.log('初始化完成', wgtFile);
setTimeout(() => {
installMP();
clearInterval(timer);
timer = null;
}, 700)
},
fail(res) {
clearInterval(timer);
timer = null;
uni.hideLoading();
}
});
downloadTask.onProgressUpdate((res) => {
// console.log('初始化进度' + res.progress);
if (res.progress > count) count += 10;
if (count >= 90) {
clearInterval(timer);
timer = null;
}
});
} else {
open()
}
});
2023-10-08 14:17:20 +08:00
};
2023-10-28 11:30:46 +08:00
// 按信息接口加载小程序
2023-10-08 14:17:20 +08:00
const loadMPurl = async (e) => {
2023-11-22 11:47:40 +08:00
appid = e.id;
2023-10-08 14:17:20 +08:00
let FURL = e.url;
2023-11-22 11:47:40 +08:00
uni.showLoading({
title: '初始化中...',
mask: true
})
2023-10-08 14:17:20 +08:00
uni.request({
url: FURL,
method: 'GET',
success: (fileInfo) => {
mp.getUniMPVersion(appid, (ret) => {
2023-11-22 11:47:40 +08:00
console.log('当前版本', ret);
let flag;
if (HTTP_REQUEST_URL == 'https://shop.lihaink.cn') {
flag = false
} else {
flag = true
}
if (0 != ret.code || compareVersions(fileInfo.data?.data?.version, ret.versionInfo.name) == 1 ||
flag ==
true) {
let count = 0;
timer = setInterval(() => {
if (count < 100) uni.showLoading({
title: `初始化中... ${count}%`,
mask: true
})
else uni.showLoading({
title: '初始化中...99%',
mask: true
})
}, 600)
let downloadTask = uni.downloadFile({
url: fileInfo.data?.data?.down_url,
success(res) {
wgtFile = res.tempFilePath;
console.log('初始化完成', wgtFile);
setTimeout(() => {
installMP();
clearInterval(timer);
timer = null;
}, 700)
},
fail(res) {
clearInterval(timer);
timer = null;
uni.hideLoading();
}
});
downloadTask.onProgressUpdate((res) => {
// console.log('初始化进度' + res.progress);
if (res.progress > count) count += 10;
if (count >= 90) {
clearInterval(timer);
timer = null;
}
});
} else {
open()
}
2023-10-08 14:17:20 +08:00
});
2023-11-22 11:47:40 +08:00
2023-10-08 14:17:20 +08:00
},
2023-11-22 11:47:40 +08:00
fail: (err) => {
2023-10-08 14:17:20 +08:00
console.log('错误:', err);
}
})
2023-11-22 11:47:40 +08:00
2023-10-08 14:17:20 +08:00
};
2023-10-28 11:30:46 +08:00
// 按远程文件地址加载小程序
const loadMPdns = async (e) => {
2023-11-22 11:47:40 +08:00
appid = e.id;
2023-10-28 11:30:46 +08:00
let FURL = e.url;
2023-11-22 11:47:40 +08:00
uni.showLoading({
title: '初始化中...',
mask: true
})
2023-10-28 11:30:46 +08:00
let count = 0;
timer = setInterval(() => {
if (count < 100) uni.showLoading({
2023-11-22 11:47:40 +08:00
title: `初始化中... ${count}%`,
mask: true
})
else uni.showLoading({
title: '初始化中...99%',
mask: true
})
2023-10-28 11:30:46 +08:00
}, 600)
let downloadTask = uni.downloadFile({
2023-11-22 11:47:40 +08:00
url: FURL,
success(res) {
wgtFile = res.tempFilePath;
console.log('初始化完成', wgtFile);
setTimeout(() => {
installMP();
clearInterval(timer);
timer = null;
}, 700)
},
2023-10-28 11:30:46 +08:00
fail(res) {
clearInterval(timer);
timer = null;
uni.hideLoading();
}
});
downloadTask.onProgressUpdate((res) => {
2023-11-22 11:47:40 +08:00
// console.log('初始化进度' + res.progress);
if (res.progress > count) count += 10;
if (count >= 90) {
clearInterval(timer);
timer = null;
}
2023-10-28 11:30:46 +08:00
});
2023-11-22 11:47:40 +08:00
2023-10-11 10:01:28 +08:00
};
2023-10-08 14:17:20 +08:00
// 小程序版本信息
const getVersion = (id) => {
2023-11-22 11:47:40 +08:00
appid = id;
return new Promise((resolve, reject) => {
mp.getUniMPVersion(appid, (ret) => {
console.log('供销', ret);
});
})
2023-10-08 14:17:20 +08:00
}
// 初始化小程序
const installMP = () => {
2023-11-22 11:47:40 +08:00
mp.getUniMPVersion(appid, (ret) => {
doInstallMP();
console.log('getUniMPVersion: ' + JSON.stringify(ret));
});
2023-10-08 14:17:20 +08:00
};
const doInstallMP = () => {
2023-11-22 11:47:40 +08:00
mp.installUniMP({
appid: appid,
wgtFile: wgtFile
}, (r) => {
if (0 == r.code) {
open();
} else {
uni.hideLoading();
clearInterval(timer);
timer = null;
uni.showModal({
title: '初始化失败',
content: JSON.stringify(r),
showCancel: false
});
}
console.log('初始化: ' + JSON.stringify(r));
});
2023-10-08 14:17:20 +08:00
};
const open = (id = null) => {
2023-11-22 11:47:40 +08:00
let token = uni.getStorageSync('LOGIN_STATUS_TOKEN');
console.log(token);
if (!token) return uni.showToast({
icon: 'none',
title: '请先登录'
})
let avatar = store?.state?.userInfo?.avatar;
mp.openUniMP({
appid: id || appid,
extraData: {
uniMP: true,
token: token,
avatar: avatar,
}
}, (ret) => {
uni.hideLoading();
if (0 != ret.code) {
uni.showModal({
title: '启动失败',
content: JSON.stringify(ret),
showCancel: false
});
}
console.log('openUniMP: ' + JSON.stringify(ret));
});
2023-10-08 14:17:20 +08:00
}
export default {
2023-11-22 11:47:40 +08:00
loadMPcenter,
loadMP,
loadMPurl,
loadMPx,
2023-10-28 11:30:46 +08:00
loadMPdns,
2023-11-22 11:47:40 +08:00
installMP,
doInstallMP,
getVersion,
open
2023-10-08 14:17:20 +08:00
}