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

134 lines
2.9 KiB

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