helper.js 1.62 KB
import { storediff } from '../../helper/index'
import { GLSX_COMMON_VERSION_STORAGE_VERSION } from './constants'
const webwx = {
  getResult: function () {
    const { store_version_key, version: data } = this
    const v = storediff.get(store_version_key)
    const compare = compareVersions(v, data.version)
    return { compare, origin: v, isNew: compare === -1, ...data }
  },
  setVersion: function (version) {
    const v = storediff.get(this.store_version_key)
    if (v === version) return
    storediff.set(this.store_version_key, version)
  }
}
function compareVersions(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
}
function getParams(path,attr) {
  const _params =''
  if(path.includes('?')){
    _params = path.split('?')[1]
  }else{
    return ''
  }
  return new Map(
    _params
      .split('&')
      .map(kv => kv.split('='))
  ).get(attr)
}

export function setProduct(data) {
  const { moduleUrl } = data
  let mod = getParams(this.options.request.baseUrl, 'moduleUrl')
  if (!mod) {
    mod = moduleUrl
  }
  if (!mod) {
    mod = this.options.product
    Object.assign(data, { moduleUrl: mod })
  }
  this.store_version_key = `${GLSX_COMMON_VERSION_STORAGE_VERSION}_${mod.toUpperCase()}`
}


export default webwx