需求:
在公司小程序項(xiàng)目中,需要開發(fā)限時(shí)活動(dòng)變價(jià)的功能可训,開發(fā)完成以后投入測試捶枢,剛開始測試通過的,后來發(fā)布發(fā)現(xiàn)在IOS端存在異常谨胞,IOS端時(shí)間格式不識別的問題蒜鸡,想必大佬們都了解,(就是IOS不識別'2019-12-05'這種帶’-‘的時(shí)間格式叶沛,需要轉(zhuǎn)換為IOS支持的’2019/12/05‘)
部分關(guān)鍵代碼:
原代碼:
因?yàn)轫?xiàng)目接口返回?cái)?shù)據(jù)的問題忘朝,需要手動(dòng)先把活動(dòng)開始時(shí)間和結(jié)束時(shí)間保存跟對應(yīng)顏色的商品關(guān)聯(lián)起來。所以手動(dòng)保存相關(guān)字段
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
}
對比時(shí)間,判斷當(dāng)前是否符合活動(dòng)時(shí)間(看需求是否需要加定時(shí)器每秒自動(dòng)更新判斷時(shí)間區(qū)域)
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
}
}
修復(fù)只用修復(fù)存儲時(shí)的時(shí)間部分代碼即可,主要就是給存儲的開始時(shí)間和結(jié)束時(shí)間進(jìn)行正則匹配替換
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
}
調(diào)用方法不用修改
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
}
}
其實(shí)很簡單的一個(gè)正則匹配替換就能解決問題旱捧,關(guān)鍵就是想到這個(gè)點(diǎn)踩麦,因?yàn)樾〕绦蜷_發(fā)工具中模擬ios調(diào)試是正常的谓谦,真機(jī)調(diào)試中也是正常的贫橙,只有發(fā)布以后在ios才會出現(xiàn)問題,就很不容易調(diào)試反粥,很難發(fā)現(xiàn)病因卢肃!