user.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. export default {
  2. // 开启命名空间
  3. namespaced: true,
  4. // state 数据
  5. state: () => ({
  6. // 登录成功之后的 token 字符串
  7. token: uni.getStorageSync('token') || '',
  8. // 用户的基本信息
  9. userinfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'),
  10. // 老师认证结果
  11. authentication: uni.getStorageInfoSync('authentication') || '',
  12. }),
  13. // 方法
  14. mutations: {
  15. // 更新用户的基本信息
  16. updateUserInfo(state, userinfo) {
  17. state.userinfo = userinfo
  18. // 通过 this.commit() 方法,调用 m_user 模块下的 saveUserInfoToStorage 方法,将 userinfo 对象持久化存储到本地
  19. this.commit('m_user/saveUserInfoToStorage')
  20. },
  21. // 将 userinfo 持久化存储到本地
  22. saveUserInfoToStorage(state) {
  23. uni.setStorageSync('userinfo', JSON.stringify(state.userinfo))
  24. },
  25. // 更新 token 字符串
  26. updateToken(state, token) {
  27. state.token = token
  28. // 通过 this.commit() 方法,调用 m_user 模块下的 saveTokenToStorage 方法,将 token 字符串持久化存储到本地
  29. this.commit('m_user/saveTokenToStorage')
  30. },
  31. // 将 token 字符串持久化存储到本地
  32. saveTokenToStorage(state) {
  33. uni.setStorageSync('token', state.token)
  34. },
  35. // 更新老师认证结果
  36. updateAuthentication(state, message) {
  37. state.authentication = message
  38. this.commit('m_user/saveAuthentication')
  39. },
  40. // 将老师认证结果存储到本地
  41. saveAuthentication(state) {
  42. uni.setStorageSync('authentication', state.authentication)
  43. }
  44. },
  45. // 数据包装器
  46. getters: {
  47. // 收货详细地址的计算属性
  48. addstr(state) {
  49. if (!state.address.provinceName) return ''
  50. // 拼接 省,市,区,详细地址 的字符串并返回给用户
  51. return state.address.provinceName + state.address.cityName +
  52. state.address.countyName + state.address.detailInfo
  53. }
  54. },
  55. }