/*
 * @Author: PoJun
 * @Date: 2023-12-14 15:56:24
 * @LastEditors: PoJun
 * @LastEditTime: 2024-05-10 14:40:24
 * @Message: Nothing
 */
export const logoutApp = async vm => {
    // 退出设备
    try {
        // 消息相关先注释掉
        // #ifdef APP-PLUS
        // await uni.$uv.http.get("/system_ext/user/client_info", { custom: { toast: false } });
        // #endif
        uni.removeStorageSync("vuex_token");
        uni.removeStorageSync("lifeData");
        vm.$uv.vuex("vuex_userInfo", null);
        uni.$uv.sleep(100).then(() => {
            uni.$uv.route({ url: "/pages/mine/index", type: "switchTab" });
        });
    } catch (error) {
        console.log(error);
    }
};

export const checkAppUpdate = async needToast => {
    try {
        const sysInfo = uni.getSystemInfoSync();
        if (sysInfo.osName.toLowerCase() !== "android") {
            return;
        }
        const res = await uni.request({
            url: "https://www.pgyer.com/apiv2/app/check",
            data: {
                appKey: "145b8b88437f52fc04ec8409c2d2c2b6",
                _api_key: "d6e02150ac8f6289081185e041137f7d", // 蒲公英账号私人key
            },
        });
        if (res.statusCode === 200) {
            const latestVersionInfo = res.data?.data || {};
            const currentVersion = sysInfo.appVersion;

            if (latestVersionInfo.buildVersion !== currentVersion) {
                // 有新版本，提示用户更新
                uni.showModal({
                    title: "发现新版本",
                    content: `最新版本：${latestVersionInfo.buildVersion}，更新内容：${latestVersionInfo.buildUpdateDescription}`,
                    confirmText: "立即更新",
                    success: async modalRes => {
                        if (modalRes.confirm) {
                            // 打开蒲公英的下载链接
                            plus.runtime.openURL(latestVersionInfo.downloadURL);
                        }
                    },
                });
            } else {
                if (needToast) {
                    uni.showToast({ title: "当前已是最新版本！", icon: "none" });
                }
            }
        }
    } catch (error) {
        console.error("检查更新失败", error);
    }
};
