uni-events-helper-wx
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.2 KiB

3 years ago
  1. // 用户数据模块
  2. import {router} from '@/nxTemp/router/index.js'
  3. const TOKEN = uni.getStorageSync("token") || "";
  4. const OPENID = uni.getStorageSync("openId") || "";
  5. const USER_INFO = uni.getStorageSync("userInfo") || {};
  6. export const state = {
  7. // 前端token
  8. token: TOKEN,
  9. // 用户openid
  10. openId: OPENID,
  11. // 用户信息 头像 昵称
  12. userInfo: USER_INFO
  13. }
  14. export const actions = {
  15. async setUserData({
  16. state,
  17. commit
  18. }, data) {
  19. commit('setStateAttr', {
  20. key: 'userInfo',
  21. val: data
  22. })
  23. uni.setStorageSync('userInfo', data);
  24. },
  25. // 登录过期 重新登录
  26. reLogin({
  27. commit
  28. }, info) {
  29. commit('setStateAttr', {
  30. key: 'token',
  31. val: ''
  32. })
  33. uni.setStorageSync("token", '');
  34. //过期跳转登录页重新登录
  35. router.push({
  36. path: '/pages/login/login'
  37. });
  38. }
  39. }
  40. export const mutations = {
  41. //更新state数据
  42. setStateAttr(state, param) {
  43. if (param instanceof Array) {
  44. for (let item of param) {
  45. state[item.key] = item.val;
  46. }
  47. } else {
  48. state[param.key] = param.val;
  49. }
  50. }
  51. }
  52. export const getters = {
  53. // 用户是否登录
  54. hasLogin: state => {
  55. if (state.token) {
  56. return true;
  57. } else {
  58. return false
  59. }
  60. }
  61. }