index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //引入vue和vuex
  2. import Vue from 'vue'
  3. import Vuex from 'vuex'
  4. Vue.use(Vuex)
  5. // 用户是否登录
  6. let hasLogin = uni.getStorageSync('hasLogin') || false
  7. // 用户信息
  8. let userinfo = uni.getStorageSync('userinfo') || {}
  9. // 历史搜搜
  10. let searchData = uni.getStorageSync('searchData') || []
  11. const store = new Vuex.Store({ //全局变量定义
  12. state: {
  13. forcedLogin: false, //是否需要强制登录
  14. userLoginFlag: hasLogin, //用户是否登录
  15. userinfo: userinfo, //存放用户账号数据
  16. searchData: searchData ,// 历史搜索
  17. searchItem: '' // 导航栏搜索内容
  18. },
  19. getters: {
  20. // 用户信息
  21. info(state) {
  22. return state.userinfo
  23. },
  24. // 历史搜搜
  25. historySearch(state) {
  26. return state.searchData
  27. },
  28. // 历史搜索 5条之内
  29. historySearchInFive(state) {
  30. return state.searchData.slice(0,5)
  31. },
  32. // 历史搜索 超出5条
  33. historySearchOverFive(state) {
  34. return state.searchData.slice(5)
  35. },
  36. searchItemData(state) {
  37. return state.searchItem
  38. }
  39. },
  40. mutations: {
  41. // 登录
  42. login(state, provider) {
  43. state.userLoginFlag = true;
  44. state.userInfo = provider
  45. uni.setStorage({ // 异步缓存用户信息
  46. key: "userinfo",
  47. data: provider
  48. })
  49. },
  50. // 退出
  51. logout(state) {
  52. // state.userLoginFlag = false;
  53. // state.userInfo = {};
  54. // uni.removeStorage({ // 清除用户信息
  55. // key: "userinfo"
  56. // })
  57. uni.navigateTo({
  58. url: '/pages/login/index'
  59. })
  60. },
  61. // 修改导航栏搜索内容的值
  62. updateSearchItemData(state, data) {
  63. state.searchItem = data
  64. },
  65. // 清空历史搜索或者储存
  66. clearSearchData(state, data) {
  67. state.searchData = data
  68. },
  69. }
  70. })
  71. export default store