index.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import axios from '~plugins/axios'
  2. import cityService from '~plugins/city-service'
  3. export const actions = {
  4. // 全局服务初始化
  5. nuxtServerInit (store, { params, route, isDev, isServer, req }) {
  6. // 检查设备类型
  7. const userAgent = isServer ? req.headers['user-agent'] : navigator.userAgent
  8. const isMobile = /(iPhone|iPod|Opera Mini|Android.*Mobile|NetFront|PSP|BlackBerry|Windows Phone)/ig.test(userAgent)
  9. const cookie = isServer ? req.headers['cookie'] : null
  10. store.commit('option/SET_MOBILE_LAYOUT', isMobile)
  11. store.commit('option/SET_USER_AGENT', userAgent)
  12. store.commit('option/SET_COOKIES', cookie)
  13. // 设置跳转的URL
  14. if (!isDev) {
  15. store.commit('option/UPDATE_URL', 'http://192.168.253.66:8081/')
  16. }
  17. return Promise.all([
  18. // 全局数据
  19. // store.dispatch('loadCarouselInfo', { client_type: 'cc', cityId: '5' }),
  20. store.dispatch('loadIsLogin'),
  21. store.dispatch('loadUserInfo')
  22. ])
  23. },
  24. // 判断是否登录
  25. loadIsLogin ({ commit }) {
  26. commit('option/REQUEST_IS_LOGIN')
  27. return axios.get('/sso/login/isLogin')
  28. .then(response => {
  29. // console.log(JSON.stringify(response.data))
  30. commit('option/REQUEST_IS_LOGIN_SUCCESS', response.data)
  31. }, err => {
  32. commit('option/REQUEST_IS_LOGIN_FAILURE', err)
  33. })
  34. },
  35. // 获取用户登录信息
  36. loadUserInfo ({ commit }) {
  37. commit('option/REQUEST_USER_INFO')
  38. return axios.get('/sso/center/user/info')
  39. .then(response => {
  40. commit('option/REQUEST_USER_INFO_SUCCESS', response.data)
  41. }, err => {
  42. commit('option/REQUEST_USER_INFO_FAILURE', err)
  43. })
  44. },
  45. // 用户退出
  46. logout ({ commit }) {
  47. return axios.get('/sso/login/logout')
  48. .then(response => {
  49. commit('option/REQUEST_LOGOUT_SUCCESS', response.data)
  50. })
  51. },
  52. // 获取表单配置
  53. loadFormData ({ commit }, params = {}) {
  54. commit('formConfig/REQUEST_FORMDATA')
  55. return cityService.get('/api/serve/config.action', {params})
  56. .then(response => {
  57. commit('formConfig/GET_FORMDATA_SUCCESS', response.data)
  58. }, err => {
  59. commit('formConfig/GET_FORMDATA_FAILURE', err)
  60. })
  61. }
  62. }