user.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. location: uni.getStorageSync('location') || '{}',
  14. // 老师认证图片
  15. authorizePhoto: uni.getStorageSync('authorizePhoto') || '{}'
  16. }),
  17. // 方法
  18. mutations: {
  19. // 更新用户的基本信息
  20. updateUserInfo(state, userinfo) {
  21. state.userinfo = userinfo
  22. // 通过 this.commit() 方法,调用 m_user 模块下的 saveUserInfoToStorage 方法,将 userinfo 对象持久化存储到本地
  23. this.commit('m_user/saveUserInfoToStorage')
  24. },
  25. // 将 userinfo 持久化存储到本地
  26. saveUserInfoToStorage(state) {
  27. uni.setStorageSync('userinfo', JSON.stringify(state.userinfo))
  28. },
  29. // 更新 token 字符串
  30. updateToken(state, token) {
  31. state.token = token
  32. // 通过 this.commit() 方法,调用 m_user 模块下的 saveTokenToStorage 方法,将 token 字符串持久化存储到本地
  33. this.commit('m_user/saveTokenToStorage')
  34. },
  35. // 将 token 字符串持久化存储到本地
  36. saveTokenToStorage(state) {
  37. uni.setStorageSync('token', state.token)
  38. },
  39. // 更新老师认证结果
  40. updateAuthentication(state, message) {
  41. state.authentication = message
  42. this.commit('m_user/saveAuthentication')
  43. },
  44. // 将老师认证结果存储到本地
  45. saveAuthentication(state) {
  46. uni.setStorageSync('authentication', state.authentication)
  47. },
  48. // 更新经纬度
  49. updateLocation(state, location) {
  50. state.location = location
  51. this.commit('m_user/saveLocation')
  52. },
  53. // 将经纬度存储到本地
  54. saveLocation(state) {
  55. uni.setStorageSync('location', state.location)
  56. },
  57. // 将老师认证详情图片存入本地
  58. updateAuthorizePhoto(state, authorizePhoto) {
  59. state.authorizePhoto = authorizePhoto
  60. this.commit('m_user/saveAuthorizePhoto')
  61. },
  62. saveAuthorizePhoto(state) {
  63. uni.setStorageSync('authorizePhoto', state.authorizePhoto)
  64. }
  65. },
  66. // 数据包装器
  67. getters: {
  68. // 收货详细地址的计算属性
  69. addstr(state) {
  70. if (!state.address.provinceName) return ''
  71. // 拼接 省,市,区,详细地址 的字符串并返回给用户
  72. return state.address.provinceName + state.address.cityName +
  73. state.address.countyName + state.address.detailInfo
  74. }
  75. },
  76. }