1. /*
    2. * @Author: yourcandy
    3. * @Date: 2021-07-14 23:34:34
    4. * @LastEditTime: 2021-07-14 23:34:34
    5. * @LastEditors: wpf
    6. * @Description:局部混入 字典公共请求封装 (主要针对后端只支持传入单个字典的封装)
    7. * @FilePath: \ztocwst-capacitycenter-webc:\Users\Admin\Desktop\initDict.js
    8. */
    9. import { getDictRequest } from '@/api/dict'
    10. export default {
    11. data() {
    12. return {
    13. dicts: [],
    14. dictMap: {},
    15. }
    16. },
    17. methods: {
    18. /**
    19. * 请求单个字典
    20. * @description :
    21. * @param {String} name 要请求的字典名
    22. * @return {Array}
    23. */
    24. async getDict(name) {
    25. return new Promise((resolve, reject) => {
    26. getDictRequest(name)
    27. .then((res) => {
    28. this.dicts = res
    29. resolve(res)
    30. })
    31. .catch((err) => {
    32. reject(err)
    33. })
    34. })
    35. },
    36. /**
    37. * 请求多个字典
    38. * @description: 多个字典查询时使用逗号拼接, 如: this.getDictMap('user_status,job_status') 使用加载出来的字典:dictMap.[字典名称] 如:dictMap.user_status
    39. * @param {*} names
    40. * @return {*}
    41. */
    42. async getDictMap(names) {
    43. // 优先放入到dictMap中,避免页面加载时 undefined
    44. const arr = names.split(',')
    45. let getDictArray = []
    46. for (let i = 0; i < arr.length; i++) {
    47. this.dictMap[arr[i]] = []
    48. getDictArray.push(this.getDict(arr[i]))
    49. }
    50. return new Promise((resolve, reject) => {
    51. Promise.all(getDictArray)
    52. .then((result) => {
    53. for (let i = 0; i < arr.length; i++) {
    54. this.dictMap[arr[i]] = result[i]
    55. let obj = {}
    56. for (let item of result[i]) {
    57. obj[item.dictType] = item.dictName
    58. }
    59. }
    60. resolve(this.dictMap)
    61. })
    62. .catch((err) => {
    63. reject(err)
    64. })
    65. })
    66. },
    67. },
    68. }
    1. /*
    2. * @Author: yourcandy
    3. * @Date: 2021-07-14 23:34:34
    4. * @LastEditTime: 2021-07-14 23:34:34
    5. * @LastEditors: wpf
    6. * @Description:局部混入 字典公共请求封装 (主要针对后端支持传入数组的多个字典的封装)
    7. * @FilePath: \ztocwst-capacitycenter-webc:\Users\Admin\Desktop\initDict.js
    8. */
    9. export default {
    10. data() {
    11. return {
    12. dictMap: {}
    13. };
    14. },
    15. methods: {
    16. // 初始化字典
    17. intiDict(dicType) {
    18. return new Promise((resolve, reject) => {
    19. this.Http('xxxxxxxxxxurl', {
    20. ignoreRepeat: true,
    21. dicType
    22. }).then((res) => {
    23. this.dictMap = this.groupBy(res, v => v.dicType);
    24. resolve(res);
    25. });
    26. });
    27. },
    28. // 字典分组
    29. groupBy(array, f) {
    30. let obj = {};
    31. array.forEach((v) => {
    32. let key = f(v);
    33. if (obj.hasOwnProperty(key)) {
    34. obj[key].push(v);
    35. } else {
    36. obj[key] = [v];
    37. }
    38. });
    39. return obj;
    40. }
    41. }
    42. };
    1. /*
    2. * @Author: yourcandy
    3. * @Date: 2021-07-14 23:34:34
    4. * @LastEditTime: 2021-07-14 23:34:34
    5. * @LastEditors: wpf
    6. * @Description:局部混入 字典公共请求封装 (主要针对后端支持传入数组的多个字典的封装)
    7. * @FilePath: \ztocwst-capacitycenter-webc:\Users\Admin\Desktop\initDict.js
    8. */
    9. export default {
    10. data() {
    11. return {
    12. dicData: {}
    13. };
    14. },
    15. methods: {
    16. /**
    17. * 查询字典
    18. * @param {Array} dicTypes
    19. */
    20. getDict(dicTypes) {
    21. // 先赋值默认值
    22. this.dicData = dicTypes.reduce((total, item) => {
    23. total[item] = [];
    24. return total;
    25. }, {});
    26. this.Http('xxxxurl', {
    27. ignoreRepeat: true,
    28. dicTypes
    29. }).then(res => {
    30. this.dicData = res.result.reduce((total, item) => {
    31. if (total[item.dicType]) {
    32. total[item.dicType].push(item);
    33. } else {
    34. total[item.dicType] = [item];
    35. }
    36. return total;
    37. }, {});
    38. console.log(this.dicData);
    39. });
    40. }
    41. }
    42. };