Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FR]: getSystemInfoSync 微信小程序从基础库 2.20.1 开始,本接口停止维护,需要兼容新老接口 #2973

Open
shushu2013 opened this issue Feb 7, 2025 · 3 comments · May be fixed by #2975
Assignees

Comments

@shushu2013
Copy link

shushu2013 commented Feb 7, 2025

这个功能解决了什么问题?

Image

你期望的组件设计是怎样的?

import Taro from "@tarojs/taro"

interface IDeviceInfo extends
  Omit<Taro.getDeviceInfo.Result, 'deviceAbi' | 'CPUType'> {
}

// 获取设备基础信息,兼容新旧 API
export function getDeviceInfo(): IDeviceInfo {
  // 先判断 Taro.canIUse 能否使用新的 API
  if (Taro.canIUse('getDeviceInfo')) {
    return Taro.getDeviceInfo()
  }

  return Taro.getSystemInfoSync()
}

// 获取窗口信息,兼容新旧 API
export function getWindowInfo(): Taro.getWindowInfo.Result {
  // 先判断 Taro.canIUse 能否使用新的 API
  if (Taro.canIUse('getWindowInfo')) {
    return Taro.getWindowInfo()
  }

  return Taro.getSystemInfoSync()
}

// 获取应用基础信息,兼容新旧 API
export function getAppBaseInfo(): Taro.getAppBaseInfo.Result {
  // 先判断 Taro.canIUse 能否使用新的 API
  if (Taro.canIUse('getAppBaseInfo')) {
    return Taro.getAppBaseInfo()
  }

  return Taro.getSystemInfoSync()
}

代码中通过 getSystemInfoSync 获取相应信息的地方,使用上面的方法兼容一下新老 api

@Alex-huxiyang Alex-huxiyang self-assigned this Feb 8, 2025
@coderabbitai coderabbitai bot linked a pull request Feb 8, 2025 that will close this issue
@shushu2013
Copy link
Author

shushu2013 commented Feb 9, 2025

@Alex-huxiyang Taro.getDeviceInfo 在支付宝小程序上获取的信息不对,需要单独判断下

// 获取设备基础信息,兼容新旧 API
export function getDeviceInfo(): IDeviceInfo {
    const isWeApp = Taro.getEnv() === Taro.ENV_TYPE.WEAPP

  // 先判断 Taro.canIUse 能否使用新的 API
  if (isWeApp && Taro.canIUse('getDeviceInfo')) {
    return Taro.getDeviceInfo()
  }

  return Taro.getSystemInfoSync()
}

给 Taro 提了个 issue NervJS/taro#17309

@Alex-huxiyang
Copy link
Collaborator

看你们的对话,似乎是支付宝getDeviceAPi的问题?

@Alex-huxiyang Alex-huxiyang linked a pull request Feb 10, 2025 that will close this issue
@shushu2013
Copy link
Author

shushu2013 commented Feb 10, 2025

看你们的对话,似乎是支付宝getDeviceAPi的问题?

是的,只能判断在微信小程序才用 Taro.getDeviceInfo(),其他环境下还是用 Taro.getSystemInfoSync() 获取设备信息,可以暂时规避这个问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants