v1.2.0 bug修复
This commit is contained in:
@@ -1,4 +1,10 @@
|
||||
import { documentsList,findDataInfo} from '../../api/documentManagement'
|
||||
|
||||
/**
|
||||
* 本地存储选中的问题ID的键名(常见问题页)
|
||||
* @type {string}
|
||||
*/
|
||||
const SELECTED_PROBLEM_ID_KEY = 'commonProblem:selectedId'
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
@@ -46,14 +52,29 @@ export default {
|
||||
const res = await documentsList(params)
|
||||
if(res && res.code === 200){
|
||||
this.problems = res.rows;
|
||||
|
||||
if (this.$route.query.id) {
|
||||
this.DetailsParams.id = this.$route.query.id
|
||||
|
||||
}else if(this.problems.length > 0){
|
||||
this.DetailsParams.id = this.problems[0].id;
|
||||
}
|
||||
this.fetchProblemDetails(this.DetailsParams)
|
||||
|
||||
// 优先级:URL 参数 > 本地存储 > 列表首项
|
||||
const routeId = this.$route && this.$route.query && this.$route.query.id ? String(this.$route.query.id) : ''
|
||||
const savedId = this.getSavedSelectedId()
|
||||
let resolvedId = routeId || savedId || ''
|
||||
|
||||
if (!resolvedId && this.problems && this.problems.length > 0) {
|
||||
resolvedId = String(this.problems[0].id)
|
||||
}
|
||||
|
||||
if (resolvedId) {
|
||||
const existsInList = (this.problems || []).some(item => String(item.id) === String(resolvedId))
|
||||
if (!existsInList && this.problems && this.problems.length > 0) {
|
||||
resolvedId = String(this.problems[0].id)
|
||||
}
|
||||
}
|
||||
|
||||
this.DetailsParams.id = resolvedId
|
||||
this.persistSelectedId(this.DetailsParams.id)
|
||||
this.updateRouteQueryId(this.DetailsParams.id)
|
||||
if (this.DetailsParams.id) {
|
||||
this.fetchProblemDetails(this.DetailsParams)
|
||||
}
|
||||
}
|
||||
this.setLoading('problemLoading', false);
|
||||
},
|
||||
@@ -76,9 +97,61 @@ export default {
|
||||
* @param {number} id 问题ID
|
||||
*/
|
||||
handleClick(id) {
|
||||
|
||||
this.DetailsParams.id = id;
|
||||
this.persistSelectedId(this.DetailsParams.id)
|
||||
this.updateRouteQueryId(this.DetailsParams.id)
|
||||
this.fetchProblemDetails(this.DetailsParams)
|
||||
},
|
||||
|
||||
/**
|
||||
* 将选中的问题ID持久化到本地存储
|
||||
* @param {string|number} id 问题ID
|
||||
* @returns {void}
|
||||
*/
|
||||
persistSelectedId(id) {
|
||||
try {
|
||||
window.localStorage.setItem(SELECTED_PROBLEM_ID_KEY, String(id))
|
||||
} catch (error) {
|
||||
console.error('persistSelectedId error', error)
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 读取本地存储中保存的选中问题ID
|
||||
* @returns {string}
|
||||
*/
|
||||
getSavedSelectedId() {
|
||||
try {
|
||||
return window.localStorage.getItem(SELECTED_PROBLEM_ID_KEY) || ''
|
||||
} catch (error) {
|
||||
console.error('getSavedSelectedId error', error)
|
||||
return ''
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 使用 replace 同步更新当前路由的查询参数中的 id
|
||||
* 避免产生新的历史记录条目
|
||||
* @param {string|number} id 问题ID
|
||||
* @returns {void}
|
||||
*/
|
||||
updateRouteQueryId(id) {
|
||||
try {
|
||||
if (!this.$route) return
|
||||
const targetId = String(id)
|
||||
const currentId = this.$route && this.$route.query && this.$route.query.id ? String(this.$route.query.id) : ''
|
||||
if (currentId === targetId) return
|
||||
|
||||
const nextQuery = { ...(this.$route.query || {}), id: targetId }
|
||||
if (this.$router && typeof this.$router.replace === 'function') {
|
||||
const maybePromise = this.$router.replace({ query: nextQuery })
|
||||
if (maybePromise && typeof maybePromise.catch === 'function') {
|
||||
maybePromise.catch(() => {})
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('updateRouteQueryId error', error)
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user