globalMixins.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { getDict } from '@/api/toConsult.js'
  2. import dayjs from 'dayjs'
  3. import Dicts from '../utils/dicts'
  4. export default {
  5. data() {
  6. return {
  7. //字典['dict_type']
  8. dicts:[]
  9. }
  10. },
  11. created() {
  12. /** 组件中设置dictType,即可获取相应的value*/
  13. if(this.dicts && this.dicts.length > 0){
  14. this.dicts.forEach(item=>{
  15. this.getDictHandler(item,(res)=>{
  16. this[item] = res
  17. })
  18. })
  19. }
  20. },
  21. computed:{
  22. },
  23. methods: {
  24. //时间范围模板:2020-01-01~2020-01-02
  25. rangDate(start,end,a){
  26. if(!start || !end) return '暂无';
  27. !a && (a = '~')
  28. return `${start}${a}${end}`;
  29. },
  30. //根据字典类型获取字典值
  31. getDictHandler(dictType,callBack) {
  32. getDict( dictType ).then(res => {
  33. let { code, data, msg } = res
  34. if (code == 200) {
  35. callBack(data)
  36. }
  37. })
  38. },
  39. //根据字典类型获取字典值,返回字典label
  40. getDicts(s,dict){
  41. console.log(this[dict],dict);
  42. //判断内存中是否有该字典,没有就去获取
  43. if( !this[dict]){
  44. getDict( dict ).then(res => {
  45. let { code, data } = res;
  46. if (code == 200) {
  47. this[dict] = data;
  48. //return this[dict].find(v=> s == v.dictValue).dictLabel;
  49. }
  50. })
  51. }else {
  52. return this[dict].find(v=> s == v.dictValue).dictLabel;
  53. }
  54. },
  55. }
  56. }