user.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. import { setToken, setRefreshToken, removeToken, removeRefreshToken } from '@/util/auth'
  2. import { Message } from 'element-ui'
  3. import { setStore, getStore } from '@/util/store'
  4. import { isURL, validatenull } from '@/util/validate'
  5. import { deepClone } from '@/util/util'
  6. import website from '@/config/website'
  7. import { loginByUsername, loginBySocial, getUserInfo, logout, refreshToken, getButtons } from '@/api/user'
  8. import { getByRoute, getTenantMenuByTenantIdAndRoleId, allByRole } from '@/api/lesseeLogin'
  9. import { getRoutes } from '@/api/system/menu'
  10. import { getList, getDictToWeb } from '@/api/system/dict'
  11. import md5 from 'js-md5'
  12. import { getUserAll } from '@/api/activiti/taskSetting'
  13. function addPath(ele, first) {
  14. const menu = website.menu
  15. const propsConfig = menu.props
  16. const propsDefault = {
  17. label: propsConfig.label || 'name',
  18. path: propsConfig.path || 'path',
  19. icon: propsConfig.icon || 'icon',
  20. children: propsConfig.children || 'children',
  21. }
  22. const icon = ele[propsDefault.icon]
  23. ele[propsDefault.icon] = validatenull(icon) ? menu.iconDefault : icon
  24. const isChild = ele[propsDefault.children] && ele[propsDefault.children].length !== 0
  25. if (!isChild) ele[propsDefault.children] = []
  26. if (!isChild && first && !isURL(ele[propsDefault.path])) {
  27. ele[propsDefault.path] = ele[propsDefault.path]
  28. // ele[propsDefault.path] = ele[propsDefault.path] + '/index'
  29. } else {
  30. ele[propsDefault.children].forEach((child) => {
  31. addPath(child)
  32. })
  33. }
  34. }
  35. function toTree(data) {
  36. let result = []
  37. if (!Array.isArray(data)) {
  38. return result
  39. }
  40. let map = {}
  41. data.forEach((item) => {
  42. map[item.id] = item
  43. })
  44. data.forEach((item) => {
  45. let parent = map[item.parentId]
  46. if (parent) {
  47. if (!parent.children) {
  48. parent.children = []
  49. }
  50. parent.children.push(item)
  51. } else {
  52. result.push(item)
  53. }
  54. })
  55. return result
  56. }
  57. const user = {
  58. state: {
  59. tenantId: getStore({ name: 'tenantId' }) || '',
  60. userInfo: getStore({ name: 'userInfo' }) || [],
  61. permission: getStore({ name: 'permission' }) || {},
  62. roles: [],
  63. menuId: {},
  64. menu: getStore({ name: 'menu' }) || [],
  65. menuAll: getStore({ name: 'menuAll' }) || [],
  66. menuTop: getStore({ name: 'menuTop' }) || [],
  67. pitchOn: getStore({ name: 'pitchOn' }) || '',
  68. dictData: getStore({ name: 'dictData' }) || [],
  69. dictTenantData: getStore({ name: 'dictTenantData' }) || [],
  70. constantData: getStore({ name: 'constantData' }) || [],
  71. token: getStore({ name: 'token' }) || '',
  72. refreshToken: getStore({ name: 'refreshToken' }) || '',
  73. },
  74. actions: {
  75. //获取所有字典
  76. GetDictAll({ commit }) {
  77. return new Promise((resolve, reject) => {
  78. getList()
  79. .then((res) => {
  80. res.data.data = res.data.data.map((item) => {
  81. return {
  82. ...item,
  83. dictLabel: item.dictValue,
  84. dictValue: item.dictKey,
  85. }
  86. })
  87. commit('SET_DICT_ALL', res.data.data)
  88. })
  89. .catch((error) => {
  90. reject(error)
  91. })
  92. getDictToWeb()
  93. .then((res) => {
  94. commit('SET_TENANT_DICT_ALL', res.data.data)
  95. })
  96. .catch((error) => {
  97. reject(error)
  98. })
  99. })
  100. },
  101. // 获取所有用户
  102. getAllUserInformation({ commit }) {
  103. return new Promise(() => {
  104. getUserAll({}).then((res) => {
  105. commit('SET_USER_ALL', res.data.data.list)
  106. })
  107. })
  108. },
  109. //租户业务员登录
  110. SalesmanLogin({ commit, dispatch }, userInfo) {
  111. return new Promise((resolve, reject) => {
  112. var pwd = ''
  113. var noPwd = ''
  114. if (userInfo.pwd) {
  115. pwd = md5(userInfo.pwd)
  116. } else {
  117. noPwd = 'noPwd'
  118. }
  119. loginByUsername(userInfo.tenantId, userInfo.deptId, userInfo.roleId, userInfo.account, pwd, 'tenant_salesman', userInfo.key, userInfo.code, noPwd)
  120. .then((res) => {
  121. const data = res.data
  122. if (data.error_description) {
  123. Message({
  124. message: data.error_description,
  125. type: 'error',
  126. })
  127. } else {
  128. commit('SET_TOKEN', data.access_token)
  129. commit('SET_REFRESH_TOKEN', data.access_token)
  130. commit('SET_TENANT_ID', data.tenant_id)
  131. commit('SET_USER_INFO', data)
  132. commit('DEL_ALL_TAG')
  133. commit('CLEAR_LOCK')
  134. getTenantMenuByTenantIdAndRoleId().then((resS) => {
  135. let menuAll = deepClone(resS.data.data.list)
  136. menuAll.forEach((ele) => {
  137. addPath(ele, true)
  138. })
  139. let pitchOn = ''
  140. if (data.applications.length > 0) {
  141. pitchOn = data.applications[0].id
  142. }
  143. let menu = deepClone(menuAll).filter(function (item) {
  144. return item.applicationId === pitchOn && item.category === 1
  145. })
  146. commit('SET_MENU_TOP', data.applications)
  147. commit('SET_SELECT', pitchOn)
  148. commit('SET_MENU_ALL', menuAll)
  149. commit('SET_MENU', toTree(menu))
  150. dispatch('GetButtons')
  151. resolve()
  152. })
  153. }
  154. })
  155. .catch((error) => {
  156. reject(error)
  157. })
  158. })
  159. },
  160. getSalesmanMenuAll({ commit, dispatch }, pitchOn) {
  161. return new Promise((resolve, reject) => {
  162. getTenantMenuByTenantIdAndRoleId()
  163. .then((res) => {
  164. let menuAll = deepClone(res.data.data.list)
  165. menuAll.forEach((ele) => {
  166. addPath(ele, true)
  167. })
  168. let menu = deepClone(menuAll).filter(function (item) {
  169. return item.applicationId === pitchOn && item.category === 1
  170. })
  171. commit('SET_MENU_ALL', menuAll)
  172. commit('SET_MENU', toTree(menu))
  173. dispatch('GetButtons')
  174. resolve()
  175. })
  176. .catch((error) => {
  177. reject(error)
  178. })
  179. })
  180. },
  181. //租户管理员登录
  182. LesseeLogin({ commit, dispatch }, userInfo) {
  183. return new Promise((resolve, reject) => {
  184. var pwd = ''
  185. var noPwd = ''
  186. if (userInfo.pwd) {
  187. pwd = md5(userInfo.pwd)
  188. } else {
  189. noPwd = 'noPwd'
  190. }
  191. loginByUsername(userInfo.tenantId, userInfo.deptId, userInfo.roleId, userInfo.account, pwd, 'tenant_admin', userInfo.key, userInfo.code, noPwd)
  192. .then((res) => {
  193. const data = res.data
  194. if (data.error_description) {
  195. Message({
  196. message: data.error_description,
  197. type: 'error',
  198. })
  199. } else {
  200. commit('SET_TOKEN', data.access_token)
  201. commit('SET_REFRESH_TOKEN', data.access_token)
  202. commit('SET_TENANT_ID', data.tenant_id)
  203. commit('SET_USER_INFO', data)
  204. commit('DEL_ALL_TAG')
  205. commit('CLEAR_LOCK')
  206. getByRoute({ path: '/tenantControl' }).then((res) => {
  207. let menuAll = deepClone(res.data.data.list)
  208. menuAll.forEach((ele) => {
  209. addPath(ele, true)
  210. })
  211. let menu = deepClone(menuAll).filter(function (item) {
  212. return item.id
  213. })
  214. commit('SET_MENU_TOP', [{ name: '控制台', source: 'iconfont iconicon_safety', id: '' }])
  215. commit('SET_SELECT', '')
  216. commit('SET_MENU_ALL', menuAll)
  217. commit('SET_MENU', toTree(menu))
  218. dispatch('GetButtons')
  219. resolve()
  220. })
  221. }
  222. })
  223. .catch((error) => {
  224. reject(error)
  225. })
  226. })
  227. },
  228. getLesseeMenuAll({ commit, dispatch }) {
  229. return new Promise((resolve, reject) => {
  230. getByRoute({ path: '/tenantControl' })
  231. .then((res) => {
  232. let menuAll = deepClone(res.data.data.list)
  233. menuAll.forEach((ele) => {
  234. addPath(ele, true)
  235. })
  236. let menu = deepClone(menuAll).filter(function (item) {
  237. return item.id
  238. })
  239. commit('SET_MENU_ALL', menuAll)
  240. commit('SET_MENU', toTree(menu))
  241. dispatch('GetButtons')
  242. resolve()
  243. })
  244. .catch((error) => {
  245. reject(error)
  246. })
  247. })
  248. },
  249. //服务商管理员登录
  250. LoginByUsername({ commit, dispatch }, userInfo) {
  251. return new Promise((resolve, reject) => {
  252. loginByUsername('000000', userInfo.deptId, userInfo.roleId, userInfo.username, md5(userInfo.password), 'manage', userInfo.key, userInfo.code, '')
  253. .then((res) => {
  254. const data = res.data
  255. if (data.error_description) {
  256. Message({
  257. message: data.error_description,
  258. type: 'error',
  259. })
  260. } else {
  261. commit('SET_TOKEN', data.access_token)
  262. commit('SET_REFRESH_TOKEN', data.access_token)
  263. commit('SET_TENANT_ID', data.tenant_id)
  264. commit('SET_USER_INFO', data)
  265. commit('DEL_ALL_TAG')
  266. commit('CLEAR_LOCK')
  267. }
  268. allByRole().then((res) => {
  269. const data = res.data.data.details.topMenus || []
  270. let pitchOn = ''
  271. if (data.length > 0) {
  272. pitchOn = data[0].id
  273. }
  274. let menuAll = deepClone(res.data.data.details.menus)
  275. menuAll.forEach((ele) => {
  276. addPath(ele, true)
  277. })
  278. let menu = deepClone(menuAll).filter(function (item) {
  279. return item.topMenuId === pitchOn && item.category === 1
  280. })
  281. commit('SET_SELECT', pitchOn)
  282. commit('SET_MENU_TOP', data)
  283. commit('SET_MENU_ALL', menuAll)
  284. commit('SET_MENU', toTree(menu))
  285. dispatch('GetButtons')
  286. resolve()
  287. })
  288. })
  289. .catch((error) => {
  290. reject(error)
  291. })
  292. })
  293. },
  294. getAdminMenuAll({ commit, dispatch }, pitchOn) {
  295. return new Promise((resolve, reject) => {
  296. allByRole()
  297. .then((res) => {
  298. let menuAll = deepClone(res.data.data.details.menus)
  299. menuAll.forEach((ele) => {
  300. addPath(ele, true)
  301. })
  302. let menu = deepClone(menuAll).filter(function (item) {
  303. return item.topMenuId === pitchOn && item.category === 1
  304. })
  305. commit('SET_MENU_ALL', menuAll)
  306. commit('SET_MENU', toTree(menu))
  307. dispatch('GetButtons')
  308. resolve()
  309. })
  310. .catch((error) => {
  311. reject(error)
  312. })
  313. })
  314. },
  315. //根据手机号登录
  316. LoginByPhone({ commit }, userInfo) {
  317. return new Promise((resolve) => {
  318. loginByUsername(userInfo.phone, userInfo.code).then((res) => {
  319. const data = res.data.data
  320. commit('SET_TOKEN', data)
  321. commit('DEL_ALL_TAG')
  322. commit('CLEAR_LOCK')
  323. resolve()
  324. })
  325. })
  326. },
  327. //根据第三方信息登录
  328. LoginBySocial({ commit }, userInfo) {
  329. return new Promise((resolve) => {
  330. loginBySocial(userInfo.tenantId, userInfo.source, userInfo.code, userInfo.state).then((res) => {
  331. const data = res.data
  332. if (data.error_description) {
  333. Message({
  334. message: data.error_description,
  335. type: 'error',
  336. })
  337. } else {
  338. commit('SET_TOKEN', data.access_token)
  339. commit('SET_REFRESH_TOKEN', data.access_token)
  340. commit('SET_USER_INFO', data)
  341. commit('DEL_ALL_TAG')
  342. commit('CLEAR_LOCK')
  343. }
  344. resolve()
  345. })
  346. })
  347. },
  348. //获取用户信息
  349. GetUserInfo({ commit }) {
  350. return new Promise((resolve, reject) => {
  351. getUserInfo()
  352. .then((res) => {
  353. const data = res.data.data
  354. commit('SET_ROLES', data.roles)
  355. resolve(data)
  356. })
  357. .catch((err) => {
  358. reject(err)
  359. })
  360. })
  361. },
  362. //刷新token
  363. refreshToken({ state, commit }) {
  364. window.console.log('handle refresh token')
  365. return new Promise((resolve, reject) => {
  366. refreshToken(state.refreshToken, state.tenantId)
  367. .then((res) => {
  368. const data = res.data
  369. commit('SET_TOKEN', data.access_token)
  370. commit('SET_REFRESH_TOKEN', data.access_token)
  371. resolve()
  372. })
  373. .catch((error) => {
  374. reject(error)
  375. })
  376. })
  377. },
  378. // 登出
  379. LogOut({ commit }) {
  380. return new Promise((resolve, reject) => {
  381. logout()
  382. .then(() => {
  383. commit('SET_TOKEN', '')
  384. commit('SET_MENU', [])
  385. commit('SET_MENU_ALL_NULL', [])
  386. commit('SET_USER_ALL_NULL', [])
  387. commit('SET_ROLES', [])
  388. commit('SET_TAG_LIST', [])
  389. commit('DEL_ALL_TAG')
  390. commit('CLEAR_LOCK')
  391. removeToken()
  392. removeRefreshToken()
  393. localStorage.clear()
  394. resolve()
  395. })
  396. .catch((error) => {
  397. reject(error)
  398. })
  399. })
  400. },
  401. //注销session
  402. FedLogOut({ commit }) {
  403. return new Promise((resolve) => {
  404. commit('SET_TOKEN', '')
  405. commit('SET_MENU_ALL_NULL', [])
  406. commit('SET_USER_ALL_NULL', [])
  407. commit('SET_MENU', [])
  408. commit('SET_ROLES', [])
  409. commit('SET_TAG_LIST', [])
  410. commit('DEL_ALL_TAG')
  411. commit('CLEAR_LOCK')
  412. removeToken()
  413. removeRefreshToken()
  414. resolve()
  415. })
  416. },
  417. //获取顶部菜单
  418. GetTopMenu({ commit, dispatch }) {
  419. return new Promise((resolve) => {
  420. allByRole().then((res) => {
  421. const data = res.data.data.details.topMenus || []
  422. let pitchOn = ''
  423. if (data.length > 0) {
  424. pitchOn = data[0].id
  425. }
  426. let menuAll = deepClone(res.data.data.details.menus)
  427. menuAll.forEach((ele) => {
  428. addPath(ele, true)
  429. })
  430. let menu = deepClone(menuAll).filter(function (item) {
  431. return item.topMenuId === pitchOn && item.category === 1
  432. })
  433. commit('SET_SELECT', pitchOn)
  434. commit('SET_MENU_ALL', menuAll)
  435. commit('SET_MENU', toTree(menu))
  436. dispatch('GetButtons')
  437. resolve(data)
  438. })
  439. })
  440. },
  441. //切换菜单
  442. SwitchToMenu({ commit, dispatch }, data) {
  443. return new Promise((resolve) => {
  444. var menu = []
  445. if (data.id) {
  446. menu = deepClone(data.menus).filter(function (item) {
  447. return (item.id === data.id || item.applicationId === data.id) && item.category === 1
  448. })
  449. } else {
  450. menu = deepClone(data.menus)
  451. }
  452. commit('SET_SELECT', data.id)
  453. commit('SET_MENU', toTree(menu))
  454. dispatch('GetButtons')
  455. resolve(menu)
  456. })
  457. },
  458. //服务商切换菜单
  459. AdminSwitchToMenu({ commit, dispatch }, data) {
  460. return new Promise((resolve) => {
  461. var menu = []
  462. if (data.id) {
  463. menu = deepClone(data.menus).filter(function (item) {
  464. return item.topMenuId === data.id && item.category === 1
  465. })
  466. } else {
  467. menu = deepClone(data.menus)
  468. }
  469. commit('SET_SELECT', data.id)
  470. commit('SET_MENU', toTree(menu))
  471. dispatch('GetButtons')
  472. resolve(menu)
  473. })
  474. },
  475. //获取系统菜单
  476. GetMenu({ commit, dispatch }, topMenuId) {
  477. return new Promise((resolve) => {
  478. getRoutes(topMenuId).then((res) => {
  479. const data = res.data.data
  480. let menu = deepClone(data)
  481. menu.forEach((ele) => {
  482. addPath(ele, true)
  483. })
  484. commit('SET_MENU', menu)
  485. dispatch('GetButtons')
  486. resolve(menu)
  487. })
  488. })
  489. },
  490. //获取系统按钮
  491. GetButtons({ commit }) {
  492. return new Promise((resolve) => {
  493. getButtons().then((res) => {
  494. const data = res.data.data
  495. commit('SET_PERMISSION', data)
  496. resolve()
  497. })
  498. })
  499. },
  500. },
  501. mutations: {
  502. SET_TOKEN: (state, token) => {
  503. setToken(token)
  504. state.token = token
  505. setStore({ name: 'token', content: state.token })
  506. },
  507. SET_MENU_ID(state, menuId) {
  508. state.menuId = menuId
  509. },
  510. SET_MENU_ALL: (state, menuAll) => {
  511. let menu = state.menuAll
  512. menuAll.forEach((ele) => {
  513. if (!menu.find((item) => item.label === ele.label && item.path === ele.path && item.applicationId === ele.applicationId)) {
  514. menu.push(ele)
  515. }
  516. })
  517. state.menuAll = menuAll
  518. setStore({ name: 'menuAll', content: state.menuAll })
  519. },
  520. SET_MENU_ALL_NULL: (state) => {
  521. state.menuAll = []
  522. setStore({ name: 'menuAll', content: state.menuAll })
  523. },
  524. SET_USER_ALL: (state, userAll) => {
  525. state.userAll = userAll
  526. setStore({ name: 'userAll', content: state.userAll })
  527. },
  528. SET_USER_ALL_NULL: (state) => {
  529. state.userAll = []
  530. setStore({ name: 'userAll', content: state.userAll })
  531. },
  532. SET_MENU: (state, menu) => {
  533. state.menu = menu
  534. setStore({ name: 'menu', content: state.menu })
  535. },
  536. SET_SELECT: (state, pitchOn) => {
  537. state.pitchOn = pitchOn
  538. setStore({ name: 'pitchOn', content: state.pitchOn })
  539. },
  540. SET_DICT_ALL: (state, dictData) => {
  541. state.dictData = dictData
  542. setStore({ name: 'dictData', content: state.dictData })
  543. },
  544. SET_TENANT_DICT_ALL: (state, dictTenantData) => {
  545. state.dictTenantData = dictTenantData
  546. setStore({ name: 'dictTenantData', content: state.dictTenantData })
  547. },
  548. SET_MENU_TOP: (state, menuTop) => {
  549. state.menuTop = menuTop
  550. setStore({ name: 'menuTop', content: state.menuTop })
  551. },
  552. SET_REFRESH_TOKEN: (state, refreshToken) => {
  553. setRefreshToken(refreshToken)
  554. state.refreshToken = refreshToken
  555. setStore({ name: 'refreshToken', content: state.refreshToken })
  556. },
  557. SET_TENANT_ID: (state, tenantId) => {
  558. state.tenantId = tenantId
  559. setStore({ name: 'tenantId', content: state.tenantId })
  560. },
  561. SET_USER_INFO: (state, userInfo) => {
  562. if (validatenull(userInfo.avatar)) {
  563. userInfo.avatar = '/img/bg/img-logo.png'
  564. }
  565. state.userInfo = userInfo
  566. setStore({ name: 'userInfo', content: state.userInfo })
  567. },
  568. SET_ROLES: (state, roles) => {
  569. state.roles = roles
  570. },
  571. SET_PERMISSION: (state, permission) => {
  572. let result = []
  573. function getCode(list) {
  574. list.forEach((ele) => {
  575. if (typeof ele === 'object') {
  576. const chiildren = ele.children
  577. const code = ele.code
  578. if (chiildren) {
  579. getCode(chiildren)
  580. } else {
  581. result.push(code)
  582. }
  583. }
  584. })
  585. }
  586. getCode(permission)
  587. state.permission = {}
  588. result.forEach((ele) => {
  589. state.permission[ele] = true
  590. })
  591. setStore({ name: 'permission', content: state.permission })
  592. },
  593. },
  594. }
  595. export default user