product.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import axios from '~/plugins/axios'
  2. export const actions = {
  3. // 全局服务初始化
  4. nuxtServerInit (store, { params, route, isServer, req }) {
  5. // 检查设备类型
  6. const userAgent = isServer ? req.headers['user-agent'] : navigator.userAgent
  7. const isMobile = /(iPhone|iPod|Opera Mini|Android.*Mobile|NetFront|PSP|BlackBerry|Windows Phone)/ig.test(userAgent)
  8. store.commit('option/SET_MOBILE_LAYOUT', isMobile)
  9. store.commit('option/SET_USER_AGENT', userAgent)
  10. return Promise.all([
  11. // 全局数据
  12. // store.dispatch('loadUserInfo')
  13. ])
  14. },
  15. // 品牌列表推荐品牌配置
  16. loadRecommends ({ commit }) {
  17. commit('brand/REQUEST_RECOMMENDS')
  18. return axios.get(`/api/product/brand/hot/5`)
  19. .then(response => {
  20. commit('brand/GET_RECOMMENDS_SUCCESS', response.data)
  21. }, err => {
  22. commit('brand/GET_RECOMMENDS_FAILURE', err)
  23. })
  24. },
  25. // 品牌列表配置
  26. loadBrands ({ commit }, params = {}) {
  27. let keyword = params.keyword
  28. commit('brand/REQUEST_BRANDS', params)
  29. return axios.get(`/api/product/brand/initial/${keyword}`)
  30. .then(response => {
  31. commit('brand/GET_BRANDS_SUCCESS', response.data)
  32. }, err => {
  33. commit('brand/GET_BRANDS_FAILURE', err)
  34. })
  35. },
  36. // 获取全部子器件类目
  37. loadAllProductKinds ({ commit }, params = {}) {
  38. let id = params.id
  39. commit('kind/REQUEST_KIND', params)
  40. return axios.get(`/api/product/kind/${id}/children_all`)
  41. .then(response => {
  42. commit('kind/GET_KIND_SUCCESS', { id, result: response.data })
  43. }, err => {
  44. commit('kind/GET_KIND_FAILURE', {id, err})
  45. })
  46. },
  47. loadKindParentsWithBothers ({ commit }, params = {}) {
  48. let id = params.id
  49. commit('kind/REQUEST_KINDPARENTSWITHBOTHERS', params)
  50. return axios.get(`/api/product/kind/${id}/parentsWithBothers`)
  51. .then(response => {
  52. commit('kind/GET_KINDPARENTSWITHBOTHERS_SUCCESS', response.data)
  53. if (response.data) {
  54. if (!response.data[response.data.length - 1].leaf) {
  55. commit('kind/REQUEST_CHILDREN')
  56. return axios.get(`/api/product/kind/${id}/children`)
  57. .then(response => {
  58. commit('kind/GET_CHILDREN_SUCCESS', response.data)
  59. }, err => {
  60. commit('kind/GET_CHILDREN_FAILURE', err)
  61. })
  62. } else {
  63. commit('kind/REQUEST_KINDPROPERTY')
  64. return axios.get(`/api/product/kind/${id}/properties/values`)
  65. .then(response => {
  66. commit('kind/GET_KINDPROPERTY_SUCCESS', response.data)
  67. }, err => {
  68. commit('kind/GET_KINDPROPERTY_FAILURE', err)
  69. })
  70. }
  71. }
  72. }, err => {
  73. commit('kind/GET_KINDPARENTSWITHBOTHERS_FAILURE', err)
  74. })
  75. },
  76. loadKindBrands ({ commit }, params = {}) {
  77. let id = params.id
  78. commit('kind/REQUEST_KINDBRANDS')
  79. return axios.get(`/api/product/kind/${id}/brands`)
  80. .then(response => {
  81. commit('kind/GET_KINDBRANDS_SUCCESS', response.data)
  82. }, err => {
  83. commit('kind/GET_KINDBRANDS_FAILURE', err)
  84. })
  85. },
  86. pageComGoods ({ commit }, kindid = '', brandid = '', pageParams = { page: 1, count: 10 }) {
  87. let params = {}
  88. let filter = {kindid: kindid.kindid, brandid: kindid.brandid}
  89. params.filter = filter
  90. params.page = pageParams.page
  91. params.count = pageParams.count
  92. commit('component/REQUEST_CMPGOODS')
  93. return axios.get('/api/product/product/getCompGoodsByKindid', { params })
  94. .then(response => {
  95. commit('component/GET_CMPGOODS_SUCCESS', response.data)
  96. }, err => {
  97. commit('component/GET_CMPGOODS_FAILURE', err)
  98. })
  99. }
  100. }