user.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import {
  2. login,
  3. logout,
  4. userinfo,
  5. menuList,
  6. dingdingdetail,
  7. logoutx,
  8. } from "@/apis/user";
  9. import {
  10. getToken,
  11. setToken,
  12. removeToken,
  13. getMenu,
  14. setMenu,
  15. removeMenu,
  16. getBtn,
  17. setBtn,
  18. removeBtn,
  19. getRoles,
  20. setRoles,
  21. removeRoles,
  22. } from "@/utils/auth";
  23. const state = {
  24. token: getToken(),
  25. name: "",
  26. avatar: require("@/assets/sheji/user.png"),
  27. introduction: "",
  28. roles: getRoles() ? getRoles() : "edit",
  29. btnList: getBtn(),
  30. navList: getMenu(),
  31. };
  32. const mutations = {
  33. SET_TOKEN: (state, token) => {
  34. state.token = token;
  35. },
  36. SET_MENU: (state, menu) => {
  37. state.menu = menu;
  38. },
  39. SET_HAS: (state, has) => {
  40. state.has = has;
  41. },
  42. SET_INTRODUCTION: (state, introduction) => {
  43. state.introduction = introduction;
  44. },
  45. SET_NAME: (state, name) => {
  46. state.name = name;
  47. },
  48. SET_AVATAR: (state, avatar) => {
  49. state.avatar = avatar;
  50. },
  51. SET_ROLES: (state, roles) => {
  52. state.roles = roles;
  53. },
  54. navListFn(state, val) {
  55. if (!val) {
  56. val = [];
  57. }
  58. state.navList = val;
  59. setMenu(val);
  60. },
  61. btnListFn(state, val) {
  62. if (!val) {
  63. val = [];
  64. }
  65. state.btnList = val;
  66. setBtn(val);
  67. },
  68. };
  69. const actions = {
  70. // user login
  71. login({ commit }, userInfo) {
  72. const { username, password } = userInfo;
  73. return new Promise((resolve, reject) => {
  74. login({ username: username.trim(), password: password })
  75. .then((response) => {
  76. const { data } = response;
  77. if (response.code === 0) {
  78. let ROLES = username === "admin" ? "admin" : "edit";
  79. commit("SET_TOKEN", data.token);
  80. commit("SET_ROLES", ROLES);
  81. setRoles(ROLES);
  82. setToken(data.token);
  83. }
  84. resolve(response);
  85. })
  86. .catch((error) => {
  87. reject(error);
  88. });
  89. });
  90. },
  91. dingUserInfor({ commit }, code) {
  92. return new Promise(async (resolve, reject) => {
  93. dingdingdetail(code)
  94. .then((response) => {
  95. const { data } = response;
  96. if (response.code === 0) {
  97. commit("SET_TOKEN", data.token);
  98. setToken(data.token);
  99. resolve("success");
  100. } else {
  101. resolve("error");
  102. }
  103. })
  104. .catch((error) => {
  105. reject(error);
  106. });
  107. });
  108. },
  109. async getMenuList({ commit, that }) {
  110. return new Promise(async (resolve, reject) => {
  111. if (!getToken()) {
  112. resolve("noToken");
  113. } else {
  114. const { code, data, message } = await menuList({});
  115. if (code === 0) {
  116. let list = data;
  117. list = list.filter((item) => item.child && item.child.length > 0);
  118. let arrag = [];
  119. let btnList = [];
  120. console.log(list);
  121. list.forEach((v1) => {
  122. // if(v1.menu_route!=="system" )
  123. let pItem = {
  124. name: v1.menu_route,
  125. path: `/${v1.menu_route}`,
  126. meta: {
  127. title: v1.menu_name,
  128. icon: v1.menu_img,
  129. },
  130. children: [],
  131. };
  132. v1.child.forEach((v2) => {
  133. if (v2.menu_route === "menuOperator") {
  134. if (getRoles() === "admin") {
  135. let item = {
  136. path: v2.menu_route,
  137. name: v2.menu_route,
  138. meta: {
  139. title: v2.menu_name,
  140. icon: v2.menu_img,
  141. },
  142. url: v2.menu_url,
  143. };
  144. let model = {
  145. menu_route: v2.menu_route,
  146. action: v2.action,
  147. };
  148. btnList.push(model);
  149. pItem.children.push(item);
  150. }
  151. } else {
  152. let item = {
  153. path: v2.menu_route,
  154. name: v2.menu_route,
  155. meta: {
  156. title: v2.menu_name,
  157. icon: v2.menu_img,
  158. },
  159. url: v2.menu_url,
  160. };
  161. let model = {
  162. menu_route: v2.menu_route,
  163. action: v2.action,
  164. };
  165. btnList.push(model);
  166. pItem.children.push(item);
  167. }
  168. });
  169. arrag.push(pItem);
  170. });
  171. console.log(arrag);
  172. // 生成页面权限
  173. commit("navListFn", arrag);
  174. // 生成按钮权限
  175. commit("btnListFn", btnList);
  176. // 生成路由
  177. initRouter(arrag, that);
  178. // state.commit("loginInfoCommit", res.data);
  179. resolve("success");
  180. } else if (code === 101 || code === 102) {
  181. resolve("noToken");
  182. } else {
  183. resolve({ code, data, message });
  184. }
  185. }
  186. });
  187. },
  188. // user logout
  189. async logout({ commit, state, dispatch }) {
  190. return new Promise(async (resolve, reject) => {
  191. if (!getToken()) {
  192. await resetAll({ commit, dispatch });
  193. resolve();
  194. } else {
  195. const { code, data, message } = await logoutx({ token: getToken() });
  196. if (code === 0) {
  197. await resetAll({ commit, dispatch });
  198. resolve();
  199. } else {
  200. await resetAll({ commit, dispatch });
  201. resolve();
  202. }
  203. }
  204. });
  205. },
  206. // remove token
  207. resetToken({ commit }) {
  208. return new Promise((resolve) => {
  209. commit("SET_TOKEN", "");
  210. commit("SET_ROLES", "");
  211. removeToken();
  212. resolve();
  213. removeRoles();
  214. });
  215. },
  216. };
  217. async function resetAll({ commit, dispatch }) {
  218. return new Promise((resolve) => {
  219. commit("SET_TOKEN", "");
  220. commit("SET_ROLES", "");
  221. commit("SET_MENU", "");
  222. commit("SET_HAS", "");
  223. removeMenu();
  224. removeBtn();
  225. removeToken();
  226. removeRoles();
  227. // reset visited views and cached views
  228. // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
  229. dispatch("tagsView/delAllViews", null, { root: true });
  230. resolve();
  231. });
  232. }
  233. function initRouter(arr) {
  234. const IndexRoute = [
  235. //欢迎页
  236. {
  237. path: "/welcome",
  238. component: () => import("@/layout/index"),
  239. redirect: "/welcome/dashboard",
  240. children: [
  241. {
  242. path: "dashboard",
  243. component: () => import("@/views/dashboard/index"),
  244. name: "Dashboard",
  245. meta: { title: "首页", icon: "dashboard", affix: true },
  246. },
  247. ],
  248. },
  249. ];
  250. arr.map((v1) => {
  251. v1.component = (resolve) => require([`@/layout/index.vue`], resolve);
  252. if (v1.children && v1.children.length > 0) {
  253. v1.redirect = `/${v1.name}/${v1.children[0].name}`;
  254. v1.children.map((v2) => {
  255. v2.component = (resolve) => require([`@/views/${v2.url}.vue`], resolve);
  256. return v2;
  257. });
  258. }
  259. return v1;
  260. });
  261. IndexRoute.push(...arr);
  262. IndexRoute.push({
  263. path: "404",
  264. component: () => import("@/views/error-page/404"),
  265. hidden: false,
  266. noCache: true,
  267. });
  268. IndexRoute.push({
  269. path: "*",
  270. redirect: "/404",
  271. });
  272. window.vm.$router.addRoutes(IndexRoute);
  273. }
  274. export default {
  275. namespaced: true,
  276. state,
  277. mutations,
  278. actions,
  279. };