123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- export function downloadApkWithJsonPost(apiUrl, requestData) {
- fetch(apiUrl, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(requestData),
- })
- .then((response) => {
- if (!response.ok) throw new Error("下载失败");
- return response.blob(); // 获取二进制 APK 文件
- })
- .then((blob) => {
- saveBlobToFile(blob); // 保存到本地
- })
- .catch((err) => {
- plus.nativeUI.alert("更新失败:" + err.message);
- });
- }
- function saveBlobToFile(blob) {
- const apkPath = "_doc/update/app.apk";
- plus.io.resolveLocalFileSystemURL("_doc/", function (dirEntry) {
- dirEntry.getDirectory("update", { create: true }, function (updateDir) {
- updateDir.getFile("app.apk", { create: true }, function (fileEntry) {
- fileEntry.createWriter(function (writer) {
- writer.onwrite = function () {
- console.log("文件保存成功");
- installApk(fileEntry.toLocalURL());
- };
- writer.onerror = function (e) {
- plus.nativeUI.alert("保存文件失败:" + e.message);
- };
- writer.write(blob);
- });
- });
- });
- });
- }
- function installApk(filepath) {
- plus.runtime.install(
- filepath,
- {
- force: true,
- },
- function () {
- plus.nativeUI.alert("更新完成,应用即将重启", function () {
- plus.runtime.restart();
- });
- },
- function (e) {
- plus.nativeUI.alert("安装失败:" + JSON.stringify(e));
- }
- );
- }
|