Commit 32e40361 by liimin

fix(ReceptionCenter): 修复记录列表响应式问题并优化代码结构

重构 RecordList.vue 组件,解决 Vue2 响应式更新问题,使用 $set 确保数组更新触发视图渲染
优化代码结构,将网络请求和数据处理逻辑拆分为独立方法
修复日志级别从 info 改为 warn 以更好标识异常记录恢复
1 parent 082f121d
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
</view> </view>
</template> </template>
</view> </view>
</view> </view>
<scroll-view class="record-list" scroll-y @scrolltolower="pageonReachBottom"> <scroll-view class="record-list" scroll-y @scrolltolower="pageonReachBottom">
<view class="record-tips flex" v-if="tabIndex == 1 && isPrePage && list.length && list[0] && list[0].length"> <view class="record-tips flex" v-if="tabIndex == 1 && isPrePage && list.length && list[0] && list[0].length">
...@@ -280,51 +279,51 @@ export default { ...@@ -280,51 +279,51 @@ export default {
let recordFailListAll = await uni.$dbRecord.getRecords(); let recordFailListAll = await uni.$dbRecord.getRecords();
this.recordFailList = recordFailListAll.reverse(); this.recordFailList = recordFailListAll.reverse();
}, },
async getPreRecordFail () {
await this.getPreRecordNum()
this.list[0] = this.preRecordFailList.reverse().map(i => {
return Object.assign(i, {
duration: 0,
})
})
this.isPrePage = true
this.flag = true
this.nothing = !!this.preRecordFailList.length
},
async getPreRecordNum () { async getPreRecordNum () {
if (!this.recorder) {
this.recorder = this.getRecorder()
}
if (!this.recorder) return
const preRecordList = await this.recorder.getRecords() const preRecordList = await this.recorder.getRecords()
this.preRecordFailList =this.formatRecordList(preRecordList) this.preRecordFailList = this.formatRecordList(preRecordList || [])
//[{
// result: '',
// customerPhone: '',
// customerType: 'VIP',
// customerName: '',
// carType: '',
// startTime: '',
// endTime: '' }]
}, },
formatRecordList(list) { formatRecordList(list) {
if (!list) return []
list.forEach(record => { list.forEach(record => {
// return { let duration = ''
// result: record.result, if (record.startTime && record.endTime) {
// customerPhone: record.customerPhone, const startTime = new Date(record.startTime).getTime()
// customerType: record.customerType, const endTime = new Date(record.endTime).getTime()
// customerName: record.customerName || '未填写姓名', duration = this.formatSeconds(endTime - startTime)
// carType: record.carType || '未选择车型', }
record.duration=this.formatSeconds(new Date(record.endTime).getTime() - new Date(record.startTime).getTime()) record.duration = duration
// startTime: record.startTime,
// endTime: record.endTime
// }
}) })
return list return list
}, },
getRecordFail () { async getRecordFail () {
this.getRecordNum() await this.getRecordNum()
this.list[0] = this.formatRecordList(this.recordFailList) // 修复 Vue2 响应式问题:直接通过索引赋值无法触发视图更新
this.$set(this.list, 0, this.formatRecordList(this.recordFailList))
this.flag = true this.flag = true
this.isPrePage = false this.isPrePage = false
this.nothing = !!this.recordFailList.length this.nothing = !!this.recordFailList.length
}, },
async getPreRecordFail () {
await this.getPreRecordNum()
// 这里的 preRecordFailList 是一个数组,我们需要创建一个新数组来触发 reverse,避免修改原数组(虽然这里修改原数组也没大碍)
// 关键:确保 formattedList 是一个数组
const formattedList = [...this.preRecordFailList].reverse()
// 修复 Vue2 响应式问题
// 确保 list[0] 被正确设置
this.$set(this.list, 0, formattedList)
this.isPrePage = true
this.flag = true
// 使用 formattedList.length 来判断,更准确
this.nothing = !formattedList.length
},
init () { init () {
this.getReceptionList() this.getReceptionList()
this.recorder = this.getRecorder() this.recorder = this.getRecorder()
...@@ -334,15 +333,29 @@ export default { ...@@ -334,15 +333,29 @@ export default {
try { try {
await uni.$isNetWork() await uni.$isNetWork()
} catch (error) { } catch (error) {
const cache = JSON.parse(uni.$storage.get("recordSuccessList") || "{}") this.loadCacheData()
this.$set(this.list, 0, cache[this.tabIndex] || [])
this.flag = true
return return
} }
// 2. 请求数据 // 2. 请求数据
uni.showLoading({ mask: true }) uni.showLoading({ mask: true })
try { try {
await this.fetchNetworkData()
} catch (error) {
console.error('Fetch reception list failed:', error)
} finally {
this.flag = true
uni.hideLoading()
}
},
loadCacheData() {
const cache = JSON.parse(uni.$storage.get("recordSuccessList") || "{}")
this.$set(this.list, 0, cache[this.tabIndex] || [])
this.flag = true
},
async fetchNetworkData() {
const { tabIndex, pageNum, pageSize, reviewFlag, orderFinishFlag } = this const { tabIndex, pageNum, pageSize, reviewFlag, orderFinishFlag } = this
// const { shopMerchantId, sceneType } = uni.$storage.get("userInfo") || {} // const { shopMerchantId, sceneType } = uni.$storage.get("userInfo") || {}
const { records = [], total = 0 } = await uni.$api.reception.getList({ const { records = [], total = 0 } = await uni.$api.reception.getList({
...@@ -350,39 +363,28 @@ export default { ...@@ -350,39 +363,28 @@ export default {
pageNum, pageNum,
pageSize, pageSize,
reviewFlag, reviewFlag,
result:orderFinishFlag===1?'完成':'未完成', result: orderFinishFlag === 1 ? '完成' : '未完成',
// bussinessType: sceneType == 0 ? 1 : 2, // bussinessType: sceneType == 0 ? 1 : 2,
})||{} }) || {}
// 3. 数据处理 // 3. 数据处理
records.reverse().forEach(e => { records.reverse().forEach(e => {
e.duration = e.durationStr || this.formatSeconds(new Date(e.endTime).getTime() - new Date(e.startTime).getTime()) e.duration = e.durationStr || this.formatSeconds(new Date(e.endTime).getTime() - new Date(e.startTime).getTime())
e.orderFinishFlag = e.result === '完成'?1:0 e.orderFinishFlag = e.result === '完成' ? 1 : 0
}) })
// 4. 更新缓存 (仅第一页) // 4. 更新缓存 (仅第一页)
if (pageNum === 1) { if (pageNum === 1) {
const cache = JSON.parse(uni.$storage.get("recordSuccessList") || "{}") const cache = JSON.parse(uni.$storage.get("recordSuccessList") || "{}")
cache[tabIndex] = records cache[tabIndex] = records
uni.$storage.set("recordSuccessList", JSON.stringify(cache)) uni.$storage.set("recordSuccessList", JSON.stringify(cache))
} }
// 5. 更新视图 // 5. 更新视图
this.$set(this.list, pageNum - 1, records) this.$set(this.list, pageNum - 1, records)
this.total = total this.total = total
} catch (error) {
console.error('Fetch reception list failed:', error)
} finally {
this.flag = true
uni.hideLoading()
}
},
formatTime (num) {
let minutes = Math.floor(num / 60) || '0',
seconds = num % 60 || '0'
seconds = seconds < 10 ? `0${seconds}` : seconds
return {
minutes,
seconds
}
}, },
formatSeconds (seconds) { formatSeconds (seconds) {
seconds = Math.floor(seconds / 1000) seconds = Math.floor(seconds / 1000)
let formattedTime = '' let formattedTime = ''
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
* @Author: limin * @Author: limin
* @Date: 2025-01-02 18:49:17 * @Date: 2025-01-02 18:49:17
* @LastEditors: limin * @LastEditors: limin
* @LastEditTime: 2025-12-30 18:24:02 * @LastEditTime: 2025-12-30 19:14:28
* @FilePath: \simgle_app\utils\recordManager\TempRecord.js * @FilePath: \simgle_app\utils\recordManager\TempRecord.js
*/ */
import utils from '../utils' import utils from '../utils'
...@@ -106,7 +106,7 @@ class TempRecord { ...@@ -106,7 +106,7 @@ class TempRecord {
async recoverRecord(record) { async recoverRecord(record) {
if (!record) return if (!record) return
uni.$logger.local.info('〖接待记录Record〗 == 开始恢复异常记录 ==' + record.id) uni.$logger.local.warn('〖接待记录Record〗 == 开始恢复异常记录 ==' + record.id)
// 复用 save 逻辑进行持久化 // 复用 save 逻辑进行持久化
// 专门的入口有利于后续扩展,比如在这里添加恢复失败的特殊重试机制 // 专门的入口有利于后续扩展,比如在这里添加恢复失败的特殊重试机制
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!