243 lines
7.1 KiB
JavaScript
243 lines
7.1 KiB
JavaScript
export default {
|
|
namespaced: true,
|
|
state: {
|
|
syncStatus: 'idle', // idle, syncing, success, error
|
|
lastSyncTime: null,
|
|
syncProgress: 0,
|
|
pendingSyncCount: 0,
|
|
syncLog: []
|
|
},
|
|
mutations: {
|
|
SET_SYNC_STATUS(state, status) {
|
|
state.syncStatus = status
|
|
},
|
|
SET_LAST_SYNC_TIME(state, time) {
|
|
state.lastSyncTime = time
|
|
},
|
|
SET_SYNC_PROGRESS(state, progress) {
|
|
state.syncProgress = progress
|
|
},
|
|
SET_PENDING_SYNC_COUNT(state, count) {
|
|
state.pendingSyncCount = count
|
|
},
|
|
ADD_SYNC_LOG(state, log) {
|
|
state.syncLog.unshift(log)
|
|
// 只保留最近50条日志
|
|
if (state.syncLog.length > 50) {
|
|
state.syncLog.splice(50)
|
|
}
|
|
},
|
|
CLEAR_SYNC_LOG(state) {
|
|
state.syncLog = []
|
|
}
|
|
},
|
|
actions: {
|
|
async syncData({ commit, dispatch, state }) {
|
|
if (state.syncStatus === 'syncing') {
|
|
console.log('同步已在进行中')
|
|
return
|
|
}
|
|
|
|
try {
|
|
commit('SET_SYNC_STATUS', 'syncing')
|
|
commit('SET_SYNC_PROGRESS', 0)
|
|
|
|
// 记录开始同步日志
|
|
commit('ADD_SYNC_LOG', {
|
|
type: 'start',
|
|
message: '开始数据同步',
|
|
timestamp: new Date().toISOString()
|
|
})
|
|
|
|
// 获取待同步数据
|
|
const pendingData = await dispatch('getPendingSyncData')
|
|
|
|
if (pendingData.length === 0) {
|
|
commit('SET_SYNC_STATUS', 'success')
|
|
commit('SET_SYNC_PROGRESS', 100)
|
|
commit('ADD_SYNC_LOG', {
|
|
type: 'info',
|
|
message: '没有待同步的数据',
|
|
timestamp: new Date().toISOString()
|
|
})
|
|
return
|
|
}
|
|
|
|
// 开始同步
|
|
let successCount = 0
|
|
const totalCount = pendingData.length
|
|
|
|
for (let i = 0; i < pendingData.length; i++) {
|
|
const item = pendingData[i]
|
|
|
|
try {
|
|
// 模拟同步到服务器
|
|
await dispatch('syncItemToServer', item)
|
|
|
|
// 标记为已同步
|
|
await dispatch('markItemSynced', item)
|
|
|
|
successCount++
|
|
commit('SET_SYNC_PROGRESS', Math.round((i + 1) / totalCount * 100))
|
|
|
|
commit('ADD_SYNC_LOG', {
|
|
type: 'success',
|
|
message: `成功同步: ${item.type} - ${item.data.id}`,
|
|
timestamp: new Date().toISOString()
|
|
})
|
|
|
|
} catch (error) {
|
|
commit('ADD_SYNC_LOG', {
|
|
type: 'error',
|
|
message: `同步失败: ${item.type} - ${item.data.id} - ${error.message}`,
|
|
timestamp: new Date().toISOString()
|
|
})
|
|
}
|
|
|
|
// 模拟网络延迟
|
|
await new Promise(resolve => setTimeout(resolve, 500))
|
|
}
|
|
|
|
// 更新同步状态
|
|
if (successCount === totalCount) {
|
|
commit('SET_SYNC_STATUS', 'success')
|
|
} else {
|
|
commit('SET_SYNC_STATUS', 'error')
|
|
}
|
|
|
|
// 更新最后同步时间
|
|
const now = new Date().toISOString()
|
|
commit('SET_LAST_SYNC_TIME', now)
|
|
uni.setStorageSync('lastSyncTime', now)
|
|
|
|
// 更新待同步数量
|
|
await dispatch('updatePendingSyncCount')
|
|
|
|
// 记录同步完成日志
|
|
commit('ADD_SYNC_LOG', {
|
|
type: 'complete',
|
|
message: `同步完成: ${successCount}/${totalCount} 成功`,
|
|
timestamp: new Date().toISOString()
|
|
})
|
|
|
|
} catch (error) {
|
|
console.error('同步数据失败:', error)
|
|
commit('SET_SYNC_STATUS', 'error')
|
|
commit('ADD_SYNC_LOG', {
|
|
type: 'error',
|
|
message: `同步过程出错: ${error.message}`,
|
|
timestamp: new Date().toISOString()
|
|
})
|
|
}
|
|
},
|
|
|
|
async getPendingSyncData({ }) {
|
|
try {
|
|
const pendingSync = uni.getStorageSync('pendingSync') || []
|
|
return pendingSync
|
|
} catch (error) {
|
|
console.error('获取待同步数据失败:', error)
|
|
return []
|
|
}
|
|
},
|
|
|
|
async syncItemToServer({ }, item) {
|
|
// 模拟同步到服务器
|
|
// 实际应用中应该调用真实的API
|
|
return new Promise((resolve, reject) => {
|
|
setTimeout(() => {
|
|
// 模拟95%的成功率
|
|
if (Math.random() > 0.05) {
|
|
resolve({ success: true, message: '同步成功' })
|
|
} else {
|
|
reject(new Error('网络错误'))
|
|
}
|
|
}, 1000)
|
|
})
|
|
},
|
|
|
|
async markItemSynced({ }, item) {
|
|
try {
|
|
const pendingSync = uni.getStorageSync('pendingSync') || []
|
|
const updatedSync = pendingSync.filter(syncItem =>
|
|
!(syncItem.type === item.type && syncItem.data.id === item.data.id)
|
|
)
|
|
|
|
uni.setStorageSync('pendingSync', updatedSync)
|
|
|
|
// 更新本地记录状态
|
|
const recordKey = item.type === 'inbound' ? 'inboundRecords' :
|
|
item.type === 'outbound' ? 'outboundRecords' :
|
|
item.type === 'inventory' ? 'inventoryRecords' : null
|
|
|
|
if (recordKey) {
|
|
const records = uni.getStorageSync(recordKey) || []
|
|
const updatedRecords = records.map(record => {
|
|
if (record.id === item.data.id) {
|
|
return { ...record, syncStatus: 'synced' }
|
|
}
|
|
return record
|
|
})
|
|
uni.setStorageSync(recordKey, updatedRecords)
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('标记同步成功失败:', error)
|
|
throw error
|
|
}
|
|
},
|
|
|
|
async updatePendingSyncCount({ commit }) {
|
|
try {
|
|
const pendingSync = uni.getStorageSync('pendingSync') || []
|
|
commit('SET_PENDING_SYNC_COUNT', pendingSync.length)
|
|
} catch (error) {
|
|
console.error('更新待同步数量失败:', error)
|
|
}
|
|
},
|
|
|
|
async uploadErrorLogs({ }, logs) {
|
|
// 模拟上传错误日志到服务器
|
|
console.log('上传错误日志:', logs.length, '条')
|
|
},
|
|
|
|
async startAutoSync({ dispatch, state, rootState }) {
|
|
// 启动自动同步
|
|
const syncInterval = rootState.app.settings.syncInterval || 5
|
|
|
|
setInterval(async () => {
|
|
if (state.syncStatus !== 'syncing') {
|
|
await dispatch('syncData')
|
|
}
|
|
}, syncInterval * 60 * 1000)
|
|
}
|
|
},
|
|
getters: {
|
|
syncStatusText: state => {
|
|
const statusMap = {
|
|
idle: '待同步',
|
|
syncing: '同步中',
|
|
success: '同步成功',
|
|
error: '同步失败'
|
|
}
|
|
return statusMap[state.syncStatus] || '未知状态'
|
|
},
|
|
lastSyncTimeText: state => {
|
|
if (!state.lastSyncTime) return '从未同步'
|
|
|
|
const date = new Date(state.lastSyncTime)
|
|
const now = new Date()
|
|
const diff = now - date
|
|
|
|
if (diff < 60000) {
|
|
return '刚刚'
|
|
} else if (diff < 3600000) {
|
|
return Math.floor(diff / 60000) + '分钟前'
|
|
} else if (diff < 86400000) {
|
|
return Math.floor(diff / 3600000) + '小时前'
|
|
} else {
|
|
return date.toLocaleDateString('zh-CN')
|
|
}
|
|
}
|
|
}
|
|
} |