罗广辉
2026-04-24 648904d076ae6e17892b40675598b1c8dc474277
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * @Author       : yuan
 * @Date         : 2025-10-15 15:52:12
 * @LastEditors  : yuan
 * @LastEditTime : 2025-12-17 14:09:14
 * @FilePath     : \src\utils\common\index.js
 * @Description  :
 * Copyright 2025 OBKoro1, All Rights Reserved.
 * 2025-10-15 15:52:12
 */
// 小程序更新检测
import { useUserStore } from "@/store/index.js"
import configEnv from "@/config/env.js"
import process from "node:process"
 
export function mpUpdate () {
  const updateManager = uni.getUpdateManager()
  updateManager.onCheckForUpdate(res => {
    // 请求完新版本信息的回调
    console.log(res.hasUpdate)
  })
  updateManager.onUpdateReady(() => {
    uni.showModal({
      title: "更新提示",
      content: "检测到新版本,是否下载新版本并重启小程序?",
      success (res) {
        if (res.confirm) {
          // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
          updateManager.applyUpdate()
        }
      }
    })
  })
  updateManager.onUpdateFailed(() => {
    // 新的版本下载失败
    uni.showModal({
      title: "已经有新版本了哟~",
      content: "新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~",
      showCancel: false
    })
  })
}
export function getStatusBarHeight () {
  try {
    const systemInfo = uni.getWindowInfo()
 
    // #ifdef APP-PLUS
    return systemInfo.statusBarHeight || 0
    // #endif
 
    // #ifdef MP-WEIXIN
    return 0
    // #endif
 
    // #ifdef H5
    return systemInfo.statusBarHeight || 0
    // #endif
    return systemInfo.statusBarHeight || 0
  } catch (error) {
    return 0
  }
}
export function getEnvObj () {
  return configEnv[__APP_ENV__?.ENV_NAME] || {}
}
 
export function getWebViewUrl (targetUrl, otherParams) {
  const userStore = useUserStore()
  const url = getEnvObj().VITE_APP_WEBVIEW_URL
  const uniPlatform = __APP_ENV__.UNI_PLATFORM
  const statusBarHeight = getStatusBarHeight()
  // 1. 处理用户参数
  const userParams = userStore?.userInfo ? JSON.stringify(userStore.userInfo) : '{}'
  // 2. 构建查询参数字符串
  let queryString = `params=${encodeURIComponent(userParams)}&topMargin=${statusBarHeight}&uniPlatform=${uniPlatform}`
 
  // 3. 处理 otherParams 对象
  if (otherParams && typeof otherParams === 'object') {
    Object.keys(otherParams).forEach(key => {
      const value = otherParams[key]
      if (value !== undefined && value !== null) {
        queryString += `&${key}=${encodeURIComponent(value)}`
      }
    })
  }
  // 4. 拼接完整 URL
  return `${url}${targetUrl}?${queryString}`
}
 
export function getAssetsImage (targetUrl) {
  const url = getEnvObj().VITE_APP_ASSETS_URL
  return `${url}${targetUrl}`
}