index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. loadUserInfo ({ commit }) {
  17. commit('option/REQUEST_USER_INFO')
  18. return axios.get('/user/authentication')
  19. .then(response => {
  20. commit('option/REQUEST_USER_INFO_SUCCESS', response.data)
  21. }, err => {
  22. commit('option/REQUEST_USER_INFO_FAILURE', err)
  23. })
  24. },
  25. // 用户退出
  26. logout ({ commit }) {
  27. return axios.get('/logout')
  28. .then(response => {
  29. commit('option/REQUEST_LOGOUT_SUCCESS', response.data)
  30. })
  31. },
  32. // 获取楼层配置
  33. loadFloors ({ commit }) {
  34. commit('floor/REQUEST_LIST')
  35. return axios.get('/api/floors/home')
  36. .then(response => {
  37. commit('floor/GET_LIST_SUCCESS', response.data)
  38. }, err => {
  39. commit('floor/GET_LIST_FAILURE', err)
  40. })
  41. },
  42. // 获取轮播配置
  43. loadBanners ({ commit }) {
  44. commit('carousel/REQUEST_BANNER')
  45. return axios.get('/api/carousel/home%20page%20banner')
  46. .then(response => {
  47. commit('carousel/GET_BANNER_SUCCESS', response.data)
  48. }, err => {
  49. commit('carousel/GET_BANNER_FAILURE', err)
  50. })
  51. },
  52. // 获取子器件类目
  53. loadProductKinds ({ commit }, params = {}) {
  54. let id = params.id
  55. commit('product/kind/REQUEST_KIND', params)
  56. return axios.get(`/api/product/kind/${id}/children`)
  57. .then(response => {
  58. commit('product/kind/GET_KIND_SUCCESS', { id, result: response.data })
  59. }, err => {
  60. commit('product/kind/GET_KIND_FAILURE', {id, err})
  61. })
  62. },
  63. // 获取全部子器件类目
  64. loadAllProductKinds ({ commit }, params = {}) {
  65. let id = params.id
  66. commit('product/kind/REQUEST_KIND', params)
  67. return axios.get(`/api/product/kind/${id}/children_all`)
  68. .then(response => {
  69. commit('product/kind/GET_KIND_SUCCESS', { id, result: response.data })
  70. }, err => {
  71. commit('product/kind/GET_KIND_FAILURE', {id, err})
  72. })
  73. },
  74. // 获取首页新闻
  75. loadNewsSnapshot ({ commit }, params = {}) {
  76. commit('news/REQUEST_SNAPSHOT')
  77. return axios.get('/api/news/created', {params})
  78. .then(response => {
  79. commit('news/GET_SNAPSHOT_SUCCESS', response.data)
  80. }, err => {
  81. commit('news/GET_SNAPSHOT_FAILURE', err)
  82. })
  83. },
  84. // 获取器件统计信息
  85. loadProductCounts ({ commit }, params = {}) {
  86. commit('product/common/REQUEST_COUNTS')
  87. return axios.get('/api/product/commoncount', {params})
  88. .then(response => {
  89. commit('product/common/GET_COUNTS_SUCCESS', response.data)
  90. }, err => {
  91. commit('product/common/GET_COUNTS_FAILURE', err)
  92. })
  93. },
  94. // 搜索关键字
  95. searchKeywords ({ commit }, params = {}) {
  96. commit('search/REQUEST_KEYWORDS')
  97. return axios.get('/search/similarKeywords', {params})
  98. .then(response => {
  99. commit('search/GET_KEYWORDS_SUCCESS', response.data)
  100. }, err => {
  101. commit('search/GET_KEYWORDS_FAILURE', err)
  102. })
  103. },
  104. resetSearchKeywords ({ commit }) {
  105. commit('search/RESET_KEYWORDS')
  106. }
  107. }