goodsclass.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import PersistedState from '../PersistedState'
  2. import { getGoodsclassList } from '../../api/homesearch'
  3. // initial state
  4. const state = {
  5. items: PersistedState.getStorage('goodsclass','items',[]),
  6. currentItem: PersistedState.getStorage('goodsclass','currentItem')
  7. }
  8. // mutations
  9. const mutations = {
  10. saveGoodsclassItems (state, items) {
  11. state.items = items
  12. PersistedState.setStorage('goodsclass',state)
  13. },
  14. clearGoodsclassItems (state) {
  15. state.items = null
  16. PersistedState.setStorage('goodsclass',state)
  17. },
  18. saveCurrentGoodsclassItem (state, item) {
  19. state.currentItem = item
  20. PersistedState.setStorage('goodsclass',state)
  21. },
  22. resetCurrentGoodsclassItem (state) {
  23. if (state.items && state.items.length) {
  24. state.currentItem = state.items[0]
  25. PersistedState.setStorage('goodsclass',state)
  26. }
  27. }
  28. }
  29. // actions
  30. const actions = {
  31. fetchGoodsclassList ({ commit, state }) {
  32. return new Promise((resolve, reject) => {
  33. getGoodsclassList().then((response) => {
  34. if (response.result && response.result.class_list && response.result.class_list.length) {
  35. commit('saveGoodsclassItems', response.result.class_list)
  36. if (!state.currentItem) {
  37. commit('saveCurrentGoodsclassItem', response.result.class_list[0])
  38. }
  39. }
  40. resolve(response)
  41. }, (error) => {
  42. reject(error)
  43. })
  44. })
  45. }
  46. }
  47. export default {
  48. state,
  49. mutations,
  50. actions
  51. }