index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <template>
  2. <view class="settings-container">
  3. <!-- 消息通知设置 -->
  4. <view class="settings-section">
  5. <view class="section-title">消息通知</view>
  6. <view class="settings-list">
  7. <view class="settings-item">
  8. <text>系统通知</text>
  9. <switch :checked="notifications.system" @change="handleSystemNotification" color="#4CAF50" />
  10. </view>
  11. <view class="settings-item">
  12. <text>作物提醒</text>
  13. <switch :checked="notifications.crop" @change="handleCropNotification" color="#4CAF50" />
  14. </view>
  15. <view class="settings-item">
  16. <text>天气预警</text>
  17. <switch :checked="notifications.weather" @change="handleWeatherNotification" color="#4CAF50" />
  18. </view>
  19. </view>
  20. </view>
  21. <!-- 通用设置 -->
  22. <view class="settings-section">
  23. <view class="section-title">通用设置</view>
  24. <view class="settings-list">
  25. <view class="settings-item" @click="handleClearCache">
  26. <view class="item-left">
  27. <text>清除缓存</text>
  28. <text class="sub-text">{{ cacheSize }}</text>
  29. </view>
  30. <text class="arrow">></text>
  31. </view>
  32. <view class="settings-item" @click="checkUpdate">
  33. <view class="item-left">
  34. <text>检查更新</text>
  35. <text class="sub-text">当前版本 {{ version }}</text>
  36. </view>
  37. <text class="arrow">></text>
  38. </view>
  39. </view>
  40. </view>
  41. <!-- 关于 -->
  42. <view class="settings-section">
  43. <view class="section-title">关于</view>
  44. <view class="settings-list">
  45. <view class="settings-item" @click="navigateToAboutUs">
  46. <text>关于我们</text>
  47. <text class="arrow">></text>
  48. </view>
  49. <view class="settings-item" @click="navigateToPrivacy">
  50. <text>隐私政策</text>
  51. <text class="arrow">></text>
  52. </view>
  53. </view>
  54. </view>
  55. </view>
  56. </template>
  57. <script setup>
  58. import { ref, reactive } from 'vue'
  59. // 响应式数据
  60. const notifications = reactive({
  61. system: true,
  62. crop: true,
  63. weather: true
  64. })
  65. const cacheSize = ref('0.00MB')
  66. const version = ref('1.0.0')
  67. // 获取缓存大小
  68. const getCacheSize = () => {
  69. uni.getStorageInfo({
  70. success: (res) => {
  71. const size = (res.currentSize / 1024).toFixed(2)
  72. cacheSize.value = size + 'MB'
  73. }
  74. })
  75. }
  76. // 清除缓存
  77. const handleClearCache = () => {
  78. uni.showModal({
  79. title: '提示',
  80. content: '确定要清除缓存吗?',
  81. success: (res) => {
  82. if (res.confirm) {
  83. uni.clearStorage({
  84. success: () => {
  85. uni.showToast({
  86. title: '清除成功',
  87. icon: 'success'
  88. })
  89. getCacheSize()
  90. }
  91. })
  92. }
  93. }
  94. })
  95. }
  96. // 检查更新
  97. const checkUpdate = () => {
  98. uni.showLoading({ title: '检查更新中...' })
  99. setTimeout(() => {
  100. uni.hideLoading()
  101. uni.showToast({
  102. title: '已是最新版本',
  103. icon: 'none'
  104. })
  105. }, 1500)
  106. }
  107. // 通知设置处理函数
  108. const handleSystemNotification = (e) => {
  109. notifications.system = e.detail.value
  110. saveNotificationSettings()
  111. }
  112. const handleCropNotification = (e) => {
  113. notifications.crop = e.detail.value
  114. saveNotificationSettings()
  115. }
  116. const handleWeatherNotification = (e) => {
  117. notifications.weather = e.detail.value
  118. saveNotificationSettings()
  119. }
  120. // 保存通知设置
  121. const saveNotificationSettings = () => {
  122. uni.setStorageSync('notifications', notifications)
  123. }
  124. // 加载通知设置
  125. const loadNotificationSettings = () => {
  126. const savedSettings = uni.getStorageSync('notifications')
  127. if (savedSettings) {
  128. Object.assign(notifications, savedSettings)
  129. }
  130. }
  131. // 页面跳转
  132. const navigateToAboutUs = () => {
  133. uni.navigateTo({ url: '/pages/about/index' })
  134. }
  135. const navigateToPrivacy = () => {
  136. uni.navigateTo({ url: '/pages/privacy/index' })
  137. }
  138. // uni-app 页面生命周期
  139. const onShow = () => {
  140. getCacheSize()
  141. loadNotificationSettings()
  142. }
  143. // 导出页面生命周期方法供 uni-app 调用
  144. defineExpose({
  145. onShow
  146. })
  147. </script>
  148. <style lang="scss">
  149. .settings-container {
  150. min-height: 100vh;
  151. background-color: #f5f5f5;
  152. padding: 20rpx;
  153. }
  154. .settings-section {
  155. margin-bottom: 20rpx;
  156. background-color: #ffffff;
  157. border-radius: 16rpx;
  158. overflow: hidden;
  159. }
  160. .section-title {
  161. padding: 20rpx 30rpx;
  162. font-size: 28rpx;
  163. color: #999;
  164. background-color: #f8f8f8;
  165. }
  166. .settings-list {
  167. .settings-item {
  168. display: flex;
  169. justify-content: space-between;
  170. align-items: center;
  171. padding: 30rpx;
  172. border-bottom: 1rpx solid #f0f0f0;
  173. &:last-child {
  174. border-bottom: none;
  175. }
  176. text {
  177. font-size: 28rpx;
  178. color: #333;
  179. }
  180. .item-left {
  181. display: flex;
  182. flex-direction: column;
  183. .sub-text {
  184. font-size: 24rpx;
  185. color: #999;
  186. margin-top: 4rpx;
  187. }
  188. }
  189. .arrow {
  190. color: #999;
  191. font-size: 24rpx;
  192. }
  193. switch {
  194. transform: scale(0.8);
  195. transform-origin: right center;
  196. }
  197. }
  198. }
  199. </style>