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.

84 lines
1.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
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({state,commit}, data) {
  16. commit('setStateAttr', {
  17. key: 'userInfo',
  18. val: data
  19. })
  20. uni.setStorageSync('userInfo', data);
  21. state.token = data.token
  22. uni.setStorageSync('token', data.token);
  23. },
  24. // 登录过期 重新登录
  25. reLogin({commit}, info) {
  26. commit('setStateAttr', {
  27. key: 'token',
  28. val: ''
  29. })
  30. uni.setStorageSync("token", '');
  31. //过期跳转登录页重新登录
  32. router.push({
  33. path: '/pages/login/login'
  34. });
  35. },
  36. //预留,已将token更新整合到setUserData,后面可能有单独更新token的必要
  37. setUserToken({commit},token){
  38. commit('setStateAttr', {
  39. key: 'token',
  40. val: token
  41. })
  42. uni.setStorageSync("token", token);
  43. },
  44. //清空登录信息
  45. logoutUser({commit}){
  46. commit('setStateAttr', {
  47. key: 'token',
  48. val: ''
  49. })
  50. uni.setStorageSync("token", '');
  51. uni.setStorageSync("userInfo", '');
  52. // router.push({
  53. // path: '/pages/index/index'
  54. // });
  55. },
  56. }
  57. export const mutations = {
  58. //更新state数据
  59. setStateAttr(state, param) {
  60. if (param instanceof Array) {
  61. for (let item of param) {
  62. state[item.key] = item.val;
  63. }
  64. } else {
  65. state[param.key] = param.val;
  66. }
  67. }
  68. }
  69. export const getters = {
  70. // 用户是否登录
  71. hasLogin: state => {
  72. if (state.token) {
  73. return true;
  74. } else {
  75. return false
  76. }
  77. }
  78. }