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.

103 lines
2.2 KiB

4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
  1. <template>
  2. <view class="main">
  3. <view :style="{height:iSMT+'px'}"></view>
  4. <view class="theme">
  5. <view class="left">
  6. <image class="img-theme" src="/static/my/whiteTheme.png" mode="widthFix" />
  7. <radio value="0" class="radio-btn" activeBackgroundColor="red" :checked="selectedIndex === 0"
  8. @click="updateTheme('light')" />
  9. </view>
  10. <view class="left">
  11. <image class="img-theme" src="/static/my/blackTheme.png" mode="widthFix" />
  12. <radio value="1" class="radio-btn" activeBackgroundColor="red" :checked="selectedIndex === 1"
  13. @click="updateTheme('dark')" />
  14. </view>
  15. </view>
  16. </view>
  17. </template>
  18. <script setup>
  19. import {
  20. ref,
  21. onMounted
  22. } from 'vue'
  23. import {
  24. getSetting,
  25. updateSetting
  26. } from "@/api/setting/general"
  27. const iSMT = ref(0)
  28. const selectedIndex = ref(0)
  29. const themeTypeMap = {
  30. 'light': 0,
  31. 'dark': 1
  32. }
  33. const getTheme = async () => {
  34. try {
  35. const res = await getSetting()
  36. if (res.code === 200) {
  37. const theme = res.data.theme
  38. selectedIndex.value = themeTypeMap[theme] ?? 0
  39. }
  40. } catch (err) {
  41. console.error("获取主题设置失败:", err);
  42. }
  43. }
  44. const updateTheme = async (themeType) => {
  45. try {
  46. selectedIndex.value = themeTypeMap[themeType]
  47. console.log('主题:', themeType, ',looklook索引:', selectedIndex.value)
  48. const updateRes = await updateSetting({
  49. theme: themeType
  50. })
  51. if (updateRes.code === 200) {
  52. uni.showToast({
  53. title: '主题设置成功',
  54. icon: 'none'
  55. })
  56. }
  57. } catch (err) {
  58. console.error("更新主题设置失败:", err);
  59. uni.showToast({
  60. title: '设置失败,请重试',
  61. icon: 'none'
  62. })
  63. }
  64. }
  65. onMounted(() => {
  66. // 状态栏高度
  67. iSMT.value = uni.getSystemInfoSync().statusBarHeight;
  68. console.log('看看高度', iSMT.value)
  69. getTheme()
  70. })
  71. </script>
  72. <style>
  73. .theme {
  74. margin-top: 1.5vh;
  75. height: 34vh;
  76. background-color: white;
  77. display: flex;
  78. justify-content: space-around;
  79. }
  80. .img-theme {
  81. width: 316rpx;
  82. height: 362rpx;
  83. }
  84. .left {
  85. width: 350rpx;
  86. display: flex;
  87. flex-direction: column;
  88. justify-content: center;
  89. align-items: center;
  90. }
  91. .radio-btn {
  92. margin-top: 36rpx;
  93. transform: scale(0.8);
  94. }
  95. </style>