actions.js 10.9 KB
import types from './mutation-types'
import { services } from '../api'
import { asynRouter } from '@/router'
import router from '@/router'
import store from './store'

// const getUserInfo = new Promise((resovle, reject) => {
//   services.common.getLoginUser()
//     .then((res) => {
//       resovle(res)
//     })
// })

// const getUserRoleInfo = new Promise((resolve, reject) => {
//   services.common.getUserRoleInfo().then((res)=>{
//     resolve(res)
//   })
// })

export default {
  getNavItems({ commit }) {
    // API request
    return services.menu.getAll().then(response => {
      commit(types.GET_MENU, response.data)
    })
  },
  toggleCollapse({ commit }) {
    commit(types.TOGGLE_COLLAPSE)
  },
  getLoginUser({ commit }) {
    return new Promise((resolve, reject) => {
      services.common
        .getLoginUser()
        .then(res => {
          commit(types.USER_NAME, res.username)
          commit(types.NAME, res.name)
          commit(types.USER_ID, res.id)
          resolve(res)
        })
        .catch(error => {
          reject(error)
        })
    })
    /* 2021-11-05 jack 注释
    return getUserInfo.then((res)=>{
      commit(types.USER_NAME, res.username)
      commit(types.NAME, res.name)
      commit(types.USER_ID, res.id)
    })*/
    /* return services.common.getLoginUser()
      .then((res) => {
        commit(types.USER_NAME, res.username)
        commit(types.NAME, res.name)
      }) */
  },

  /* 获取登录用户角色权限*/
  getRoleInfo({ commit, state, dispatch }) {
    return new Promise((resolve, reject) => {
      services.common.getUserRoleInfo().then(res => {
        const data = res.data
        console.log(data)
        if (data != null) {
          console.log('getRoleInfo')
          dispatch('getLoginUser')

          commit(types.ROLE_TYPE, data.auth)
          commit(types.IS_GQ, data.isGq)
          commit(types.IS_GQ_ORDER_READ, data.isGqOrderRead)
          commit(types.CAR_REMARK, data.carRemark)
          commit(types.CAR_USED, data.carUsed)
          commit(types.USER_REMARK, data.userRemark)
          commit(types.VEHICLE_ICON, data.vehicleIcon)
          commit(types.IS_ZHONGLI, data.zhongli)
          commit(types.EDIT_IMAGE, data.editImage)
          commit(types.pingAn, data.pingAn)
          if (data.isHtfz) {
            commit(types.IS_CHECKBOOK, res.data.isHtfz)
          }
          if (!data.showBusinessWorkOrder) {
            commit(types.IS_WORK_ORDER_ADMIN, data.showBusinessWorkOrder)
          }
          if (data.auditAccount) {
            commit(types.AUDIT_ACCOUNT, data.auditAccount)
          }
          if (data.showLocationComparison) {
            commit(types.COMPARE_LOCATE, data.showLocationComparison)
          }
          if (data.roleEShieldReconciliationManagement) {
            commit(
              types.ANJI_ACCOUNT,
              data.roleEShieldReconciliationManagement
            )
          }
          if (data.roleEShieldExportToExcel) {
            commit(types.ANJI_ACCOUNT_EXPORT, data.roleEShieldExportToExcel)
          }
          if (data.customerService) {
            commit(types.CUSTOMER_SERVCIE, data.customerService)
          }
          if (data.zlAccount) {
            commit(types.ZL_ACCOUNT, data.zlAccount)
          }
          if (data.roleEShieldVehicleGenerate) {
            commit(types.vehicleGenerate, data.roleEShieldVehicleGenerate)
          }
          // if (data.functionItemList && data.functionItemList.length) {
          //   return Promise.resolve({
          //     permission: data.functionItemList,
          //   })
          // }
          const mn = formaterRoutes(data.functionItemList)
          dispatch('setPermission', mn)

          resolve(mn)
        }
        // resolve(res)
      })
    })
    /* 2021-11-05 jack 注释
    return getUserRoleInfo.then((res) => {
      const data = res.data
      if (data != null) {
        console.log('getRoleInfo')
        commit(types.ROLE_TYPE, data.auth)
        commit(types.IS_GQ, data.isGq)
        commit(types.IS_GQ_ORDER_READ, data.isGqOrderRead)
        commit(types.CAR_REMARK, data.carRemark)
        commit(types.CAR_USED, data.carUsed)
        if (data.isHtfz) {
          commit(types.IS_CHECKBOOK, res.data.isHtfz)
        }
        if (!data.showBusinessWorkOrder) {
          commit(types.IS_WORK_ORDER_ADMIN, data.showBusinessWorkOrder)
        }
        if (data.auditAccount) {
          commit(types.AUDIT_ACCOUNT, data.auditAccount)
        }
        if (data.showLocationComparison) {
          commit(types.COMPARE_LOCATE, data.showLocationComparison)
        }
        if (data.roleEShieldReconciliationManagement) {
          commit(types.ANJI_ACCOUNT, data.roleEShieldReconciliationManagement)
        }
        if (data.roleEShieldExportToExcel) {
          commit(types.ANJI_ACCOUNT_EXPORT, data.roleEShieldExportToExcel)
        }
        if (data.customerService) {
          commit(types.CUSTOMER_SERVCIE, data.customerService)
        }
        if (data.zlAccount) {
          commit(types.ZL_ACCOUNT, data.zlAccount)
        }
        if (data.roleEShieldVehicleGenerate) {
          commit(types.vehicleGenerate, data.roleEShieldVehicleGenerate)
        }
        if (data.functionItemList && data.functionItemList.length) {
          return Promise.resolve({
            permission: data.functionItemList,
          })
        }
      }
      return Promise.resolve()
    })*/
    return new Promise((resolve, reject) => {
      if (typeof state.common.roleType !== 'undefined') {
        resolve()
        return
      }
      services.common.getUserRoleInfo().then(res => {
        console.log('state', state.common.roleType)
        if (res.returnCode == 0) {
          const data = res.data
          if (data != null) {
            commit(types.ROLE_TYPE, data.auth)
            commit(types.IS_GQ, data.isGq)
            commit(types.IS_GQ_ORDER_READ, data.isGqOrderRead)
            commit(types.CAR_REMARK, data.carRemark)
            commit(types.CAR_USED, data.carUsed)
            if (data.isHtfz) {
              commit(types.IS_CHECKBOOK, res.data.isHtfz)
            }
            if (!data.showBusinessWorkOrder) {
              commit(types.IS_WORK_ORDER_ADMIN, data.showBusinessWorkOrder)
            }
            if (data.auditAccount) {
              commit(types.AUDIT_ACCOUNT, data.auditAccount)
            }
            if (data.showLocationComparison) {
              commit(types.COMPARE_LOCATE, data.showLocationComparison)
            }
            if (data.roleEShieldReconciliationManagement) {
              commit(
                types.ANJI_ACCOUNT,
                data.roleEShieldReconciliationManagement
              )
            }
            if (data.roleEShieldExportToExcel) {
              commit(types.ANJI_ACCOUNT_EXPORT, data.roleEShieldExportToExcel)
            }
            if (data.customerService) {
              commit(types.CUSTOMER_SERVCIE, data.customerService)
            }
            if (data.zlAccount) {
              commit(types.ZL_ACCOUNT, data.zlAccount)
            }
            if (data.functionItemList && data.functionItemList.length) {
              resolve({
                permission: data.functionItemList
              })
            }
          }
        }
        resolve()
      })
    })
  },
  /* 获取设备管理菜单权限*/
  getDeviceResources({ commit }) {
    return new Promise((resolve, reject) => {
      services.common.getDeviceResources().then(res => {
        if (res.returnCode == 0) {
          const data = res.data
          const subData = data.filter(item => item.funId !== '108')
          commit(types.DEVICE_RESOURCES, subData)
        }
        resolve()
      })
    })
  },
  setFullsize({ commit }, data) {
    commit(types.SET_FULLSIZE, data)
  },
  setPermission({ commit }, data) {
    commit(types.PERMISSION, data)
  },

  /* 判断是否是仲利账号*/
  iszhognligroup({ commit }, userId) {
    return new Promise((resolve, reject) => {
      services.report.iszhognligroup({ userId }).then(res => {
        commit(types.ZHONGLI_TYPE, res.data)
        resolve(res.data)
      })
    })
  },

  // 退出登录
  logout({ commit }, data) {
    commit(types.PERMISSION, [])
    sessionStorage.removeItem('token')
    sessionStorage.removeItem('user')
    sessionStorage.clear()
  }
}

