index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // store/index.js (Vue 3)
  2. import { createStore } from 'vuex'
  3. import storage from '@/utils/storage'
  4. const store = createStore({
  5. state: {
  6. isShowToast: false, // 是否在展示Toast中
  7. remark: [], // 填写订单备注
  8. shareLink: "", // 分享链接
  9. verificationKey: "", // 获取key表示验证通过
  10. distributionId: "", // 分销员Id
  11. hasLogin: false,
  12. userInfo: {},
  13. uuid: "",
  14. token: "",
  15. userName: "",
  16. },
  17. mutations: {
  18. // 初始化状态(从存储中恢复)
  19. initState(state) {
  20. state.hasLogin = storage.getHasLogin() || false;
  21. state.userInfo = storage.getUserInfo() || {};
  22. state.uuid = storage.getUuid() || "";
  23. if (state.hasLogin && state.userInfo) {
  24. state.userName = state.userInfo.Name || state.userInfo.Nickname || state.userInfo.Username || "匿名用户";
  25. }
  26. },
  27. login(state, userInfo) {
  28. state.userInfo = userInfo || {};
  29. state.userName =
  30. userInfo.Name || userInfo.Nickname || userInfo.Username || "匿名用户";
  31. state.hasLogin = true;
  32. },
  33. logout(state) {
  34. state.userName = "";
  35. state.hasLogin = false;
  36. state.userInfo = {};
  37. },
  38. // 设置填写订单中备注
  39. setRemark(state, remark) {
  40. state.remark = remark;
  41. },
  42. },
  43. actions: {
  44. // 初始化 store(在应用启动后调用)
  45. init({ commit }) {
  46. commit('initState');
  47. },
  48. },
  49. })
  50. export default store