Parcourir la source

【升级】前端升级字典缓存,增加统一翻译filter,缓存模式

俞宝山 il y a 4 ans
Parent
commit
e23b192000

+ 15 - 0
_web/src/api/modular/system/dictManage.js

@ -69,3 +69,18 @@ export function sysDictTypeDropDown (parameter) {
    params: parameter
  })
}
/**
 * 获取所有字典,启动时加入缓存使用
 *
 * @author yubaoshan
 * @date 2020/6/10 00:10
 */
export function sysDictTypeTree (parameter) {
  return axios({
    url: '/sysDictType/tree',
    method: 'get',
    params: parameter
  })
}

+ 21 - 1
_web/src/store/modules/user.js

@ -1,8 +1,9 @@
import Vue from 'vue'
import { login, getLoginUser, logout } from '@/api/modular/system/loginManage'
import { sysDictTypeTree } from '@/api/modular/system/dictManage'
import { sysMenuChange } from '@/api/modular/system/menuManage'
import { sysUserUpdatePwd } from '@/api/modular/system/userManage'
import { ACCESS_TOKEN, ALL_APPS_MENU } from '@/store/mutation-types'
import { ACCESS_TOKEN, ALL_APPS_MENU, DICT_TYPE_TREE_DATA } from '@/store/mutation-types'
import { welcome } from '@/utils/util'
import store from '../index'
@ -101,6 +102,25 @@ const user = {
          commit('SET_ADMINTYPE', '')
          Vue.ls.remove(ACCESS_TOKEN)
          Vue.ls.remove(ALL_APPS_MENU)
          Vue.ls.remove(DICT_TYPE_TREE_DATA)
        })
      })
    },
    // 加载所有字典数据
    dictTypeData () {
      return new Promise((resolve, reject) => {
        sysDictTypeTree().then((data) => {
          if (data.success) {
            const result = data.data
            Vue.ls.set(DICT_TYPE_TREE_DATA, result)
            resolve()
          } else {
            // eslint-disable-next-line no-undef
            reject(new Error(data.message))
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

+ 2 - 1
_web/src/store/mutation-types.js

@ -1,6 +1,6 @@
export const ACCESS_TOKEN = 'Access-Token'
export const SIDEBAR_TYPE = 'SIDEBAR_TYPE'
export const ALL_APPS_MENU = []
export const ALL_APPS_MENU = 'ALL_APPS_MENU'
export const DEFAULT_THEME = 'DEFAULT_THEME'
export const DEFAULT_LAYOUT_MODE = 'DEFAULT_LAYOUT_MODE'
export const DEFAULT_COLOR = 'DEFAULT_COLOR'
@ -10,6 +10,7 @@ export const DEFAULT_FIXED_SIDEMENU = 'DEFAULT_FIXED_SIDEMENU'
export const DEFAULT_FIXED_HEADER_HIDDEN = 'DEFAULT_FIXED_HEADER_HIDDEN'
export const DEFAULT_CONTENT_WIDTH_TYPE = 'DEFAULT_CONTENT_WIDTH_TYPE'
export const DEFAULT_MULTI_TAB = 'DEFAULT_MULTI_TAB'
export const DICT_TYPE_TREE_DATA = 'DICT_TYPE_TREE_DATA'
export const CONTENT_WIDTH_TYPE = {
  Fluid: 'Fluid',

+ 51 - 0
_web/src/utils/filter.js

@ -1,4 +1,5 @@
import Vue from 'vue'
import { DICT_TYPE_TREE_DATA } from '@/store/mutation-types'
import moment from 'moment'
import 'moment/locale/zh-cn'
moment.locale('zh-cn')
@ -18,6 +19,13 @@ Vue.filter('dayjs', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
Vue.filter('moment', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr).format(pattern)
})
/**
 * 金额格式化 ,使用方法:{{ val | Fmoney }}
 *
 * @author yubaoshan
 * @date 2020-9-15 15:02:20
 */
Vue.filter('Fmoney', function (val) {
  // eslint-disable-next-line no-useless-escape
  val = val.toString().replace(/\$|\,/g, '')
@ -38,3 +46,46 @@ Vue.filter('Fmoney', function (val) {
  }
  return (((sign) ? '' : '-') + val + '.' + cents)
})
/**
 * 翻译使用方法,直接返回翻译后的name {{ code | dictType(value) }}
 *
 * @author yubaoshan
 * @date 2020-9-15 15:02:20
 */
Vue.filter('dictType', function (code, value) {
  const dictTypeTree = Vue.ls.get(DICT_TYPE_TREE_DATA)
  if (dictTypeTree === undefined) {
    return '需重新登录'
  }
  // eslint-disable-next-line eqeqeq
  const tree = dictTypeTree.filter(item => item.code == code)[0].children
  if (tree === undefined || tree.length === 0) {
    return '无此字典'
  }
  // eslint-disable-next-line eqeqeq
  const values = tree.filter(item => item.code == value)
  if (values.length === undefined || values.length === 0) {
    return '无此字典'
  }
  return values[0].name
})
/**
 * 获取某个code下字典的列表,多用于字典下拉框,使用方法:{{ code | dictData }}
 *
 * @author yubaoshan
 * @date 2020-9-19 22:40:22
 */
Vue.filter('dictData', function (code) {
  const dictTypeTree = Vue.ls.get(DICT_TYPE_TREE_DATA)
  if (dictTypeTree === undefined) {
    return []
  }
  // eslint-disable-next-line eqeqeq
  const tree = dictTypeTree.filter(item => item.code == code)[0].children
  if (tree === undefined) {
    return []
  }
  return tree
})

+ 3 - 1
_web/src/views/userLoginReg/Login.vue

@ -178,7 +178,7 @@ export default {
    // this.requiredTwoStepCaptcha = true
  },
  methods: {
    ...mapActions(['Login', 'Logout']),
    ...mapActions(['Login', 'Logout', 'dictTypeData']),
    // handler
    handleUsernameOrEmail (rule, value, callback) {
      const { state } = this
@ -278,6 +278,8 @@ export default {
    loginSuccess (res) {
      this.$router.push({ path: '/' })
      this.isLoginError = false
      // 加载字典所有字典到缓存中
      this.dictTypeData().then((res) => { })
    },
    requestFailed (err) {
      this.accountLoginErrMsg = err