389 lines
8.6 KiB
JavaScript
Raw Permalink Normal View History

2024-05-07 17:31:34 +08:00
import {
Toast
} from "../libs/uniApi";
2024-02-26 13:35:08 +08:00
import {
getGXconfig,
miniapp
} from "@/api/uniMP.js";
import {
ENV
} from '@/config/app';
const mp = uni.requireNativePlugin('uniMP');
import store from "@/store/modules/app.js"
let appid = ''; // 应用id
let wgtFile = ''; // 应用文件地址
let timer = null; // 加载计时器
// 比较版本号大小
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;
let info = await getGXconfig();
uni.$emit('showLoading', true, '初始化中');
mp.getUniMPVersion(id, (ret) => {
let flag;
if (ENV == 'prod') {
flag = false
} else {
flag = true
}
if (0 != ret.code || compareVersions(info.data.version, ret.versionInfo.name) == 1 || flag ==
true) {
let count = 0;
timer = setInterval(() => {
2024-04-29 23:05:04 +08:00
if (count < 100 && count > 0) uni.$emit('showLoading', true,
`初始化中... ${count}%`)
2024-02-26 13:35:08 +08:00
else uni.$emit('showLoading', true, `初始化中... 99%`)
}, 200)
let downloadTask = uni.downloadFile({
2024-05-05 22:22:45 +08:00
url: info.data.version_info?.dow_url,
// url: "https://lihai001.oss-cn-chengdu.aliyuncs.com/attach/36e40202405031202367449.wgt",
2024-02-26 13:35:08 +08:00
success(res) {
wgtFile = res.tempFilePath;
2024-04-29 23:05:04 +08:00
setTimeout(() => {
installMP();
clearInterval(timer);
timer = null;
2024-02-26 13:35:08 +08:00
}, 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()
}
});
};
//加载商城小程序
const loadMPx = async (id) => {
2024-04-29 23:05:04 +08:00
2024-02-26 13:35:08 +08:00
appid = id;
let info = await miniapp();
console.log('最新版本', info.data);
console.log(mp, uni);
// return ;
uni.$emit('showLoading', true, `初始化中...`)
mp.getUniMPVersion(id, (ret) => {
console.log('当前版本', ret);
let flag;
if (ENV == 'prod') {
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(() => {
2024-04-29 23:05:04 +08:00
if (count < 100 && count > 0) uni.$emit('showLoading', true,
`初始化中... ${count}%`)
2024-02-26 13:35:08 +08:00
else uni.$emit('showLoading', true, `初始化中... 99%`)
}, 200)
let downloadTask = uni.downloadFile({
2024-05-03 15:02:21 +08:00
url: './__UNI__7626C0F.wgt',
2024-02-26 13:35:08 +08:00
success(res) {
wgtFile = res.tempFilePath;
console.log('初始化完成', wgtFile);
2024-04-29 23:05:04 +08:00
setTimeout(() => {
installMP();
clearInterval(timer);
timer = null;
2024-02-26 13:35:08 +08:00
}, 700)
},
fail(res) {
clearInterval(timer);
timer = null;
uni.hideLoading();
}
});
downloadTask.onProgressUpdate((res) => {
if (res.progress > count) count += 10;
if (count >= 90) {
clearInterval(timer);
timer = null;
}
});
} else {
open()
}
});
};
// 按信息接口加载小程序
const loadMPurl = async (e) => {
appid = e.id;
2024-04-29 23:05:04 +08:00
let FURL = e.url;
2024-02-26 13:35:08 +08:00
uni.$emit('showLoading', true, `初始化中...`)
2024-04-29 23:05:04 +08:00
uni.request({
url: FURL,
method: 'GET',
success: (fileInfo) => {
mp.getUniMPVersion(appid, (ret) => {
console.log('当前版本', ret);
let flag;
if (ENV == 'prod') {
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 && count > 0) uni.$emit('showLoading', true,
`初始化中... ${count}%`)
else uni.$emit('showLoading', true, `初始化中... 99%`)
}, 200)
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()
}
});
},
fail: (err) => {
console.log('错误:', err);
}
})
2024-02-26 13:35:08 +08:00
};
// 商城配置的小程序
const loadAppletMP = async (data) => {
2024-05-07 17:31:34 +08:00
// console.log(data)
// return
2024-02-26 13:35:08 +08:00
appid = data.app_id;
// return ;
uni.$emit('showLoading', true, '初始化中');
mp.getUniMPVersion(appid, (ret) => {
let flag;
if (ENV == 'prod') {
flag = false
} else {
flag = true
}
2024-05-07 17:31:34 +08:00
if (data.showToast) {
uni.showLoading({
title: '正在加载中...'
});
}
2024-02-26 13:35:08 +08:00
if (0 != ret.code || compareVersions(data.version, ret.versionInfo.name) == 1 || flag ==
true) {
let count = 0;
timer = setInterval(() => {
2024-04-29 23:05:04 +08:00
if (count < 100 && count > 0) uni.$emit('showLoading', true,
`初始化中... ${count}%`)
2024-02-26 13:35:08 +08:00
else uni.$emit('showLoading', true, `初始化中... 99%`)
}, 200)
2024-05-07 17:31:34 +08:00
2024-02-26 13:35:08 +08:00
let downloadTask = uni.downloadFile({
2024-05-05 22:22:45 +08:00
url: data.url,
// url: "https://lihai001.oss-cn-chengdu.aliyuncs.com/attach/3ffde202405031254278460.wgt",
2024-02-26 13:35:08 +08:00
success(res) {
wgtFile = res.tempFilePath;
console.log('初始化完成', wgtFile);
2024-04-29 23:05:04 +08:00
setTimeout(() => {
installMP(data);
clearInterval(timer);
timer = null;
2024-02-26 13:35:08 +08:00
}, 700)
},
fail(res) {
clearInterval(timer);
timer = null;
uni.hideLoading();
}
});
downloadTask.onProgressUpdate((res) => {
if (res.progress > count) count += 10;
if (count >= 90) {
clearInterval(timer);
timer = null;
}
});
} else {
open()
}
});
};
// 按远程文件地址加载小程序
const loadMPdns = async (e) => {
appid = e.id;
2024-04-29 23:05:04 +08:00
let FURL = e.url;
2024-02-26 13:35:08 +08:00
uni.$emit('showLoading', true, `初始化中...`)
2024-04-29 23:05:04 +08:00
let count = 0;
timer = setInterval(() => {
if (count < 100 && count > 0) uni.$emit('showLoading', true, `初始化中... ${count}%`)
else uni.$emit('showLoading', true, `初始化中... 99%`)
}, 200)
let downloadTask = uni.downloadFile({
url: FURL,
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) => {
if (res.progress > count) count += 10;
if (count >= 90) {
clearInterval(timer);
timer = null;
}
});
2024-02-26 13:35:08 +08:00
};
// 小程序版本信息
const getVersion = (id) => {
appid = id;
return new Promise((resolve, reject) => {
mp.getUniMPVersion(appid, (ret) => {
console.log('供销', ret);
});
})
}
// 初始化小程序
2024-04-29 23:05:04 +08:00
const installMP = (data) => {
2024-02-26 13:35:08 +08:00
mp.getUniMPVersion(appid, (ret) => {
2024-04-29 23:05:04 +08:00
doInstallMP(data);
2024-02-26 13:35:08 +08:00
console.log('getUniMPVersion: ' + JSON.stringify(ret));
});
};
2024-04-29 23:05:04 +08:00
const doInstallMP = (data) => {
2024-02-26 13:35:08 +08:00
mp.installUniMP({
appid: appid,
wgtFile: wgtFile
}, (r) => {
if (0 == r.code) {
2024-04-29 23:05:04 +08:00
open(data);
2024-02-26 13:35:08 +08:00
} else {
uni.hideLoading();
clearInterval(timer);
timer = null;
uni.showModal({
title: '初始化失败',
content: JSON.stringify(r),
showCancel: false
});
}
console.log('初始化: ' + JSON.stringify(r));
});
};
const open = (id = null) => {
2024-05-07 17:31:34 +08:00
uni.hideLoading()
2024-04-29 23:05:04 +08:00
uni.$emit('showLoading', false);
2024-02-26 13:35:08 +08:00
let token = uni.getStorageSync('LOGIN_STATUS_TOKEN');
if (!token) return uni.showToast({
icon: 'none',
title: '请先登录'
})
let avatar = store?.state?.userInfo?.avatar;
mp.openUniMP({
2024-04-29 23:05:04 +08:00
appid: typeof(id) == 'object' ? appid : (id || appid),
2024-02-26 13:35:08 +08:00
extraData: {
uniMP: true,
token: token,
avatar: avatar,
2024-04-24 18:00:36 +08:00
},
2024-02-26 13:35:08 +08:00
}, (ret) => {
uni.hideLoading();
if (0 != ret.code) {
uni.showModal({
title: '启动失败',
content: JSON.stringify(ret),
showCancel: false
});
}
2024-04-29 23:05:04 +08:00
if (id.type == 2) {
2024-04-30 18:56:28 +08:00
setTimeout(() => {
console.log("发送给小程序的", id)
2024-05-03 15:02:21 +08:00
mp.sendUniMPEvent(appid, 'tocustomlist', id, (ret) => {
2024-04-30 18:56:28 +08:00
console.log("开始发送消息")
});
2024-05-05 22:22:45 +08:00
}, 1000)
2024-04-29 23:05:04 +08:00
}
2024-02-26 13:35:08 +08:00
});
}
export default {
loadMP,
loadMPurl,
loadMPx,
2024-04-29 23:05:04 +08:00
loadMPdns,
2024-02-26 13:35:08 +08:00
installMP,
2024-04-29 23:05:04 +08:00
loadAppletMP,
2024-02-26 13:35:08 +08:00
doInstallMP,
getVersion,
open
}