小说小程序前端代码仓库(小程序)
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.

122 lines
2.6 KiB

3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex); //vue的插件机制
  4. import api from '@/api/api.js'
  5. //Vuex.Store 构造器选项
  6. const store = new Vuex.Store({
  7. state: {
  8. configList: {}, //配置列表
  9. userInfo: {}, //用户信息
  10. themeMode: uni.getStorageSync('themeMode') || 'light', // 主题模式:light(白天)或dark(黑夜)
  11. isLogin : !!uni.getStorageSync('token'),
  12. },
  13. getters: {
  14. // 当前主题模式
  15. currentTheme(state) {
  16. return state.themeMode
  17. },
  18. // 是否为黑夜模式
  19. isDarkMode(state) {
  20. return state.themeMode === 'dark'
  21. }
  22. },
  23. mutations: {
  24. // 初始化配置
  25. initConfig(state) {
  26. api('getConfig', res => {
  27. if (res.code == 200) {
  28. // state.configList = res.result
  29. res.result.forEach(n => {
  30. state.configList[n.keyName] = n.keyContent
  31. })
  32. }
  33. })
  34. },
  35. // 切换主题模式
  36. toggleThemeMode(state) {
  37. state.themeMode = state.themeMode === 'light' ? 'dark' : 'light'
  38. // 保存到本地存储,以便下次打开时恢复
  39. uni.setStorageSync('themeMode', state.themeMode)
  40. },
  41. // 设置为指定主题模式
  42. setThemeMode(state, mode) {
  43. if (mode === 'light' || mode === 'dark') {
  44. state.themeMode = mode
  45. // 保存到本地存储,以便下次打开时恢复
  46. uni.setStorageSync('themeMode', state.themeMode)
  47. }
  48. },
  49. // 初始化主题模式
  50. initThemeMode(state) {
  51. // 从本地存储中读取主题设置
  52. const savedMode = uni.getStorageSync('themeMode')
  53. if (savedMode) {
  54. state.themeMode = savedMode
  55. }
  56. },
  57. login(state) {
  58. uni.showLoading({
  59. title: '登录中...'
  60. })
  61. uni.login({
  62. success(res) {
  63. if (res.errMsg != "login:ok") {
  64. return
  65. }
  66. api('wxLogin', {
  67. code: res.code
  68. }, res => {
  69. uni.hideLoading()
  70. if (res.code != 200) {
  71. return
  72. }
  73. state.userInfo = res.result.userInfo
  74. uni.setStorageSync('token', res.result.token)
  75. state.isLogin = true
  76. if (!state.userInfo.nickName || !state.userInfo.headImage) {
  77. uni.navigateTo({
  78. url: '/pages_order/auth/wxUserInfo'
  79. })
  80. } else {
  81. uni.navigateBack(-1)
  82. }
  83. })
  84. }
  85. })
  86. },
  87. getUserInfo(state) {
  88. api('getUserInfo', res => {
  89. if (res.code == 200) {
  90. state.userInfo = res.result
  91. }
  92. })
  93. },
  94. // 退出登录
  95. logout(state, title = '确认退出登录吗') {
  96. uni.showModal({
  97. title,
  98. success(r) {
  99. if (r.confirm) {
  100. state.userInfo = {}
  101. state.isLogin = false
  102. uni.removeStorageSync('token')
  103. uni.reLaunch({
  104. url: '/pages/index/index'
  105. })
  106. }
  107. }
  108. })
  109. },
  110. },
  111. actions: {},
  112. })
  113. export default store