美文网首页微信小程序开发微信小程序开发
IOS苹果端微信小程序中时间、计时实现活动变价异常的解决方案!I

IOS苹果端微信小程序中时间、计时实现活动变价异常的解决方案!I

作者: 霖深雾起不见你 | 来源:发表于2019-12-05 17:08 被阅读0次

需求:

在公司小程序项目中,需要开发限时活动变价的功能,开发完成以后投入测试,刚开始测试通过的,后来发布发现在IOS端存在异常,IOS端时间格式不识别的问题,想必大佬们都了解,(就是IOS不识别'2019-12-05'这种带’-‘的时间格式,需要转换为IOS支持的’2019/12/05‘)

部分关键代码:

原代码:
因为项目接口返回数据的问题,需要手动先把活动开始时间和结束时间保存跟对应颜色的商品关联起来。所以手动保存相关字段
if (skus[i].skuProperties[0].strategyStartTime !== null && skus[i].skuProperties[0].strategyStartTime !== undefined) {
  that.colorPriceJson[key].startTime = skus[i].skuProperties[0].strategyStartTime
  that.colorPriceJson[key].endTime = skus[i].skuProperties[0].strategyEndTime
  that.colorPriceJson[key].strategyPrice = skus[i].skuProperties[0].strategyPrice
}
对比时间,判断当前是否符合活动时间(看需求是否需要加定时器每秒自动更新判断时间区域)
if (that.colorPriceJson[that.colorName].startTime !== null && that.colorPriceJson[that.colorName].endTime !== null) {
  let now = new Date().getTime() / 1000
  let start = new Date(that.colorPriceJson[that.colorName].startTime).getTime() / 1000
  let end = new Date(that.colorPriceJson[that.colorName].endTime).getTime() / 1000
  let x = end - start
  if (now - start > 0 && end - now > 0) {
    that.price = that.colorPriceJson[that.colorName].strategyPrice / 1
  }
}

修复只用修复存储时的时间部分代码即可,主要就是给存储的开始时间和结束时间进行正则匹配替换
if (skus[i].skuProperties[0].strategyStartTime !== null && skus[i].skuProperties[0].strategyStartTime !== undefined) {
  that.colorPriceJson[key].startTime = skus[i].skuProperties[0].strategyStartTime.replace(/-/g, '/')
  that.colorPriceJson[key].endTime = skus[i].skuProperties[0].strategyEndTime.replace(/-/g, '/')
  that.colorPriceJson[key].strategyPrice = skus[i].skuProperties[0].strategyPrice
}

调用方法不用修改
if (that.colorPriceJson[that.colorName].startTime !== null && that.colorPriceJson[that.colorName].endTime !== null) {
  let now = new Date().getTime() / 1000
  let start = new Date(that.colorPriceJson[that.colorName].startTime).getTime() / 1000
  let end = new Date(that.colorPriceJson[that.colorName].endTime).getTime() / 1000
  let x = end - start
  if (now - start > 0 && end - now > 0) {
    that.price = that.colorPriceJson[that.colorName].strategyPrice / 1
  }
}

其实很简单的一个正则匹配替换就能解决问题,关键就是想到这个点,因为小程序开发工具中模拟ios调试是正常的,真机调试中也是正常的,只有发布以后在ios才会出现问题,就很不容易调试,很难发现病因!

相关文章

网友评论

    本文标题:IOS苹果端微信小程序中时间、计时实现活动变价异常的解决方案!I

    本文链接:https://www.haomeiwen.com/subject/kwqjgctx.html