user.js 3.4 KB

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