function getPathArr(arr, prevArr = []) {
  return arr.reduce((prevArr, item) => {
    prevArr.push(item.url)
    if (item.children && item.children.length) {
      getPathArr(item.children, prevArr)
    }
    return prevArr
  }, prevArr)
}

function filterMenuItems(arr, initArr = []) {
  return arr.reduce((prevArr, item) => {
    if (!item.notMenu) {
      if (item.children && item.children.length) {
        initArr.push({
          ...item,
          children: filterMenuItems(item.children)
        })
      } else {
        initArr.push({
          ...item
        })
      }
    }

    return initArr
  }, initArr)
}

function getMenuItems(arr) {
  return arr.map(item => {
    const _url = item.url.split('')
    _url.shift()
    const index = _url.join('')

    if (item.children && item.children.length) {
      const children = getMenuItems([...item.children])
      return {
        index,
        icon: item.icon,
        title: item.meta && item.meta.title,
        children
      }
    }
    return {
      index,
      icon: item.icon,
      title: item.meta && item.meta.title
    }
  })
}

function getFirstPath(arr) {
  const _item = arr[0]
  if (_item.children && _item.children.length) {
    return getFirstPath(_item.children)
  }
  return _item.url
}

function getFullPath(arr, prefix = '') {
  return arr.map(item => {
    item.fullPath = `${prefix}${item.path}`
    if (item.children && item.children.length) {
      const children = getFullPath([...item.children], `${item.path}/`)
      return {
        ...item,
        url: item.fullPath,
        children
      }
    }
    return {
      ...item,
      url: item.fullPath,
      title: item.meta && item.meta.title
    }
  })
}

function filterRoutes(arr, path, initArr = []) {
  arr.reduce((prevArr, item) => {
    if (item.notMenu) {
      initArr.push({
        ...item
      })
    } else {
      if (path.indexOf(item.url) > -1) {
        if (item.children && item.children.length) {
          initArr.push({
            ...item,
            children: filterRoutes(item.children, path)
          })
        } else {
          initArr.push({
            ...item
          })
        }
      }
    }
    return initArr
  }, initArr)
  return initArr
}

/** 处理菜单 */
function formaterRoutes(list) {
  const allPath = getPathArr(list)
  const fullPathRoutes = getFullPath(asynRouter)
  const allRoutes = filterRoutes(fullPathRoutes, allPath)

  const menuItems = getMenuItems(filterMenuItems([...allRoutes]))
  // firstPath = getFirstPath(allRoutes)

  //  commit('setPermission', menuItems)
  // store.dispatch('SET_MENU', menuItems)
  if (
    !store.state.common.permission ||
    store.state.common.permission.length <= 0
  ) {
    router.addRoutes(allRoutes)
  }

  return menuItems
}