main.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { StoreOptions } from 'vuex'
  2. import Vue from 'vue'
  3. import {
  4. getHomeHot,
  5. getHomeActivity,
  6. getCashOutInfo,
  7. submitCashOutInfo,
  8. getDetails
  9. } from '@/api/main'
  10. interface InterfaceStore<S> extends StoreOptions<S> {
  11. namespaced?: boolean;
  12. }
  13. type APIStructure = Record<string, any>;
  14. type modulesOption = InterfaceStore<any>;
  15. function recoveryPageData (key: string, defaultValues = {}) {
  16. return sessionStorage.getItem(key) ? JSON.parse(sessionStorage.getItem(key) || '') : defaultValues
  17. }
  18. function formatData (v: APIStructure) {
  19. return {
  20. img: v?.docImg,
  21. type: v?.docFileType,
  22. id: v.docId,
  23. title: v?.docName || v?.docTitle,
  24. money: v.price,
  25. size: v?.docFileSize,
  26. page: v?.docPageSize,
  27. down: v?.downTimes,
  28. contribution: v?.sourceUserId
  29. }
  30. }
  31. const modulesOption: modulesOption = {
  32. namespaced: true,
  33. state: {
  34. // 提现页面数据缓存(用于跳出页面返回时恢复)
  35. cashOutInfo: recoveryPageData('partner-cashout'),
  36. cashOutSuccess: recoveryPageData('partner-cashout-success'),
  37. homePageData: recoveryPageData('jy-docs-home-page')
  38. },
  39. mutations: {
  40. // 保存提现页面数据
  41. saveCashOutInfo (state, data) {
  42. for (const key in data) {
  43. state.cashOutInfo[key] = data[key]
  44. }
  45. sessionStorage.setItem('partner-cashout', JSON.stringify(data))
  46. },
  47. // 清除提现页面数据
  48. clearCashOutInfo (state) {
  49. state.cashOutInfo = {}
  50. sessionStorage.setItem('partner-cashout', JSON.stringify({}))
  51. },
  52. // 保存提现成功数据
  53. saveCashOutSuccessInfo (state, data) {
  54. for (const key in data) {
  55. state.cashOutSuccess[key] = data[key]
  56. }
  57. sessionStorage.setItem('partner-cashout-success', JSON.stringify(data))
  58. },
  59. // 清除提现成功数据
  60. clearCashOutSuccessInfo (state) {
  61. state.cashOutSuccess = {}
  62. sessionStorage.setItem('partner-cashout-success', JSON.stringify({}))
  63. },
  64. // 保存首页数据
  65. saveHomeData (state, data) {
  66. for (const key in data) {
  67. Vue.set(state.homePageData, key, data[key])
  68. }
  69. sessionStorage.setItem('jy-docs-home-page', JSON.stringify(state.homePageData))
  70. }
  71. },
  72. actions: {
  73. // 首页数据
  74. async getHome (state) {
  75. getHomeHot({ sign: 'new', num: 5 }).then((res: APIStructure) => {
  76. if (!res.data.error_msg.length) {
  77. state.commit('saveHomeData', {
  78. new: res.data.data.map((v: APIStructure) => formatData(v))
  79. })
  80. }
  81. })
  82. getHomeHot({ sign: 'hot', num: 5 }).then((res: APIStructure) => {
  83. if (!res.data.error_msg.length) {
  84. state.commit('saveHomeData', {
  85. hot: res.data.data.map((v: APIStructure) => formatData(v))
  86. })
  87. }
  88. })
  89. getHomeActivity({ code: 3, size: 3, num: 1 }).then((res: APIStructure) => {
  90. if (!res.data.error_msg.length) {
  91. state.commit('saveHomeData', {
  92. keep: res.data.data.map((v: APIStructure) => formatData(v))
  93. })
  94. }
  95. })
  96. },
  97. // 提现查询
  98. async getCashOutInfo (state, data) {
  99. try {
  100. const res = await getCashOutInfo(data)
  101. return res.data
  102. } catch (error) {}
  103. },
  104. // 提现提交
  105. async submitCashOutInfo (state, data) {
  106. try {
  107. const res = await submitCashOutInfo(data)
  108. return res.data
  109. } catch (error) {}
  110. },
  111. // 详情
  112. async getDetails (state, data) {
  113. try {
  114. const res = await getDetails(data)
  115. console.log(res)
  116. return res.data
  117. } catch (error) {}
  118. }
  119. },
  120. getters: {}
  121. }
  122. export default modulesOption