dict.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * 数据字典工具类
  3. */
  4. import request from "@/utils/request";
  5. import { getStoreObject, setStoreObject } from './common'
  6. const DICT_NAME = '_dict';
  7. export const useDictStoreWithOut = () => {
  8. const listSimpleDictData = async () => {
  9. const result = await request.get('/admin-api/system/dict-data/list-all-simple')
  10. return result.data;
  11. }
  12. const dictObj = getStoreObject(DICT_NAME);
  13. if (!dictObj) {
  14. listSimpleDictData().then((resultData) => {
  15. if (resultData && resultData?.length > 0) {
  16. setStoreObject(DICT_NAME, resultData);
  17. }
  18. });
  19. }
  20. }
  21. /**
  22. * 获取 dictType 对应的数据字典数组
  23. *
  24. * @param dictType 数据类型
  25. * @returns {*|Array} 数据字典数组
  26. */
  27. export interface DictDataType {
  28. dictType: string
  29. label: string
  30. value: string | number | boolean
  31. cssClass: string
  32. }
  33. export const getDictOptions = (dictType: string, option?: {
  34. dictType?: string
  35. label?: string
  36. value?: string
  37. cssClass?: string
  38. }) => {
  39. const dictStore = getStoreObject(DICT_NAME);
  40. const nDicts: any[] = []
  41. dictStore.forEach((dict: DictDataType) => {
  42. if (dict.dictType === dictType) {
  43. nDicts.push({
  44. [option?.dictType ?? 'dictType']: dict.dictType,
  45. [option?.label ?? 'label']: dict.label,
  46. [option?.value ?? 'value']: dict.value,
  47. [option?.cssClass ?? 'cssClass']: dict.cssClass,
  48. })
  49. }
  50. })
  51. return nDicts;
  52. }
  53. export const getIntDictOptions = (dictType: string) => {
  54. const dictOption: DictDataType[] = []
  55. const dictOptions: DictDataType[] = getDictOptions(dictType)
  56. dictOptions.forEach((dict: DictDataType) => {
  57. dictOption.push({
  58. ...dict,
  59. value: parseInt(dict.value + '')
  60. })
  61. })
  62. return dictOption
  63. }
  64. export const getStrDictOptions = (dictType: string) => {
  65. const dictOption: DictDataType[] = []
  66. const dictOptions: DictDataType[] = getDictOptions(dictType)
  67. dictOptions.forEach((dict: DictDataType) => {
  68. dictOption.push({
  69. ...dict,
  70. value: dict.value + ''
  71. })
  72. })
  73. return dictOption
  74. }
  75. export const getBoolDictOptions = (dictType: string) => {
  76. const dictOption: DictDataType[] = []
  77. const dictOptions: DictDataType[] = getDictOptions(dictType)
  78. dictOptions.forEach((dict: DictDataType) => {
  79. dictOption.push({
  80. ...dict,
  81. value: dict.value + '' === 'true'
  82. })
  83. })
  84. return dictOption
  85. }
  86. /**
  87. * 获取指定字典类型的指定值对应的字典对象
  88. * @param dictType 字典类型
  89. * @param value 字典值
  90. * @return DictDataType 字典对象
  91. */
  92. export const getDictObj = (dictType: string, value: any): DictDataType | undefined => {
  93. const dictOptions: DictDataType[] = getDictOptions(dictType)
  94. for (const dict of dictOptions) {
  95. if (dict.value === value + '') {
  96. return dict
  97. }
  98. }
  99. }
  100. /**
  101. * 获得字典数据的文本展示
  102. *
  103. * @param dictType 字典类型
  104. * @param value 字典数据的值
  105. * @return 字典名称
  106. */
  107. export const getDictLabel = (dictType: string, value: any): string => {
  108. const dictOptions: DictDataType[] = getDictOptions(dictType)
  109. const dictLabel = ref('')
  110. dictOptions.forEach((dict: DictDataType) => {
  111. if (dict.value === value + '') {
  112. dictLabel.value = dict.label
  113. }
  114. })
  115. return dictLabel.value
  116. }
  117. export enum DICT_TYPE {
  118. USER_TYPE = 'user_type',
  119. COMMON_STATUS = 'common_status',
  120. COMMON_STATE = 'common_state',
  121. SYSTEM_TENANT_PACKAGE_ID = 'system_tenant_package_id',
  122. TERMINAL = 'terminal', // 终端
  123. // ========== SYSTEM 模块 ==========
  124. SYSTEM_USER_SEX = 'system_user_sex',
  125. SYSTEM_MENU_TYPE = 'system_menu_type',
  126. SYSTEM_ROLE_TYPE = 'system_role_type',
  127. SYSTEM_DATA_SCOPE = 'system_data_scope',
  128. SYSTEM_NOTICE_TYPE = 'system_notice_type',
  129. SYSTEM_OPERATE_TYPE = 'system_operate_type',
  130. SYSTEM_LOGIN_TYPE = 'system_login_type',
  131. SYSTEM_LOGIN_RESULT = 'system_login_result',
  132. SYSTEM_SMS_CHANNEL_CODE = 'system_sms_channel_code',
  133. SYSTEM_SMS_TEMPLATE_TYPE = 'system_sms_template_type',
  134. SYSTEM_SMS_SEND_STATUS = 'system_sms_send_status',
  135. SYSTEM_SMS_RECEIVE_STATUS = 'system_sms_receive_status',
  136. SYSTEM_ERROR_CODE_TYPE = 'system_error_code_type',
  137. SYSTEM_OAUTH2_GRANT_TYPE = 'system_oauth2_grant_type',
  138. SYSTEM_MAIL_SEND_STATUS = 'system_mail_send_status',
  139. SYSTEM_NOTIFY_TEMPLATE_TYPE = 'system_notify_template_type',
  140. // ========== INFRA 模块 ==========
  141. INFRA_BOOLEAN_STRING = 'infra_boolean_string',
  142. INFRA_JOB_STATUS = 'infra_job_status',
  143. INFRA_JOB_LOG_STATUS = 'infra_job_log_status',
  144. INFRA_API_ERROR_LOG_PROCESS_STATUS = 'infra_api_error_log_process_status',
  145. INFRA_CONFIG_TYPE = 'infra_config_type',
  146. INFRA_CODEGEN_TEMPLATE_TYPE = 'infra_codegen_template_type',
  147. INFRA_CODEGEN_FRONT_TYPE = 'infra_codegen_front_type',
  148. INFRA_CODEGEN_SCENE = 'infra_codegen_scene',
  149. INFRA_FILE_STORAGE = 'infra_file_storage',
  150. // ========== ADM 模块 ==========
  151. ADM_ATTENDANCE_STATUS = 'adm_attendance_status',
  152. // ========== BUSINESS 模块 ==========
  153. WF_COMMON_COST_TYPE = 'WF_COMMON_COST_TYPE',
  154. WF_SEAL_TYPE = 'WF_SEAL_TYPE',
  155. WF_SEAL_NAME = 'WF_SEAL_NAME',
  156. WF_TRAVEL_COST_OTHER_TYPE = 'WF_TRAVEL_COST_OTHER_TYPE',
  157. WF_TRAVEL_COST_VEHICLE_TYPE = 'WF_TRAVEL_COST_VEHICLE_TYPE',
  158. FINANCE_STATUS = 'finance_status',
  159. PROJECT_STATUS = 'project_status',
  160. CONTRACT_COST_STATUS = 'CONTRACT_COST_STATUS',
  161. CONTRACT_SIGN_STATUS = 'contract_is_sign',
  162. CONTRACT_SIGN_WAY = 'contract_sign_way',
  163. CONTRACT_MAIN_TYPE = 'contract_main_type',
  164. CONTRACT_SECOND_TYPE = 'contract_second_type',
  165. SUBCONTRACT_SHARE_WAY='subcontract_share_way',
  166. PROJECT_HY = 'project_hy',
  167. POST_TYPE = 'post_type',
  168. ABILITY_LEVEL = 'ABILITY_LEVEL',
  169. INVOICE_OUT_STATUS = 'INVOICE_OUT_STATUS',
  170. INVOICE_TYPE = 'invoice_type',
  171. WF_LEAVE_TYPE = 'WF_LEAVE_TYPE',
  172. RECRUIT_TYPE = 'RECRUIT_TYPE',
  173. SEX_TYPE = 'sex_type',
  174. NATION_TYPE = 'nation_type',
  175. HY_TYPE = 'hy_type',
  176. HK_TYPE = 'hk_type',
  177. POLITY_TYPE = 'polity_type',
  178. XL_TYPE = 'xl_type',
  179. }