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

135 lines
2.9 KiB

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