index.mock.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { SUCCESS_CODE } from '@/constants'
  2. import { toAnyString } from '@/utils'
  3. const timeout = 500
  4. interface ChannelItem {
  5. id: string
  6. name: string
  7. code: string
  8. description: string
  9. status: boolean
  10. createTime: string
  11. }
  12. let channelList: ChannelItem[] = [
  13. {
  14. id: toAnyString(),
  15. name: '微信',
  16. code: 'wx',
  17. description: '微信小程序渠道',
  18. status: true,
  19. createTime: '2024-01-01 10:00:00'
  20. },
  21. {
  22. id: toAnyString(),
  23. name: '抖音',
  24. code: 'douyin',
  25. description: '抖音小程序渠道',
  26. status: true,
  27. createTime: '2024-01-02 10:00:00'
  28. },
  29. {
  30. id: toAnyString(),
  31. name: '支付宝',
  32. code: 'alipay',
  33. description: '支付宝小程序渠道',
  34. status: false,
  35. createTime: '2024-01-03 10:00:00'
  36. }
  37. ]
  38. const listHandler = ({ query }) => {
  39. const { name, pageIndex = 1, pageSize = 10 } = query
  40. const filtered = channelList.filter((item) => {
  41. if (name && !item.name.includes(name) && !item.code.includes(name)) return false
  42. return true
  43. })
  44. const start = (pageIndex - 1) * pageSize
  45. const list = filtered.slice(start, start + pageSize)
  46. return { code: SUCCESS_CODE, data: { total: filtered.length, list } }
  47. }
  48. const saveHandler = ({ body }) => {
  49. const item: ChannelItem = {
  50. id: toAnyString(),
  51. name: body.name,
  52. code: body.code,
  53. description: body.description || '',
  54. status: body.status !== false,
  55. createTime: new Date().toLocaleString('zh-CN')
  56. }
  57. channelList.unshift(item)
  58. return { code: SUCCESS_CODE, data: item.id }
  59. }
  60. const updateHandler = ({ body }) => {
  61. const idx = channelList.findIndex((item) => item.id === body.id)
  62. if (idx >= 0) channelList[idx] = { ...channelList[idx], ...body }
  63. return { code: SUCCESS_CODE, data: 'success' }
  64. }
  65. const deleteHandler = ({ body }) => {
  66. const { ids } = body
  67. if (!ids?.length) return { code: 500, message: '请选择需要删除的数据' }
  68. channelList = channelList.filter((item) => !ids.includes(item.id))
  69. return { code: SUCCESS_CODE, data: 'success' }
  70. }
  71. export default [
  72. // 渠道走 Go 后端,不 mock
  73. ]