index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // 页面路径:store/index.js
  2. import Vue from 'vue'
  3. import Vuex from 'vuex'
  4. import persistedstate from 'vuex-persistedstate'
  5. Vue.use(Vuex); //vue的插件机制
  6. //Vuex.Store 构造器选项
  7. const store = new Vuex.Store({
  8. actions: {
  9. //相当于异步的操作,不能直接改变state的值,只能通过触发mutations的方法才能改变
  10. asyncUpdateUser(context, user) {
  11. context.commit('updateUser', user);
  12. },
  13. asyncUpdatemsg(context, data) {
  14. context.commit('updatemsg', data);
  15. },
  16. asyncDelMsg(context, data) {
  17. context.commit('delMsg', data);
  18. },
  19. asyncDelAll(context, data) {
  20. context.commit('delAll', data);
  21. }
  22. },
  23. mutations: {
  24. //相当于同步的操作
  25. updateUser(state, user) {
  26. // console.log('啊啊啊啊啊');
  27. state.user = user;
  28. },
  29. // 修改头像
  30. updateAvatar(state, data) {
  31. state.user.avatar = data;
  32. },
  33. // 修改昵称nc
  34. updateNc(state, data) {
  35. state.user.nc = data;
  36. },
  37. // 添加消息提示
  38. updatemsg(state, msg) {
  39. console.log('data:',msg);
  40. state.msgpop.push(msg);
  41. // state.msgpop.splice(0,1,msg);
  42. },
  43. // 删除消息提示
  44. delMsg(state, data) {
  45. // setTimeout(() => {
  46. // console.log('删除了');
  47. // }, 2000)
  48. state.msgpop.shift();
  49. },
  50. delAll(state, data) {
  51. state.msgpop = [];
  52. },
  53. // 保存发布内容
  54. // saveformData(state, formData) {
  55. // state.formData = formData;
  56. // },
  57. removeData(state) {
  58. state.formData = {};
  59. }
  60. },
  61. state: {
  62. //公共的变量,存储数据,这里的变量不能随便修改,只能通过触发mutations的方法才能改变
  63. user: {
  64. openid: '',
  65. avatar: '', //头像
  66. username: '', //用户名
  67. sex: '', //用户性别
  68. school: '', //学校
  69. spe: '', //专业
  70. rankl: '', //职称
  71. brief: '', //用户简介
  72. tel: "", //联系方式
  73. },
  74. msgpop: []
  75. // 聊天记录
  76. // message: [],
  77. // formData: {}
  78. },
  79. getters: {
  80. getUser(state) {
  81. return state.user;
  82. },
  83. getMsg(state) {
  84. return state.message;
  85. }
  86. },
  87. plugins: [persistedstate({
  88. // 传入参数
  89. storage: {
  90. getItem: (key) => uni.getStorageSync(key),
  91. setItem: (key, val) => uni.setStorageSync(key, val),
  92. removeItem: (key) => uni.removeStorageSync(key)
  93. }
  94. })]
  95. })
  96. export default store