globalMixins.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { getDict } from '@/api/toConsult.js'
  2. import dayjs from 'dayjs'
  3. import Dicts from '../utils/dicts'
  4. import {mapActions} from 'vuex'
  5. export default {
  6. data() {
  7. return {
  8. }
  9. },
  10. created() {
  11. /** 组件中设置dicts数组,会将字典存储在vuex中*/
  12. if(this.dicts && this.dicts.length > 0){
  13. this.setDicts()
  14. // this.dicts.forEach(item=>{
  15. // this.getDictHandler(item,(res)=>{
  16. // this[item] = res
  17. // })
  18. // })
  19. }
  20. },
  21. methods: {
  22. ...mapActions(['setDict']),
  23. setDicts(){
  24. this.dicts.forEach(item=>{
  25. this.setDict(item)
  26. })
  27. },
  28. /** 获取字典值
  29. * 组件页面中需要设置:mapGetters(['dictionary']']),
  30. * */
  31. getDictItem(key){
  32. let item = this.dictionary?.find((v)=>{
  33. return v.key === key
  34. })
  35. return item?.value
  36. },
  37. //时间范围模板:2020-01-01~2020-01-02
  38. rangDate(start,end,a){
  39. if(!start || !end) return '暂无';
  40. !a && (a = '~')
  41. return `${start}${a}${end}`;
  42. },
  43. //根据字典类型获取字典值
  44. getDictHandler(dictType,callBack) {
  45. getDict( dictType ).then(res => {
  46. let { data } = res
  47. callBack(data)
  48. })
  49. },
  50. }
  51. }