main.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { StoreOptions } from 'vuex'
  2. import Vue from 'vue'
  3. import {
  4. getHomeHot,
  5. getHomeActivity,
  6. getCashOutInfo,
  7. submitCashOutInfo,
  8. getDetails,
  9. getRemove,
  10. getAdd,
  11. getCoin,
  12. getDocPay
  13. } from '@/api/main'
  14. interface InterfaceStore<S> extends StoreOptions<S> {
  15. namespaced?: boolean;
  16. }
  17. type APIStructure = Record<string, any>;
  18. type modulesOption = InterfaceStore<any>;
  19. function recoveryPageData (key: string, defaultValues = {}) {
  20. return sessionStorage.getItem(key) ? JSON.parse(sessionStorage.getItem(key) || '') : defaultValues
  21. }
  22. function checkType (type: string | number) {
  23. let typeStr = ''
  24. switch (type) {
  25. case 1: {
  26. typeStr = 'word'
  27. break
  28. }
  29. case 2: {
  30. typeStr = 'pdf'
  31. break
  32. }
  33. case 3: {
  34. typeStr = 'excel'
  35. break
  36. }
  37. case 4: {
  38. typeStr = 'ppt'
  39. break
  40. }
  41. case 'doc': {
  42. typeStr = 'word'
  43. break
  44. }
  45. case 'xls': {
  46. typeStr = 'excel'
  47. break
  48. }
  49. }
  50. return typeStr
  51. }
  52. function formatData (v: APIStructure) {
  53. return {
  54. img: v?.docImg,
  55. type: checkType(v?.docFileType),
  56. id: v.docId,
  57. title: v?.docName || v?.docTitle,
  58. money: v.price,
  59. size: v?.docFileSize,
  60. page: v?.docPageSize,
  61. down: v?.downTimes,
  62. contribution: v?.sourceUserId
  63. }
  64. }
  65. const modulesOption: modulesOption = {
  66. namespaced: true,
  67. state: {
  68. // 提现页面数据缓存(用于跳出页面返回时恢复)
  69. cashOutInfo: recoveryPageData('partner-cashout'),
  70. cashOutSuccess: recoveryPageData('partner-cashout-success'),
  71. homePageData: recoveryPageData('jy-docs-home-page')
  72. },
  73. mutations: {
  74. // 保存提现页面数据
  75. saveCashOutInfo (state, data) {
  76. for (const key in data) {
  77. state.cashOutInfo[key] = data[key]
  78. }
  79. sessionStorage.setItem('partner-cashout', JSON.stringify(data))
  80. },
  81. // 清除提现页面数据
  82. clearCashOutInfo (state) {
  83. state.cashOutInfo = {}
  84. sessionStorage.setItem('partner-cashout', JSON.stringify({}))
  85. },
  86. // 保存提现成功数据
  87. saveCashOutSuccessInfo (state, data) {
  88. for (const key in data) {
  89. state.cashOutSuccess[key] = data[key]
  90. }
  91. sessionStorage.setItem('partner-cashout-success', JSON.stringify(data))
  92. },
  93. // 清除提现成功数据
  94. clearCashOutSuccessInfo (state) {
  95. state.cashOutSuccess = {}
  96. sessionStorage.setItem('partner-cashout-success', JSON.stringify({}))
  97. },
  98. // 保存首页数据
  99. saveHomeData (state, data) {
  100. for (const key in data) {
  101. Vue.set(state.homePageData, key, data[key])
  102. }
  103. sessionStorage.setItem('jy-docs-home-page', JSON.stringify(state.homePageData))
  104. }
  105. },
  106. actions: {
  107. // 首页数据
  108. async getHome (state) {
  109. getHomeHot({ sign: 'new', num: 5 }).then((res: APIStructure) => {
  110. if (!res.data.error_msg.length) {
  111. state.commit('saveHomeData', {
  112. new: res.data.data.map((v: APIStructure) => formatData(v))
  113. })
  114. }
  115. })
  116. getHomeHot({ sign: 'hot', num: 5 }).then((res: APIStructure) => {
  117. if (!res.data.error_msg.length) {
  118. state.commit('saveHomeData', {
  119. hot: res.data.data.map((v: APIStructure) => formatData(v))
  120. })
  121. }
  122. })
  123. getHomeActivity({ code: 3, size: 3, num: 1 }).then((res: APIStructure) => {
  124. if (!res.data.error_msg.length) {
  125. state.commit('saveHomeData', {
  126. keep: res.data.data.map((v: APIStructure) => formatData(v))
  127. })
  128. }
  129. })
  130. },
  131. // 文库购买
  132. async getDocPay (state, data) {
  133. try {
  134. const res = await getDocPay(data)
  135. return res.data
  136. } catch (error) {}
  137. },
  138. // 提现查询
  139. async getCashOutInfo (state, data) {
  140. try {
  141. const res = await getCashOutInfo(data)
  142. return res.data
  143. } catch (error) {}
  144. },
  145. // 提现提交
  146. async submitCashOutInfo (state, data) {
  147. try {
  148. const res = await submitCashOutInfo(data)
  149. return res.data
  150. } catch (error) {}
  151. },
  152. // 详情
  153. async getDetails (state, data) {
  154. try {
  155. const res = await getDetails(data)
  156. console.log(res)
  157. return res.data
  158. } catch (error) {}
  159. },
  160. // 文库收藏
  161. async getAdd (state, data) {
  162. try {
  163. const res = await getAdd(data)
  164. console.log(res)
  165. return res.data
  166. } catch (error) {}
  167. },
  168. // 文库取消收藏
  169. async getRemove (state, data) {
  170. try {
  171. const res = await getRemove(data)
  172. console.log(res)
  173. return res.data
  174. } catch (error) {}
  175. },
  176. // 剑鱼币
  177. async getCoin (state, data) {
  178. try {
  179. const res = await getCoin(data)
  180. console.log(res)
  181. return res.data
  182. } catch (error) {}
  183. }
  184. },
  185. getters: {}
  186. }
  187. export default modulesOption