call-check-version.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. export type StoreListItem = {
  2. enable : boolean
  3. id : string
  4. name : string
  5. scheme : string
  6. priority : number // 优先级
  7. }
  8. export type UniUpgradeCenterResult = {
  9. _id : string
  10. appid : string
  11. name : string
  12. title : string
  13. contents : string
  14. url : string // 安装包下载地址
  15. platform : Array<string> // Array<'Android' | 'iOS' | 'Harmony'>
  16. version : string // 版本号 1.0.0
  17. uni_platform : string // "android" | "ios" | 'harmony'
  18. stable_publish : boolean // 是否是稳定版
  19. is_mandatory : boolean // 是否强制更新
  20. is_silently : boolean | null // 是否静默更新
  21. create_env : string // "upgrade-center"
  22. create_date : number
  23. message : string
  24. code : number
  25. type : string // "native_app" | "wgt"
  26. store_list : StoreListItem[] | null
  27. min_uni_version : string | null // 升级 wgt 的最低 uni-app 版本
  28. }
  29. export default function () : Promise<UniUpgradeCenterResult> {
  30. // #ifdef APP
  31. return new Promise<UniUpgradeCenterResult>((resolve, reject) => {
  32. const systemInfo = uni.getSystemInfoSync()
  33. const appId = systemInfo.appId
  34. const appVersion = systemInfo.appVersion //systemInfo.appVersion
  35. // #ifndef UNI-APP-X
  36. if (typeof appId === 'string' && typeof appVersion === 'string' && appId.length > 0 && appVersion.length > 0) {
  37. plus.runtime.getProperty(appId, function (widgetInfo) {
  38. if (widgetInfo.version) {
  39. let data = {
  40. action: 'checkVersion',
  41. appid: appId,
  42. appVersion: appVersion,
  43. wgtVersion: widgetInfo.version
  44. }
  45. uniCloud.callFunction({
  46. name: 'uni-upgrade-center',
  47. data,
  48. success: (e) => {
  49. resolve(e.result as UniUpgradeCenterResult)
  50. },
  51. fail: (error) => {
  52. reject(error)
  53. }
  54. })
  55. } else {
  56. reject('widgetInfo.version is EMPTY')
  57. }
  58. })
  59. } else {
  60. reject('plus.runtime.appid is EMPTY')
  61. }
  62. // #endif
  63. // #ifdef UNI-APP-X
  64. if (typeof appId === 'string' && typeof appVersion === 'string' && appId.length > 0 && appVersion.length > 0) {
  65. let data = {
  66. action: 'checkVersion',
  67. appid: appId,
  68. appVersion: appVersion,
  69. is_uniapp_x: true,
  70. wgtVersion: '0.0.0.0.0.1'
  71. }
  72. try {
  73. uniCloud.callFunction({
  74. name: 'uni-upgrade-center',
  75. data: data
  76. }).then(res => {
  77. const code = res.result['code']
  78. const codeIsNumber = ['Int', 'Long', 'number'].includes(typeof code)
  79. if (codeIsNumber) {
  80. if ((code as number) == 0) {
  81. reject({
  82. code: res.result['code'],
  83. message: res.result['message']
  84. })
  85. } else if ((code as number) < 0) {
  86. reject({
  87. code: res.result['code'],
  88. message: res.result['message']
  89. })
  90. } else {
  91. const result = JSON.parse<UniUpgradeCenterResult>(JSON.stringify(res.result)) as UniUpgradeCenterResult
  92. resolve(result)
  93. }
  94. }
  95. }).catch<void>((err : any | null) => {
  96. const error = err as UniCloudError
  97. if (error.errMsg == '未匹配到云函数[uni-upgrade-center]')
  98. error.errMsg = '【uni-upgrade-center-app】未配置uni-upgrade-center,无法升级。参考: https://uniapp.dcloud.net.cn/uniCloud/upgrade-center.html'
  99. reject(error.errMsg)
  100. })
  101. } catch (e) {
  102. reject(e.message)
  103. }
  104. } else {
  105. reject('invalid appid or appVersion')
  106. }
  107. // #endif
  108. })
  109. // #endif
  110. // #ifndef APP
  111. return new Promise((resolve, reject) => {
  112. reject({
  113. message: '请在App中使用'
  114. })
  115. })
  116. // #endif
  117. }