user.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. import router from "@/router";
  2. import {
  3. login,
  4. menuList,
  5. dingdingdetail,
  6. usergetinfo,
  7. roleProcessInfo,
  8. requsetSupertubeCompany,
  9. requsetUserBindingCompany,
  10. reuqsetRoleInfo,
  11. userlist,
  12. } from "@/apis/user";
  13. import {
  14. getToken,
  15. setToken,
  16. removeToken,
  17. getMenu,
  18. setMenu,
  19. removeMenu,
  20. getBtn,
  21. setBtn,
  22. removeBtn,
  23. get_business_company,
  24. set_business_company,
  25. remove_business_company,
  26. get_business_companyNo,
  27. set_business_companyNo,
  28. remove_business_companyNo,
  29. get_private_field,
  30. set_private_field,
  31. remove_private_field,
  32. get_action_data,
  33. set_action_data,
  34. remove_action_data,
  35. get_role_level,
  36. getUserCompany,
  37. setUserCompany,
  38. removeUserCompany,
  39. setUserInfo as _setUserInfo,
  40. getUserInfo as _getUserInfo,
  41. removeUserInfo as _removeUserInfo,
  42. getCompanyType as _getCompanyType,
  43. setCompanyType as _setCompanyType,
  44. removeCompanyType as _removeCompanyType,
  45. } from "@/utils/auth";
  46. import { MessageBox } from "element-ui";
  47. import { convertCompanylist } from "@/utils";
  48. export const userStoreActions = {
  49. reloadMenu: "user/reloadMenulist",
  50. loginByToken: "user/loginByToken",
  51. setCompanyList: "user/setCompanylist",
  52. setCurrentLevel: "user/setCurrentLevel",
  53. setCurrentCompany: "user/setCurrentCompany",
  54. };
  55. const ids = ["36", "37", "44", "109"];
  56. const tearchChenId = "37"
  57. const state = {
  58. token: getToken(),
  59. name: "",
  60. avatar: require("@/assets/img/avatar.gif"),
  61. introduction: "",
  62. roles: ["admin"],
  63. btnList: getBtn(),
  64. navList: getMenu(),
  65. role_level: get_role_level() || "1",
  66. business_company: get_business_company() || "",
  67. business_companyNo: get_business_companyNo() || "",
  68. private_field: get_private_field() || [],
  69. roleProcess: get_action_data() || [],
  70. currentCompany: getUserCompany() ? getUserCompany().code : "",
  71. userInfo: _getUserInfo(),
  72. isSupertube: false,
  73. isTeacherChen: false,
  74. is_super_admin_ids: false,
  75. companylist: [],
  76. originLevel: "0",
  77. currentLevel: "0",
  78. };
  79. const companyTypes = {
  80. business: "business",
  81. supplier: "supplier",
  82. };
  83. const mutations = {
  84. SET_TOKEN: (state, token) => {
  85. state.token = token;
  86. },
  87. SET_MENU: (state, menu) => {
  88. state.menu = menu;
  89. },
  90. SET_HAS: (state, has) => {
  91. state.has = has;
  92. },
  93. SET_INTRODUCTION: (state, introduction) => {
  94. state.introduction = introduction;
  95. },
  96. SET_NAME: (state, name) => {
  97. state.name = name;
  98. },
  99. SET_AVATAR: (state, avatar) => {
  100. state.avatar = avatar;
  101. },
  102. SET_IS_SUPER_ADMIN_IDS: (state, is) => {
  103. state.is_super_admin_ids = is;
  104. },
  105. setIsTearcherChen(state, current){
  106. state.isTeacherChen = current
  107. },
  108. SET_ROLES: (state, roles) => {
  109. state.roles = roles;
  110. },
  111. navListFn(state, val) {
  112. if (!val) {
  113. val = [];
  114. }
  115. state.navList = val;
  116. setMenu(val);
  117. },
  118. btnListFn(state, val) {
  119. if (!val) {
  120. val = [];
  121. }
  122. state.btnList = val;
  123. setBtn(val);
  124. },
  125. setSupplier(state, val) {
  126. state.isSupplier = val;
  127. },
  128. business_company(state, val) {
  129. state.business_company = val;
  130. set_business_company(val);
  131. },
  132. business_companyNo(state, val) {
  133. state.business_companyNo = val;
  134. set_business_companyNo(val);
  135. },
  136. roleProcess(state, val) {
  137. state.roleProcess = val;
  138. set_action_data(val);
  139. },
  140. private_field(state, val) {
  141. state.private_field = val;
  142. set_private_field(val);
  143. },
  144. setCurrentLevel(state, val) {
  145. state.currentLevel = val;
  146. },
  147. setOriginLevel(state, val) {
  148. state.originLevel = val;
  149. },
  150. setUserInfo(state, info) {
  151. state.userInfo = info;
  152. _setUserInfo(info);
  153. },
  154. setCurrentCompany(state, code) {
  155. //获取当前公司详细信息
  156. const company = state.companylist.find(({ code: _code }) => _code === code);
  157. setUserCompany(company);
  158. state.currentCompany = code;
  159. },
  160. setCompanyType(state, code) {
  161. const isBusiness = code.indexOf("GS") >= 0;
  162. state.companyType = isBusiness
  163. ? companyTypes.business
  164. : companyTypes.supplier;
  165. },
  166. setCompanylist(state, val) {
  167. state.companylist = val;
  168. },
  169. setSupertube(state, val) {
  170. state.isSupertube = val;
  171. },
  172. };
  173. const actions = {
  174. // user login
  175. login({ commit }, userInfo) {
  176. const { username, password } = userInfo;
  177. return new Promise((resolve, reject) => {
  178. login({ username: username.trim(), password: password })
  179. .then((response) => {
  180. const { code, data } = response;
  181. if (code === 0) {
  182. const { token, nickname } = data;
  183. commit("SET_TOKEN", token);
  184. commit("SET_NAME", nickname);
  185. setToken(token);
  186. }
  187. resolve(response);
  188. })
  189. .catch((error) => {
  190. reject(error);
  191. });
  192. });
  193. },
  194. //公司角色异常
  195. disabledCompany({ commit, that }) {
  196. commit("navListFn", []);
  197. commit("btnListFn", []);
  198. initRouter([], that);
  199. const company = getUserCompany();
  200. MessageBox.alert(
  201. `公司 ${company.name} 角色异常或该公司禁用,请检查账号或者角色设置`,
  202. "公司角色异常",
  203. { type: "warning", confirmButtonText: "前往首页" }
  204. )
  205. .then(() => {
  206. router.push("/reload?redirect=welcome/dashboard");
  207. })
  208. .catch(() => {
  209. router.push("/reload?redirect=welcome/dashboard");
  210. });
  211. },
  212. dingUserInfor({ commit }, code) {
  213. return new Promise(async (resolve, reject) => {
  214. dingdingdetail(code)
  215. .then((response) => {
  216. const { data } = response;
  217. if (response.code === 0) {
  218. commit("SET_TOKEN", data.token);
  219. setToken(data.token);
  220. resolve("success");
  221. } else {
  222. resolve("error");
  223. }
  224. })
  225. .catch((error) => {
  226. reject(error);
  227. });
  228. });
  229. },
  230. async loginByToken({ commit, dispatch }, token) {
  231. setToken(token);
  232. await commit("SET_TOKEN");
  233. await dispatch("getMenuList");
  234. },
  235. // 切换公司后重新请求
  236. async reloadMenulist({ commit, dispatch, that, state }, level) {
  237. return new Promise(async (resolve) => {
  238. const company = getUserCompany();
  239. dispatch("getRoleInfo", company);
  240. //获取菜单
  241. const { mcode, mdata, message: mmsg } = await getRoleMenuList(level);
  242. //账号禁用
  243. if (mcode === 10001) {
  244. commit("navListFn", []);
  245. commit("btnListFn", []);
  246. initRouter([], that);
  247. resolve("disabled");
  248. return;
  249. }
  250. if (mcode >= 100 && mcode <= 104) {
  251. resolve("noToken");
  252. } else if (mcode !== 0) {
  253. resolve({ code: mcode, data: {}, massage: mmsg });
  254. } else {
  255. const { arrag, isok, btnList } = mdata;
  256. // 生成页面权限
  257. commit("navListFn", arrag);
  258. // 生成按钮权限
  259. commit("btnListFn", btnList);
  260. // 生成路由
  261. initRouter(arrag, that);
  262. resolve(isok ? "success-dataV" : "success");
  263. }
  264. });
  265. },
  266. async getRoleInfo({ commit, that, dispatch }, _current_company_) {
  267. return new Promise(async (resolve) => {
  268. //查找默认的业务公司 设置到全局的currentCompany
  269. const { roleid } = _current_company_;
  270. //根据角色获取流程节点权限
  271. const { pcode, pdata, pmsg } = await getRoleProcess(roleid);
  272. if (pcode === 0) {
  273. commit("roleProcess", pdata ?? []);
  274. }
  275. if (pcode >= 100 && pcode <= 104) {
  276. resolve("noToken");
  277. return;
  278. }
  279. //根据角色获取角色详情 获取关键字字段
  280. const {
  281. code: rcode,
  282. data: rdata,
  283. message: rmsg,
  284. } = await reuqsetRoleInfo({ roleid: roleid });
  285. if (rcode === 0) {
  286. commit(
  287. "private_field",
  288. rdata.private_field ? rdata.private_field.split(",") : []
  289. );
  290. }
  291. if (rcode >= 100 && rcode <= 104) {
  292. resolve("noToken");
  293. return;
  294. }
  295. });
  296. },
  297. async getMenuList({ commit, that, dispatch }) {
  298. return new Promise(async (resolve) => {
  299. if (!getToken()) {
  300. resolve("noToken");
  301. } else {
  302. const { ucode, udata, umsg } = await getUserInfo();
  303. if (ucode >= 100 && ucode <= 104) {
  304. resolve("noToken");
  305. } else if (ucode !== 0) {
  306. resolve({ code: ucode, data: udata, massage: umsg });
  307. } else {
  308. const { level, id } = udata;
  309. //是否超管
  310. const isSupertube = level === "1";
  311. //是否供应商
  312. const isSupplier = level === "3";
  313. //保存用户信息
  314. commit("setUserInfo", udata);
  315. // const
  316. const isOkIndex = ids.findIndex((s) => String(s) === String(id));
  317. commit("SET_IS_SUPER_ADMIN_IDS", isOkIndex !== -1);
  318. commit("setIsTearcherChen", String(id) === tearchChenId)
  319. //保存是否超管
  320. commit("setSupplier", isSupplier);
  321. //保存是否超管用户
  322. commit("setSupertube", isSupertube);
  323. //设置原始level和当前level
  324. commit("setOriginLevel", level);
  325. commit("setCurrentLevel", level);
  326. //获取公司列表(区分超管和普通账户api)
  327. const api = isSupertube
  328. ? requsetSupertubeCompany
  329. : requsetUserBindingCompany;
  330. //获取缓存里的业务公司 => 公司名称搜索 => 存在设置为默认公司
  331. const { data: companyData, code: companyCode } = await api({
  332. ...(!isSupertube ? { size: 10000 } : { type: "1" }),
  333. ...{ token: getToken() },
  334. });
  335. //保存公司列表 普通用户和超管使用两个列表需要转换
  336. let _companylist = [];
  337. if (companyCode === 0) {
  338. _companylist = isSupertube
  339. ? companyData.list
  340. : convertCompanylist(companyData.list);
  341. //超管 + 业务公司 需要将 relation_code 设置为 code
  342. // if(isSupertube) _companylist = _companylist.map(item => ({...item, code: item.relation_code}))
  343. commit("setCompanylist", _companylist);
  344. }
  345. if (companyCode >= 100 && companyCode <= 104) {
  346. resolve("noToken");
  347. return;
  348. }
  349. //非超管用户获取保存 流程权限 & 关键字字段 & 设置默认业务公司
  350. let defaultCompany = "";
  351. if (!isSupertube) {
  352. //查找默认的业务公司 设置到全局的currentCompany
  353. defaultCompany = _companylist.find(
  354. ({ is_main }) => is_main === "1"
  355. );
  356. const result = dispatch("getRoleInfo", defaultCompany);
  357. if (result === "noToken") {
  358. resolve("noToken");
  359. return;
  360. }
  361. }
  362. //保存默认公司
  363. commit(
  364. "setCurrentCompany",
  365. defaultCompany ? defaultCompany.code : ""
  366. );
  367. const { mcode, mdata, mmsg } = await getRoleMenuList();
  368. //拦截账号异常: 禁用
  369. if (mcode === 10001) {
  370. dispatch("disabledCompany");
  371. return;
  372. }
  373. if (mcode >= 100 && mcode <= 104) {
  374. resolve("noToken");
  375. } else if (mcode !== 0) {
  376. resolve({ code: mcode, data: {}, massage: mmsg });
  377. } else {
  378. const { arrag, isok, btnList } = mdata;
  379. // 生成页面权限
  380. commit("navListFn", arrag);
  381. // 生成按钮权限
  382. commit("btnListFn", btnList);
  383. // 生成路由
  384. initRouter(arrag, that);
  385. resolve(isok ? "success-dataV" : "success");
  386. }
  387. }
  388. }
  389. });
  390. },
  391. // user logout
  392. logout({ commit, state, dispatch }) {
  393. return new Promise((resolve, reject) => {
  394. commit("SET_TOKEN", "");
  395. commit("SET_ROLES", []);
  396. commit("SET_MENU", "");
  397. commit("SET_HAS", "");
  398. _removeUserInfo();
  399. removeBtn();
  400. removeMenu();
  401. removeToken();
  402. remove_business_company();
  403. commit("business_company", "");
  404. commit("business_companyNo", "");
  405. remove_business_companyNo();
  406. remove_private_field();
  407. commit("private_field", []);
  408. remove_action_data();
  409. commit("action_data", []);
  410. removeUserCompany();
  411. commit("action_data", []);
  412. // removeMainList()
  413. // reset visited views and cached views
  414. // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
  415. dispatch("tagsView/delAllViews", null, { root: true });
  416. resolve();
  417. });
  418. },
  419. // remove token
  420. resetToken({ commit }) {
  421. return new Promise((resolve) => {
  422. commit("SET_TOKEN", "");
  423. commit("SET_ROLES", []);
  424. removeToken();
  425. resolve();
  426. });
  427. },
  428. };
  429. async function getUserInfo() {
  430. const { code, data, meaasge } = await usergetinfo({
  431. token: getToken(),
  432. });
  433. return {
  434. ucode: code,
  435. udata: data,
  436. umsg: meaasge,
  437. };
  438. }
  439. async function getRoleProcess(roleid) {
  440. const { code, data, meaasge } = await roleProcessInfo({ roleid });
  441. let list = data;
  442. list.map((si) => {
  443. si.action = [];
  444. if (si.child && si.child.length > 0) {
  445. si.child.map((sii) => {
  446. si.action.push(sii.order_process + "");
  447. return sii;
  448. });
  449. }
  450. return si;
  451. });
  452. return {
  453. pcode: code,
  454. pdata: list,
  455. pmsg: meaasge,
  456. };
  457. }
  458. // function isHasRoute(routes){
  459. // let isHas = false
  460. // const chunks = location.hash.split('/')
  461. // const currentPath = chunks[chunks.length - 1]
  462. // const dfs = (routes) => {
  463. // routes.forEach(route => {
  464. // if(route.path === currentPath) isHas = true;
  465. // if(route.children && route.children.length > 0) dfs(route.children)
  466. // })
  467. // }
  468. // dfs([
  469. // {
  470. // path: "/person",
  471. // component: () => import("@/layout/index"),
  472. // redirect: "/person/my-message",
  473. // hidden: true,
  474. // alwaysShow: true,
  475. // meta: {
  476. // title: "个人中心",
  477. // icon: "user",
  478. // },
  479. // children: [
  480. // {
  481. // path: "my-message",
  482. // component: () => import("@/views/person-infor/my-message"),
  483. // name: "MyMessage",
  484. // alwaysShow: true,
  485. // meta: { title: "我的信息", noCache: true, breadcrumb: true },
  486. // },
  487. // {
  488. // path: "change-phone",
  489. // component: () => import("@/views/person-infor/change-phone"),
  490. // name: "ChangePhone",
  491. // alwaysShow: true,
  492. // meta: { title: "更换手机号", noCache: true, breadcrumb: true },
  493. // },
  494. // {
  495. // path: "change-password",
  496. // component: () => import("@/views/person-infor/change-password"),
  497. // name: "ChangePassword",
  498. // alwaysShow: true,
  499. // meta: { title: "修改密码", noCache: true, breadcrumb: true },
  500. // },
  501. // {
  502. // path:'/reload',
  503. // component:() => import('@/views/reload/index')
  504. // }
  505. // ],
  506. // },
  507. // //欢迎页
  508. // {
  509. // path: "/welcome",
  510. // component: () => import("@/layout/index"),
  511. // redirect: "/welcome/dashboard",
  512. // children: [
  513. // {
  514. // path: "dashboard",
  515. // component: () => import("@/views/dashboard/index"),
  516. // name: "Dashboard",
  517. // meta: { title: "首页", icon: "dashboard", affix: true },
  518. // },
  519. // ],
  520. // },...routes])
  521. // return isHas
  522. // }
  523. // 区分供应商和业务公司获取不同的菜单列表
  524. async function getRoleMenuList(level = "") {
  525. const { code, data, message } = await menuList({
  526. ...(level && level !== "1" ? { level } : {}),
  527. });
  528. //menu接受账户异常:禁用 并返回
  529. if (message === "账户已禁用" || message === "没有该公司的默认角色") {
  530. return {
  531. mdata: {},
  532. mmsg: "账号被禁用",
  533. mcode: 10001,
  534. };
  535. }
  536. let arrag = [];
  537. let btnList = [];
  538. let list = [];
  539. let isok = false;
  540. if (code === 0) {
  541. // const { id } = _getUserInfo();
  542. // const isOkIndex = ids.findIndex((s) => String(s) === String(id));
  543. const idok = state.is_super_admin_ids;
  544. if (!idok) {
  545. console.log(data)
  546. list = data.filter(function (item) {
  547. return item.menu_route !== "system"
  548. });
  549. } else {
  550. list = data ?? [];
  551. }
  552. //获取所有的超管账号
  553. let isSuperOk = state.isSupertube && ids.includes(String(state.userInfo.id)) || !state.isSupertube
  554. if(!isSuperOk){
  555. const _i = list.findIndex(item => {
  556. return item.menu_route === "sellOut"
  557. })
  558. const _s = list.findIndex(item => {
  559. return item.menu_route === "supplierSellOut"
  560. })
  561. if(_i >= 0){
  562. list[_i].child = list[_i].child.filter(item => {
  563. return item.menu_route !=="filing"
  564. })
  565. }
  566. if(_s >= 0){
  567. list[_s].child = list[_s].child.filter(item => {
  568. return item.menu_route !=="supplierFiling"
  569. })
  570. }
  571. }
  572. list = list.filter((item) => item.child && item.child.length > 0);
  573. list.forEach((v1) => {
  574. const { menu_route, child } = v1;
  575. if (menu_route === "bigScreen" && child.length) {
  576. isok = true;
  577. }
  578. let pItem = {
  579. name: v1.menu_route,
  580. path: `/${v1.menu_route}`,
  581. meta: {
  582. title: v1.menu_name,
  583. icon: v1.menu_img,
  584. },
  585. children: [],
  586. };
  587. v1.child.forEach((v2) => {
  588. let item = {
  589. path: v2.menu_route,
  590. name: v2.menu_route,
  591. hidden: parseInt(v2.is_display + "") === 0,
  592. meta: {
  593. title: v2.menu_name,
  594. icon: v2.menu_img,
  595. },
  596. url: v2.menu_url,
  597. };
  598. let model = {
  599. menu_route: v2.menu_route,
  600. action: v2.action,
  601. };
  602. btnList.push(model);
  603. pItem.children.push(item);
  604. });
  605. arrag.push(pItem);
  606. });
  607. }
  608. // const isHas = isHasRoute(arrag)
  609. // if(!isHas) router.push('/welcome/dashboard')
  610. return {
  611. mcode: code,
  612. mdata: {
  613. isok,
  614. arrag,
  615. btnList,
  616. },
  617. mmsg: message,
  618. };
  619. }
  620. function initRouter(arr) {
  621. const IndexRoute = [
  622. // 个人信息
  623. {
  624. path: "/person",
  625. component: () => import("@/layout/index"),
  626. redirect: "/person/my-message",
  627. hidden: true,
  628. alwaysShow: true,
  629. meta: {
  630. title: "个人中心",
  631. icon: "user",
  632. },
  633. children: [
  634. {
  635. path: "my-message",
  636. component: () => import("@/views/person-infor/my-message"),
  637. name: "MyMessage",
  638. alwaysShow: true,
  639. meta: { title: "我的信息", noCache: true, breadcrumb: true },
  640. },
  641. {
  642. path: "change-phone",
  643. component: () => import("@/views/person-infor/change-phone"),
  644. name: "ChangePhone",
  645. alwaysShow: true,
  646. meta: { title: "更换手机号", noCache: true, breadcrumb: true },
  647. },
  648. {
  649. path: "change-password",
  650. component: () => import("@/views/person-infor/change-password"),
  651. name: "ChangePassword",
  652. alwaysShow: true,
  653. meta: { title: "修改密码", noCache: true, breadcrumb: true },
  654. },
  655. {
  656. path: "/reload",
  657. component: () => import("@/views/reload/index"),
  658. },
  659. ],
  660. },
  661. //欢迎页
  662. {
  663. path: "/welcome",
  664. component: () => import("@/layout/index"),
  665. redirect: "/welcome/dashboard",
  666. children: [
  667. {
  668. path: "dashboard",
  669. component: () => import("@/views/dashboard/index"),
  670. name: "Dashboard",
  671. meta: { title: "首页", icon: "dashboard", affix: true },
  672. },
  673. ],
  674. },
  675. ];
  676. arr.map((v1) => {
  677. v1.component = (resolve) => require([`@/layout/index.vue`], resolve);
  678. if (v1.children && v1.children.length > 0) {
  679. v1.redirect = `/${v1.name}/${v1.children[0].name}`;
  680. v1.children.map((v2) => {
  681. v2.component = (resolve) => require([`@/views/${v2.url}.vue`], resolve);
  682. return v2;
  683. });
  684. }
  685. return v1;
  686. });
  687. IndexRoute.push(...arr);
  688. IndexRoute.push({
  689. path: "404",
  690. component: () => import("@/views/error-page/404"),
  691. hidden: false,
  692. noCache: true,
  693. });
  694. IndexRoute.push({
  695. path: "*",
  696. redirect: "/welcome/dashboard",
  697. });
  698. window.vm.$router.addRoutes(IndexRoute);
  699. }
  700. export default {
  701. namespaced: true,
  702. state,
  703. mutations,
  704. actions,
  705. };