user.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /** @format */
  2. import config from "@/config";
  3. import storage from "@/utils/storage";
  4. import constant from "@/utils/constant";
  5. import { login, logout, getInfo } from "@/api/login";
  6. import { getToken, setToken, removeToken } from "@/utils/auth";
  7. const baseUrl = config.baseUrl;
  8. const user = {
  9. state: {
  10. token: getToken(),
  11. name: storage.get(constant.name),
  12. avatar: storage.get(constant.avatar),
  13. roles: storage.get(constant.roles),
  14. permissions: storage.get(constant.permissions),
  15. },
  16. mutations: {
  17. SET_TOKEN: (state, token) => {
  18. state.token = token;
  19. },
  20. SET_NAME: (state, name) => {
  21. state.name = name;
  22. storage.set(constant.name, name);
  23. },
  24. SET_AVATAR: (state, avatar) => {
  25. state.avatar = avatar;
  26. storage.set(constant.avatar, avatar);
  27. },
  28. SET_ROLES: (state, roles) => {
  29. state.roles = roles;
  30. storage.set(constant.roles, roles);
  31. },
  32. SET_PERMISSIONS: (state, permissions) => {
  33. state.permissions = permissions;
  34. storage.set(constant.permissions, permissions);
  35. },
  36. },
  37. actions: {
  38. // 登录
  39. Login({ commit }, userInfo) {
  40. const username = userInfo.username.trim();
  41. const password = userInfo.password;
  42. const code = userInfo.code;
  43. const uuid = userInfo.uuid;
  44. return new Promise((resolve, reject) => {
  45. login(username, password, code, uuid)
  46. .then((res) => {
  47. setToken(res.token);
  48. commit("SET_TOKEN", res.token);
  49. resolve();
  50. })
  51. .catch((error) => {
  52. reject(error);
  53. });
  54. });
  55. },
  56. // 获取用户信息
  57. GetInfo({ commit, state }) {
  58. return new Promise((resolve, reject) => {
  59. getInfo()
  60. .then(({ data }) => {
  61. const user = data;
  62. // const avatar =
  63. // user == null || user.avatar == "" || user.avatar == null
  64. // ? require("@/static/images/profile.jpg")
  65. // : baseUrl + user.avatar;
  66. // const username =
  67. // user == null || user.userName == "" || user.userName == null
  68. // ? ""
  69. // : user.userName;
  70. // if (res.roles && res.roles.length > 0) {
  71. // commit("SET_ROLES", res.roles);
  72. // commit("SET_PERMISSIONS", res.permissions);
  73. // } else {
  74. // commit("SET_ROLES", ["ROLE_DEFAULT"]);
  75. // }
  76. // commit("SET_NAME", username);
  77. // commit("SET_AVATAR", avatar);
  78. uni.setStorageSync("userInfo", JSON.stringify(user));
  79. resolve(user);
  80. })
  81. .catch((error) => {
  82. reject(error);
  83. });
  84. });
  85. },
  86. // 退出系统
  87. LogOut({ commit, state }) {
  88. return new Promise((resolve, reject) => {
  89. logout(state.token)
  90. .then(() => {
  91. commit("SET_TOKEN", "");
  92. commit("SET_ROLES", []);
  93. commit("SET_PERMISSIONS", []);
  94. removeToken();
  95. storage.clean();
  96. resolve();
  97. })
  98. .catch((error) => {
  99. reject(error);
  100. });
  101. });
  102. },
  103. },
  104. };
  105. export default user;