Browse Source

高级搜索

master
han\hanst 1 month ago
parent
commit
b625a1e1e0
  1. 74
      src/main.js
  2. 49
      src/utils/ajax.js
  3. 52
      src/utils/httpRequest.js
  4. 38
      src/views/modules/sift/advancedSearchCenter.vue

74
src/main.js

@ -87,11 +87,49 @@ function pickFormulaValue (row) {
return formula.split('..')[0] || '' return formula.split('..')[0] || ''
} }
if (symbol === 'in' || symbol === 'ne') { if (symbol === 'in' || symbol === 'ne') {
return formula.split(';')[0] || ''
return formula
} }
return formula return formula
} }
function buildAdvancedContextStorageKey (path) {
return 'advanced_search_context_' + String(path || '')
}
function normalizeRoutePath (path) {
var p = String(path || '').trim()
if (!p) {
return ''
}
if (p.charAt(0) !== '/') {
p = '/' + p
}
return p
}
function parseAdvancedContextByPath (path) {
var normalizedPath = normalizeRoutePath(path)
var raw = sessionStorage.getItem(buildAdvancedContextStorageKey(normalizedPath))
if (!raw && normalizedPath.indexOf('/modules/') === 0) {
raw = sessionStorage.getItem(buildAdvancedContextStorageKey(normalizedPath.substring('/modules'.length)))
}
if (!raw && normalizedPath.indexOf('/modules/') !== 0) {
raw = sessionStorage.getItem(buildAdvancedContextStorageKey('/modules' + normalizedPath))
}
if (!raw) {
return null
}
try {
var context = JSON.parse(raw)
if (!context || !Array.isArray(context.conditions) || context.conditions.length <= 0) {
return null
}
return context
} catch (e) {
return null
}
}
Vue.mixin({ Vue.mixin({
mounted () { mounted () {
this.__applySavedRuleFromRoute() this.__applySavedRuleFromRoute()
@ -116,19 +154,21 @@ Vue.mixin({
return return
} }
var routeQuery = this.$route.query var routeQuery = this.$route.query
var routePath = normalizeRoutePath(this.$route.path || '')
var menuId = routeQuery.menuId var menuId = routeQuery.menuId
var itemNo = routeQuery.itemNo var itemNo = routeQuery.itemNo
if (!menuId || !itemNo) { if (!menuId || !itemNo) {
return return
} }
var routeMetaMenuId = this.$route.meta && this.$route.meta.menuId
if (routeMetaMenuId && String(routeMetaMenuId) !== String(menuId)) {
return
}
var cacheKey = [this.$route.path || '', menuId, itemNo].join('_') var cacheKey = [this.$route.path || '', menuId, itemNo].join('_')
if (this.__savedRuleAppliedKey === cacheKey) { if (this.__savedRuleAppliedKey === cacheKey) {
return return
} }
var existContext = parseAdvancedContextByPath(routePath)
if (existContext) {
this.__savedRuleAppliedKey = cacheKey
return
}
try { try {
var userId = String((this.$store && this.$store.state && this.$store.state.user && this.$store.state.user.id) || '') var userId = String((this.$store && this.$store.state && this.$store.state.user && this.$store.state.user.id) || '')
if (!userId) { if (!userId) {
@ -147,16 +187,20 @@ Vue.mixin({
if (!rows.length) { if (!rows.length) {
return return
} }
rows.forEach(row => {
var fieldName = row && row.fieldName ? String(row.fieldName) : ''
if (!fieldName) {
return
}
if (!Object.prototype.hasOwnProperty.call(queryModel, fieldName)) {
return
}
this.$set(queryModel, fieldName, pickFormulaValue(row))
})
try {
var contextRows = rows
.filter(row => row && row.fieldName && row.symbol && row.formula)
.map(row => ({
fieldName: String(row.fieldName),
symbol: String(row.symbol).toLowerCase(),
formula: String(row.formula)
}))
sessionStorage.setItem(buildAdvancedContextStorageKey(routePath), JSON.stringify({
menuUrl: routePath,
conditions: contextRows
}))
} catch (e) {
}
if (Object.prototype.hasOwnProperty.call(this, 'pageIndex')) { if (Object.prototype.hasOwnProperty.call(this, 'pageIndex')) {
this.pageIndex = 1 this.pageIndex = 1
} }

49
src/utils/ajax.js

@ -1,5 +1,6 @@
import Vue from "vue"; import Vue from "vue";
import axios from 'axios'; import axios from 'axios';
import router from '@/router'
const qs = require('qs'); const qs = require('qs');
var apiServer = (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? '/proxyApi/' : window.SITE_CONFIG.baseUrl); var apiServer = (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? '/proxyApi/' : window.SITE_CONFIG.baseUrl);
@ -8,6 +9,46 @@ const resolveAcceptLanguage = () => {
return locale === 'en' ? 'en-US' : 'zh-CN'; return locale === 'en' ? 'en-US' : 'zh-CN';
}; };
const resolveAdvancedSearchHeaders = () => {
const route = router && router.currentRoute ? router.currentRoute : {}
let routePath = String(route.path || '').trim()
if (!routePath) {
return {}
}
if (routePath.charAt(0) !== '/') {
routePath = '/' + routePath
}
const key = 'advanced_search_context_' + routePath
let matchedKey = key
let raw = sessionStorage.getItem(key)
if (!raw && routePath.indexOf('/modules/') === 0) {
matchedKey = 'advanced_search_context_' + routePath.substring('/modules'.length)
raw = sessionStorage.getItem(matchedKey)
}
if (!raw && routePath.indexOf('/modules/') !== 0) {
matchedKey = 'advanced_search_context_' + '/modules' + routePath
raw = sessionStorage.getItem(matchedKey)
}
if (!raw) {
return {}
}
try {
const context = JSON.parse(raw)
if (!context || !context.menuUrl || !Array.isArray(context.conditions) || context.conditions.length <= 0) {
sessionStorage.removeItem(matchedKey)
return {}
}
sessionStorage.removeItem(matchedKey)
return {
'X-Advanced-Search-Menu': String(context.menuUrl),
'X-Advanced-Search-Conditions': encodeURIComponent(JSON.stringify(context.conditions))
}
} catch (e) {
sessionStorage.removeItem(matchedKey)
return {}
}
}
var http = axios.create({ var http = axios.create({
timeout: 10000, timeout: 10000,
headers: {}, headers: {},
@ -29,6 +70,10 @@ export default {
'Accept-Language': resolveAcceptLanguage() 'Accept-Language': resolveAcceptLanguage()
} }
}; };
const advancedHeaders = resolveAdvancedSearchHeaders()
Object.keys(advancedHeaders).forEach(key => {
config.headers[key] = advancedHeaders[key]
})
let p = http.post(apiServer +url, JSON.stringify(data), config); let p = http.post(apiServer +url, JSON.stringify(data), config);
if (callback) { if (callback) {
p.then(res => { p.then(res => {
@ -52,6 +97,10 @@ export default {
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.setRequestHeader("token", Vue.cookie.get('token') ); xhr.setRequestHeader("token", Vue.cookie.get('token') );
xhr.setRequestHeader("Accept-Language", resolveAcceptLanguage()); xhr.setRequestHeader("Accept-Language", resolveAcceptLanguage());
const advancedHeaders = resolveAdvancedSearchHeaders()
Object.keys(advancedHeaders).forEach(key => {
xhr.setRequestHeader(key, advancedHeaders[key]);
})
xhr.onreadystatechange = function (e) { xhr.onreadystatechange = function (e) {
if (xhr.readyState === 4) { if (xhr.readyState === 4) {
if (xhr.status === 200) { if (xhr.status === 200) {

52
src/utils/httpRequest.js

@ -10,6 +10,46 @@ const resolveAcceptLanguage = () => {
return locale === 'en' ? 'en-US' : 'zh-CN' return locale === 'en' ? 'en-US' : 'zh-CN'
} }
const resolveAdvancedSearchHeaders = () => {
const route = router && router.currentRoute ? router.currentRoute : {}
let routePath = String(route.path || '').trim()
if (!routePath) {
return {}
}
if (routePath.charAt(0) !== '/') {
routePath = '/' + routePath
}
const key = 'advanced_search_context_' + routePath
let matchedKey = key
let raw = sessionStorage.getItem(key)
if (!raw && routePath.indexOf('/modules/') === 0) {
matchedKey = 'advanced_search_context_' + routePath.substring('/modules'.length)
raw = sessionStorage.getItem(matchedKey)
}
if (!raw && routePath.indexOf('/modules/') !== 0) {
matchedKey = 'advanced_search_context_' + '/modules' + routePath
raw = sessionStorage.getItem(matchedKey)
}
if (!raw) {
return {}
}
try {
const context = JSON.parse(raw)
if (!context || !context.menuUrl || !Array.isArray(context.conditions) || context.conditions.length <= 0) {
sessionStorage.removeItem(matchedKey)
return {}
}
sessionStorage.removeItem(matchedKey)
return {
'X-Advanced-Search-Menu': String(context.menuUrl),
'X-Advanced-Search-Conditions': encodeURIComponent(JSON.stringify(context.conditions))
}
} catch (e) {
sessionStorage.removeItem(matchedKey)
return {}
}
}
// axios.defaults.withCredentials = false // axios.defaults.withCredentials = false
const http = axios.create({ const http = axios.create({
timeout: 1000 * 300, timeout: 1000 * 300,
@ -26,6 +66,10 @@ const http = axios.create({
http.interceptors.request.use(config => { http.interceptors.request.use(config => {
config.headers['token'] = Vue.cookie.get('token') // 请求头带上token config.headers['token'] = Vue.cookie.get('token') // 请求头带上token
config.headers['Accept-Language'] = resolveAcceptLanguage() config.headers['Accept-Language'] = resolveAcceptLanguage()
const advancedHeaders = resolveAdvancedSearchHeaders()
Object.keys(advancedHeaders).forEach(key => {
config.headers[key] = advancedHeaders[key]
})
return config return config
}, error => { }, error => {
return Promise.reject(error) return Promise.reject(error)
@ -100,6 +144,10 @@ const instance = axios.create({
instance.interceptors.request.use(config => { instance.interceptors.request.use(config => {
config.headers['token'] = Vue.cookie.get('token') // 请求头带上token config.headers['token'] = Vue.cookie.get('token') // 请求头带上token
config.headers['Accept-Language'] = resolveAcceptLanguage() config.headers['Accept-Language'] = resolveAcceptLanguage()
const advancedHeaders = resolveAdvancedSearchHeaders()
Object.keys(advancedHeaders).forEach(key => {
config.headers[key] = advancedHeaders[key]
})
return config return config
}, error => { }, error => {
return Promise.reject(error) return Promise.reject(error)
@ -136,6 +184,10 @@ const instance2 = axios.create({
instance2.interceptors.request.use(config => { instance2.interceptors.request.use(config => {
config.headers['token'] = Vue.cookie.get('token') // 请求头带上token config.headers['token'] = Vue.cookie.get('token') // 请求头带上token
config.headers['Accept-Language'] = resolveAcceptLanguage() config.headers['Accept-Language'] = resolveAcceptLanguage()
const advancedHeaders = resolveAdvancedSearchHeaders()
Object.keys(advancedHeaders).forEach(key => {
config.headers[key] = advancedHeaders[key]
})
return config return config
}, error => { }, error => {
return Promise.reject(error) return Promise.reject(error)

38
src/views/modules/sift/advancedSearchCenter.vue

@ -416,6 +416,7 @@ export default {
count: countMap[key] !== undefined ? countMap[key] : null, count: countMap[key] !== undefined ? countMap[key] : null,
menuName: item.menuName || (menu ? menu.name : ('菜单' + item.menuId)), menuName: item.menuName || (menu ? menu.name : ('菜单' + item.menuId)),
conditionPreviewList: this.buildConditionPreviewList(detailMap[key]), conditionPreviewList: this.buildConditionPreviewList(detailMap[key]),
conditionDetailList: detailMap[key] || [],
schemeColor: rowColor schemeColor: rowColor
}) })
}) })
@ -446,6 +447,33 @@ export default {
menuId: row.menuId || '', menuId: row.menuId || '',
itemNo: row.itemNo || '' itemNo: row.itemNo || ''
} }
var routePath = routeRow.routePath || ''
if (!routePath && routeRow.routeName) {
try {
var resolved = this.$router.resolve({ name: routeRow.routeName, query: query })
routePath = resolved && resolved.route ? resolved.route.path : ''
} catch (e) {
routePath = ''
}
}
routePath = this.normalizeContextRoutePath(routePath)
if (routePath) {
try {
var key = 'advanced_search_context_' + routePath
var conditionRows = (row.conditionDetailList || [])
.filter(item => item && item.fieldName && item.symbol && item.formula)
.map(item => ({
fieldName: String(item.fieldName),
symbol: String(item.symbol).toLowerCase(),
formula: String(item.formula)
}))
sessionStorage.setItem(key, JSON.stringify({
menuUrl: routePath,
conditions: conditionRows
}))
} catch (e) {
}
}
if (routeRow.routeName) { if (routeRow.routeName) {
this.$router.push({ name: routeRow.routeName, query: query }) this.$router.push({ name: routeRow.routeName, query: query })
return return
@ -461,6 +489,16 @@ export default {
this.routeResolvingKey = '' this.routeResolvingKey = ''
} }
}, },
normalizeContextRoutePath (path) {
var p = String(path || '').trim()
if (!p) {
return ''
}
if (p.charAt(0) !== '/') {
p = '/' + p
}
return p
},
openCreateDialog () { openCreateDialog () {
this.dialogMode = 'create' this.dialogMode = 'create'
this.settingDialogVisible = true this.settingDialogVisible = true

Loading…
Cancel
Save