helper.js 1.26 KB
import { storage } from '../../helper/storage/index'
import { webwx } from '../../helper/index'
import { GLSX_COMMON_VERSION_STORAGE_VERSION } from './constants'
const web = {
  getBody: data => {
    const v = storage.get(GLSX_COMMON_VERSION_STORAGE_VERSION)
    return result(v, data)
  },
  setVersion: version => storage.set(GLSX_COMMON_VERSION_STORAGE_VERSION, version)
}
const wx = {
  getBody: data => {
    const v = wx.getStorageSync(GLSX_COMMON_VERSION_STORAGE_VERSION)
    return result(v, data)
  },
  setVersion: version => wx.setStorage({ key: GLSX_COMMON_VERSION_STORAGE_VERSION, data: version })
}
function result(v, data) {
  const c = compare(v, data.version)
  return { compare: c, origin: v, new: data.version, isNew: c === -1 ? true : false, ...data }
}
function compare(version1, version2) {
  if (!version1) {
    return -1
  }
  const parts1 = version1.split('.')
  const parts2 = version2.split('.')
  for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
    const part1 = parseInt(parts1[i] || 0)
    const part2 = parseInt(parts2[i] || 0)

    if (part1 < part2) {
      return -1 // version1 < version2
    }
    if (part1 > part2) {
      return 1 // version1 > version2
    }
  }
  return 0 // version1 == version2
}
export default webwx(web, wx)