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

需求:

在公司小程序项目中,需要开发限时活动变价的功能,开发完成以后投入测试,刚开始测试通过的,后来发布发现在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才会出现问题,就很不容易调试,很难发现病因!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 侄女小悠三岁不到,喜欢吃肉,不喜欢吃青菜。好像很多小孩都这样。 大哥大嫂试了很多方法,比如把青菜剁碎混在肉丸子里,...
    暖暖2019暖暖阅读 819评论 0 2
  • 我还喜欢你 只是我不再喜欢我了 一味迎合你的我 敏感脆弱的我 失去自我的我 一个我完全不喜欢的 我 虽然努力虽然用...
    Poder_阅读 1,027评论 0 0
  • 2019.8.16学习分享: 1.相反框架 遇到问题先找方向,再找方法。 凡是遇到负面的事件转化为你想要的理想效果...
    似水流年cxy阅读 3,085评论 1 1
  • 2019-05-22 心惠语录: 人成了,事就成了。 别人怎样对你,是你自己吸引来到。 别人在你身边,是...
    helen海音阅读 968评论 0 0
  • 你们应该都看过电视吧,里面的每一个电视节目都会有导演吧。我可就当过导演,我还有一个“小助手”和一个“小演员”。...
    安琦_429f阅读 3,043评论 0 0