You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
3.8 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. import { atom } from 'jotai'
  2. import { IApiResult, IPage } from '@/global'
  3. import { atomWithMutation, atomWithQuery, queryClientAtom } from 'jotai-tanstack-query'
  4. import { message } from 'antd'
  5. import { t } from 'i18next'
  6. import websitesServ from '@/service/websites'
  7. import { IWebsiteDnsRecords } from '@/types/website/record'
  8. const i18nPrifx = 'websites.record'
  9. type SearchParams = IPage & {
  10. key?: string
  11. [key: string]: any
  12. }
  13. export const explainTypes = [
  14. {
  15. label: 'A',
  16. value: 'A'
  17. }, {
  18. label: 'CNAME',
  19. value: 'CNAME'
  20. }, {
  21. label: 'AAAA',
  22. value: 'AAAA'
  23. }, {
  24. label: 'NS',
  25. value: 'NS'
  26. }, {
  27. label: 'MX',
  28. value: 'MX'
  29. }, {
  30. label: 'SRV',
  31. value: 'SRV'
  32. }, {
  33. label: 'TXT',
  34. value: 'TXT'
  35. }, {
  36. label: 'CAA',
  37. value: 'CAA'
  38. }, {
  39. label: 'PTR',
  40. value: 'PTR'
  41. }, {
  42. label: t('websites.record.redirect_url'),
  43. value: 'REDIRECT_URL'
  44. }, {
  45. label: t('websites.record.forward_url'),
  46. value: 'FORWARD_URL'
  47. } ]
  48. export const ttlOptions = [
  49. {
  50. label: t(`${i18nPrifx}.ttl.1`, '自动'),
  51. value: 1
  52. },
  53. {
  54. label: t(`${i18nPrifx}.ttl.10`, '10分钟'),
  55. value: 10
  56. },
  57. {
  58. label: t(`${i18nPrifx}.ttl.30`, '30分钟'),
  59. value: 30
  60. },
  61. {
  62. label: t(`${i18nPrifx}.ttl.60`, '1小时'),
  63. value: 60
  64. },
  65. {
  66. label: t(`${i18nPrifx}.ttl.60`, '12小时'),
  67. value: 60 * 12
  68. },
  69. {
  70. label: t(`${i18nPrifx}.ttl.60`, '1天'),
  71. value: 60 * 24
  72. },
  73. ]
  74. export const websiteDnsRecordsIdAtom = atom(0)
  75. export const websiteDnsRecordsIdsAtom = atom<number[]>([])
  76. export const websiteDnsRecordsAtom = atom<IWebsiteDnsRecords>(undefined as unknown as IWebsiteDnsRecords)
  77. export const websiteDnsRecordsSearchAtom = atom<SearchParams>({
  78. // key: '',
  79. pageSize: 10,
  80. page: 1,
  81. } as SearchParams)
  82. export const websiteDnsDomainIdAtom = atom<number>(0)
  83. export const websiteDnsRecordssAtom = atomWithQuery((get) => {
  84. return {
  85. queryKey: [ 'websiteDnsRecordss', get(websiteDnsRecordsSearchAtom), get(websiteDnsDomainIdAtom) ],
  86. queryFn: async ({ queryKey: [ , params, id ] }) => {
  87. if (!id) {
  88. return {
  89. data: {
  90. rows: [],
  91. total: 0
  92. }
  93. }
  94. }
  95. return await websitesServ.record.list({ ...(params as any), domain_id: id })
  96. },
  97. select: res => {
  98. const data = res.data
  99. data.rows = data.rows?.map(row => {
  100. return {
  101. ...row,
  102. //status: convertToBool(row.status)
  103. }
  104. })
  105. return data
  106. }
  107. }
  108. })
  109. //saveOrUpdateAtom
  110. export const saveOrUpdateWebsiteDnsRecordsAtom = atomWithMutation<IApiResult, IWebsiteDnsRecords>((get) => {
  111. return {
  112. mutationKey: [ 'updateWebsiteDnsRecords' ],
  113. mutationFn: async (data) => {
  114. //data.status = data.status ? '1' : '0'
  115. if (data.id === 0) {
  116. return await websitesServ.record.add(data)
  117. }
  118. return await websitesServ.record.update(data)
  119. },
  120. onSuccess: (res) => {
  121. const isAdd = !!res.data?.id
  122. message.success(t(isAdd ? 'message.saveSuccess' : 'message.editSuccess', '保存成功'))
  123. //更新列表
  124. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  125. // @ts-ignore fix
  126. get(queryClientAtom).invalidateQueries({ queryKey: [ 'websiteDnsRecordss', get(websiteDnsRecordsSearchAtom) ] })
  127. return res
  128. }
  129. }
  130. })
  131. export const deleteWebsiteDnsRecordsAtom = atomWithMutation((get) => {
  132. return {
  133. mutationKey: [ 'deleteWebsiteDnsRecords' ],
  134. mutationFn: async (ids: number[]) => {
  135. return await websitesServ.record.batchDelete(ids ?? get(websiteDnsRecordsIdsAtom))
  136. },
  137. onSuccess: (res) => {
  138. message.success('message.deleteSuccess')
  139. //更新列表
  140. get(queryClientAtom).invalidateQueries({ queryKey: [ 'websiteDnsRecordss', get(websiteDnsRecordsSearchAtom) ] })
  141. return res
  142. }
  143. }
  144. })