123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766 |
- import router from "@/router";
- import {
- login,
- menuList,
- dingdingdetail,
- usergetinfo,
- roleProcessInfo,
- requsetSupertubeCompany,
- requsetUserBindingCompany,
- reuqsetRoleInfo,
- userlist,
- } from "@/apis/user";
- import {
- getToken,
- setToken,
- removeToken,
- getMenu,
- setMenu,
- removeMenu,
- getBtn,
- setBtn,
- removeBtn,
- get_business_company,
- set_business_company,
- remove_business_company,
- get_business_companyNo,
- set_business_companyNo,
- remove_business_companyNo,
- get_private_field,
- set_private_field,
- remove_private_field,
- get_action_data,
- set_action_data,
- remove_action_data,
- get_role_level,
- getUserCompany,
- setUserCompany,
- removeUserCompany,
- setUserInfo as _setUserInfo,
- getUserInfo as _getUserInfo,
- removeUserInfo as _removeUserInfo,
- getCompanyType as _getCompanyType,
- setCompanyType as _setCompanyType,
- removeCompanyType as _removeCompanyType,
- } from "@/utils/auth";
- import { MessageBox } from "element-ui";
- import { convertCompanylist } from "@/utils";
- export const userStoreActions = {
- reloadMenu: "user/reloadMenulist",
- loginByToken: "user/loginByToken",
- setCompanyList: "user/setCompanylist",
- setCurrentLevel: "user/setCurrentLevel",
- setCurrentCompany: "user/setCurrentCompany",
- };
- const ids = ["36", "37", "44", "109"];
- const tearchChenId = "37"
- const state = {
- token: getToken(),
- name: "",
- avatar: require("@/assets/img/avatar.gif"),
- introduction: "",
- roles: ["admin"],
- btnList: getBtn(),
- navList: getMenu(),
- role_level: get_role_level() || "1",
- business_company: get_business_company() || "",
- business_companyNo: get_business_companyNo() || "",
- private_field: get_private_field() || [],
- roleProcess: get_action_data() || [],
- currentCompany: getUserCompany() ? getUserCompany().code : "",
- userInfo: _getUserInfo(),
- isSupertube: false,
- isTeacherChen: false,
- is_super_admin_ids: false,
- companylist: [],
- originLevel: "0",
- currentLevel: "0",
- };
- const companyTypes = {
- business: "business",
- supplier: "supplier",
- };
- const mutations = {
- SET_TOKEN: (state, token) => {
- state.token = token;
- },
- SET_MENU: (state, menu) => {
- state.menu = menu;
- },
- SET_HAS: (state, has) => {
- state.has = has;
- },
- SET_INTRODUCTION: (state, introduction) => {
- state.introduction = introduction;
- },
- SET_NAME: (state, name) => {
- state.name = name;
- },
- SET_AVATAR: (state, avatar) => {
- state.avatar = avatar;
- },
- SET_IS_SUPER_ADMIN_IDS: (state, is) => {
- state.is_super_admin_ids = is;
- },
- setIsTearcherChen(state, current){
- state.isTeacherChen = current
- },
- SET_ROLES: (state, roles) => {
- state.roles = roles;
- },
- navListFn(state, val) {
- if (!val) {
- val = [];
- }
- state.navList = val;
- setMenu(val);
- },
- btnListFn(state, val) {
- if (!val) {
- val = [];
- }
- state.btnList = val;
- setBtn(val);
- },
- setSupplier(state, val) {
- state.isSupplier = val;
- },
- business_company(state, val) {
- state.business_company = val;
- set_business_company(val);
- },
- business_companyNo(state, val) {
- state.business_companyNo = val;
- set_business_companyNo(val);
- },
- roleProcess(state, val) {
- state.roleProcess = val;
- set_action_data(val);
- },
- private_field(state, val) {
- state.private_field = val;
- set_private_field(val);
- },
- setCurrentLevel(state, val) {
- state.currentLevel = val;
- },
- setOriginLevel(state, val) {
- state.originLevel = val;
- },
- setUserInfo(state, info) {
- state.userInfo = info;
- _setUserInfo(info);
- },
- setCurrentCompany(state, code) {
- //获取当前公司详细信息
- const company = state.companylist.find(({ code: _code }) => _code === code);
- setUserCompany(company);
- state.currentCompany = code;
- },
- setCompanyType(state, code) {
- const isBusiness = code.indexOf("GS") >= 0;
- state.companyType = isBusiness
- ? companyTypes.business
- : companyTypes.supplier;
- },
- setCompanylist(state, val) {
- state.companylist = val;
- },
- setSupertube(state, val) {
- state.isSupertube = val;
- },
- };
- const actions = {
- // user login
- login({ commit }, userInfo) {
- const { username, password } = userInfo;
- return new Promise((resolve, reject) => {
- login({ username: username.trim(), password: password })
- .then((response) => {
- const { code, data } = response;
- if (code === 0) {
- const { token, nickname } = data;
- commit("SET_TOKEN", token);
- commit("SET_NAME", nickname);
- setToken(token);
- }
- resolve(response);
- })
- .catch((error) => {
- reject(error);
- });
- });
- },
- //公司角色异常
- disabledCompany({ commit, that }) {
- commit("navListFn", []);
- commit("btnListFn", []);
- initRouter([], that);
- const company = getUserCompany();
- MessageBox.alert(
- `公司 ${company.name} 角色异常或该公司禁用,请检查账号或者角色设置`,
- "公司角色异常",
- { type: "warning", confirmButtonText: "前往首页" }
- )
- .then(() => {
- router.push("/reload?redirect=welcome/dashboard");
- })
- .catch(() => {
- router.push("/reload?redirect=welcome/dashboard");
- });
- },
- dingUserInfor({ commit }, code) {
- return new Promise(async (resolve, reject) => {
- dingdingdetail(code)
- .then((response) => {
- const { data } = response;
- if (response.code === 0) {
- commit("SET_TOKEN", data.token);
- setToken(data.token);
- resolve("success");
- } else {
- resolve("error");
- }
- })
- .catch((error) => {
- reject(error);
- });
- });
- },
- async loginByToken({ commit, dispatch }, token) {
- setToken(token);
- await commit("SET_TOKEN");
- await dispatch("getMenuList");
- },
- // 切换公司后重新请求
- async reloadMenulist({ commit, dispatch, that, state }, level) {
- return new Promise(async (resolve) => {
- const company = getUserCompany();
- dispatch("getRoleInfo", company);
- //获取菜单
- const { mcode, mdata, message: mmsg } = await getRoleMenuList(level);
- //账号禁用
- if (mcode === 10001) {
- commit("navListFn", []);
- commit("btnListFn", []);
- initRouter([], that);
- resolve("disabled");
- return;
- }
- if (mcode >= 100 && mcode <= 104) {
- resolve("noToken");
- } else if (mcode !== 0) {
- resolve({ code: mcode, data: {}, massage: mmsg });
- } else {
- const { arrag, isok, btnList } = mdata;
- // 生成页面权限
- commit("navListFn", arrag);
- // 生成按钮权限
- commit("btnListFn", btnList);
- // 生成路由
- initRouter(arrag, that);
- resolve(isok ? "success-dataV" : "success");
- }
- });
- },
- async getRoleInfo({ commit, that, dispatch }, _current_company_) {
- return new Promise(async (resolve) => {
- //查找默认的业务公司 设置到全局的currentCompany
- const { roleid } = _current_company_;
- //根据角色获取流程节点权限
- const { pcode, pdata, pmsg } = await getRoleProcess(roleid);
- if (pcode === 0) {
- commit("roleProcess", pdata ?? []);
- }
- if (pcode >= 100 && pcode <= 104) {
- resolve("noToken");
- return;
- }
- //根据角色获取角色详情 获取关键字字段
- const {
- code: rcode,
- data: rdata,
- message: rmsg,
- } = await reuqsetRoleInfo({ roleid: roleid });
- if (rcode === 0) {
- commit(
- "private_field",
- rdata.private_field ? rdata.private_field.split(",") : []
- );
- }
- if (rcode >= 100 && rcode <= 104) {
- resolve("noToken");
- return;
- }
- });
- },
- async getMenuList({ commit, that, dispatch }) {
- return new Promise(async (resolve) => {
- if (!getToken()) {
- resolve("noToken");
- } else {
- const { ucode, udata, umsg } = await getUserInfo();
- if (ucode >= 100 && ucode <= 104) {
- resolve("noToken");
- } else if (ucode !== 0) {
- resolve({ code: ucode, data: udata, massage: umsg });
- } else {
- const { level, id } = udata;
- //是否超管
- const isSupertube = level === "1";
- //是否供应商
- const isSupplier = level === "3";
- //保存用户信息
- commit("setUserInfo", udata);
- // const
- const isOkIndex = ids.findIndex((s) => String(s) === String(id));
- commit("SET_IS_SUPER_ADMIN_IDS", isOkIndex !== -1);
- commit("setIsTearcherChen", String(id) === tearchChenId)
- //保存是否超管
- commit("setSupplier", isSupplier);
- //保存是否超管用户
- commit("setSupertube", isSupertube);
- //设置原始level和当前level
- commit("setOriginLevel", level);
- commit("setCurrentLevel", level);
- //获取公司列表(区分超管和普通账户api)
- const api = isSupertube
- ? requsetSupertubeCompany
- : requsetUserBindingCompany;
- //获取缓存里的业务公司 => 公司名称搜索 => 存在设置为默认公司
- const { data: companyData, code: companyCode } = await api({
- ...(!isSupertube ? { size: 10000 } : { type: "1" }),
- ...{ token: getToken() },
- });
- //保存公司列表 普通用户和超管使用两个列表需要转换
- let _companylist = [];
- if (companyCode === 0) {
- _companylist = isSupertube
- ? companyData.list
- : convertCompanylist(companyData.list);
- //超管 + 业务公司 需要将 relation_code 设置为 code
- // if(isSupertube) _companylist = _companylist.map(item => ({...item, code: item.relation_code}))
- commit("setCompanylist", _companylist);
- }
- if (companyCode >= 100 && companyCode <= 104) {
- resolve("noToken");
- return;
- }
- //非超管用户获取保存 流程权限 & 关键字字段 & 设置默认业务公司
- let defaultCompany = "";
- if (!isSupertube) {
- //查找默认的业务公司 设置到全局的currentCompany
- defaultCompany = _companylist.find(
- ({ is_main }) => is_main === "1"
- );
- const result = dispatch("getRoleInfo", defaultCompany);
- if (result === "noToken") {
- resolve("noToken");
- return;
- }
- }
- //保存默认公司
- commit(
- "setCurrentCompany",
- defaultCompany ? defaultCompany.code : ""
- );
- const { mcode, mdata, mmsg } = await getRoleMenuList();
- //拦截账号异常: 禁用
- if (mcode === 10001) {
- dispatch("disabledCompany");
- return;
- }
- if (mcode >= 100 && mcode <= 104) {
- resolve("noToken");
- } else if (mcode !== 0) {
- resolve({ code: mcode, data: {}, massage: mmsg });
- } else {
- const { arrag, isok, btnList } = mdata;
- // 生成页面权限
- commit("navListFn", arrag);
- // 生成按钮权限
- commit("btnListFn", btnList);
- // 生成路由
- initRouter(arrag, that);
- resolve(isok ? "success-dataV" : "success");
- }
- }
- }
- });
- },
- // user logout
- logout({ commit, state, dispatch }) {
- return new Promise((resolve, reject) => {
- commit("SET_TOKEN", "");
- commit("SET_ROLES", []);
- commit("SET_MENU", "");
- commit("SET_HAS", "");
- _removeUserInfo();
- removeBtn();
- removeMenu();
- removeToken();
- remove_business_company();
- commit("business_company", "");
- commit("business_companyNo", "");
- remove_business_companyNo();
- remove_private_field();
- commit("private_field", []);
- remove_action_data();
- commit("action_data", []);
- removeUserCompany();
- commit("action_data", []);
- // removeMainList()
- // reset visited views and cached views
- // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
- dispatch("tagsView/delAllViews", null, { root: true });
- resolve();
- });
- },
- // remove token
- resetToken({ commit }) {
- return new Promise((resolve) => {
- commit("SET_TOKEN", "");
- commit("SET_ROLES", []);
- removeToken();
- resolve();
- });
- },
- };
- async function getUserInfo() {
- const { code, data, meaasge } = await usergetinfo({
- token: getToken(),
- });
- return {
- ucode: code,
- udata: data,
- umsg: meaasge,
- };
- }
- async function getRoleProcess(roleid) {
- const { code, data, meaasge } = await roleProcessInfo({ roleid });
- let list = data;
- list.map((si) => {
- si.action = [];
- if (si.child && si.child.length > 0) {
- si.child.map((sii) => {
- si.action.push(sii.order_process + "");
- return sii;
- });
- }
- return si;
- });
- return {
- pcode: code,
- pdata: list,
- pmsg: meaasge,
- };
- }
- // function isHasRoute(routes){
- // let isHas = false
- // const chunks = location.hash.split('/')
- // const currentPath = chunks[chunks.length - 1]
- // const dfs = (routes) => {
- // routes.forEach(route => {
- // if(route.path === currentPath) isHas = true;
- // if(route.children && route.children.length > 0) dfs(route.children)
- // })
- // }
- // dfs([
- // {
- // path: "/person",
- // component: () => import("@/layout/index"),
- // redirect: "/person/my-message",
- // hidden: true,
- // alwaysShow: true,
- // meta: {
- // title: "个人中心",
- // icon: "user",
- // },
- // children: [
- // {
- // path: "my-message",
- // component: () => import("@/views/person-infor/my-message"),
- // name: "MyMessage",
- // alwaysShow: true,
- // meta: { title: "我的信息", noCache: true, breadcrumb: true },
- // },
- // {
- // path: "change-phone",
- // component: () => import("@/views/person-infor/change-phone"),
- // name: "ChangePhone",
- // alwaysShow: true,
- // meta: { title: "更换手机号", noCache: true, breadcrumb: true },
- // },
- // {
- // path: "change-password",
- // component: () => import("@/views/person-infor/change-password"),
- // name: "ChangePassword",
- // alwaysShow: true,
- // meta: { title: "修改密码", noCache: true, breadcrumb: true },
- // },
- // {
- // path:'/reload',
- // component:() => import('@/views/reload/index')
- // }
- // ],
- // },
- // //欢迎页
- // {
- // path: "/welcome",
- // component: () => import("@/layout/index"),
- // redirect: "/welcome/dashboard",
- // children: [
- // {
- // path: "dashboard",
- // component: () => import("@/views/dashboard/index"),
- // name: "Dashboard",
- // meta: { title: "首页", icon: "dashboard", affix: true },
- // },
- // ],
- // },...routes])
- // return isHas
- // }
- // 区分供应商和业务公司获取不同的菜单列表
- async function getRoleMenuList(level = "") {
- const { code, data, message } = await menuList({
- ...(level && level !== "1" ? { level } : {}),
- });
- //menu接受账户异常:禁用 并返回
- if (message === "账户已禁用" || message === "没有该公司的默认角色") {
- return {
- mdata: {},
- mmsg: "账号被禁用",
- mcode: 10001,
- };
- }
- let arrag = [];
- let btnList = [];
- let list = [];
- let isok = false;
- if (code === 0) {
- // const { id } = _getUserInfo();
- // const isOkIndex = ids.findIndex((s) => String(s) === String(id));
- const idok = state.is_super_admin_ids;
- if (!idok) {
- console.log(data)
- list = data.filter(function (item) {
- return item.menu_route !== "system"
- });
- } else {
- list = data ?? [];
- }
- //获取所有的超管账号
- let isSuperOk = state.isSupertube && ids.includes(String(state.userInfo.id)) || !state.isSupertube
- if(!isSuperOk){
- const _i = list.findIndex(item => {
- return item.menu_route === "sellOut"
- })
- const _s = list.findIndex(item => {
- return item.menu_route === "supplierSellOut"
- })
- if(_i >= 0){
- list[_i].child = list[_i].child.filter(item => {
- return item.menu_route !=="filing"
- })
- }
- if(_s >= 0){
- list[_s].child = list[_s].child.filter(item => {
- return item.menu_route !=="supplierFiling"
- })
- }
- }
- list = list.filter((item) => item.child && item.child.length > 0);
- list.forEach((v1) => {
- const { menu_route, child } = v1;
- if (menu_route === "bigScreen" && child.length) {
- isok = true;
- }
- let pItem = {
- name: v1.menu_route,
- path: `/${v1.menu_route}`,
- meta: {
- title: v1.menu_name,
- icon: v1.menu_img,
- },
- children: [],
- };
- v1.child.forEach((v2) => {
- let item = {
- path: v2.menu_route,
- name: v2.menu_route,
- hidden: parseInt(v2.is_display + "") === 0,
- meta: {
- title: v2.menu_name,
- icon: v2.menu_img,
- },
- url: v2.menu_url,
- };
- let model = {
- menu_route: v2.menu_route,
- action: v2.action,
- };
- btnList.push(model);
- pItem.children.push(item);
- });
- arrag.push(pItem);
- });
- }
- // const isHas = isHasRoute(arrag)
- // if(!isHas) router.push('/welcome/dashboard')
- return {
- mcode: code,
- mdata: {
- isok,
- arrag,
- btnList,
- },
- mmsg: message,
- };
- }
- function initRouter(arr) {
- const IndexRoute = [
- // 个人信息
- {
- path: "/person",
- component: () => import("@/layout/index"),
- redirect: "/person/my-message",
- hidden: true,
- alwaysShow: true,
- meta: {
- title: "个人中心",
- icon: "user",
- },
- children: [
- {
- path: "my-message",
- component: () => import("@/views/person-infor/my-message"),
- name: "MyMessage",
- alwaysShow: true,
- meta: { title: "我的信息", noCache: true, breadcrumb: true },
- },
- {
- path: "change-phone",
- component: () => import("@/views/person-infor/change-phone"),
- name: "ChangePhone",
- alwaysShow: true,
- meta: { title: "更换手机号", noCache: true, breadcrumb: true },
- },
- {
- path: "change-password",
- component: () => import("@/views/person-infor/change-password"),
- name: "ChangePassword",
- alwaysShow: true,
- meta: { title: "修改密码", noCache: true, breadcrumb: true },
- },
- {
- path: "/reload",
- component: () => import("@/views/reload/index"),
- },
- ],
- },
- //欢迎页
- {
- path: "/welcome",
- component: () => import("@/layout/index"),
- redirect: "/welcome/dashboard",
- children: [
- {
- path: "dashboard",
- component: () => import("@/views/dashboard/index"),
- name: "Dashboard",
- meta: { title: "首页", icon: "dashboard", affix: true },
- },
- ],
- },
- ];
- arr.map((v1) => {
- v1.component = (resolve) => require([`@/layout/index.vue`], resolve);
- if (v1.children && v1.children.length > 0) {
- v1.redirect = `/${v1.name}/${v1.children[0].name}`;
- v1.children.map((v2) => {
- v2.component = (resolve) => require([`@/views/${v2.url}.vue`], resolve);
- return v2;
- });
- }
- return v1;
- });
- IndexRoute.push(...arr);
- IndexRoute.push({
- path: "404",
- component: () => import("@/views/error-page/404"),
- hidden: false,
- noCache: true,
- });
- IndexRoute.push({
- path: "*",
- redirect: "/welcome/dashboard",
- });
- window.vm.$router.addRoutes(IndexRoute);
- }
- export default {
- namespaced: true,
- state,
- mutations,
- actions,
- };
|