debug.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <template>
  2. <view class="test-container">
  3. <text class="title">调试工具</text>
  4. <!-- 缓存信息展示 -->
  5. <view class="section">
  6. <text class="section-title">当前缓存状态</text>
  7. <view class="info-card">
  8. <view class="info-row">
  9. <text class="label">是否登录:</text>
  10. <text class="value" :class="{ success: isLogined, error: !isLogined }">
  11. {{ isLogined ? '已登录' : '未登录' }}
  12. </text>
  13. </view>
  14. <view class="info-row">
  15. <text class="label">Token:</text>
  16. <text class="value token">{{ tokenDisplay }}</text>
  17. </view>
  18. <view class="info-row">
  19. <text class="label">用户信息:</text>
  20. <text class="value">{{ userInfoDisplay }}</text>
  21. </view>
  22. </view>
  23. </view>
  24. <!-- 操作按钮 -->
  25. <view class="section">
  26. <text class="section-title">操作</text>
  27. <button class="action-btn" @click="simulateLogin">模拟登录</button>
  28. <button class="action-btn warn" @click="clearCache">清除缓存</button>
  29. <button class="action-btn" @click="refreshStatus">刷新状态</button>
  30. <button class="action-btn" @click="gotoSplash">跳转到启动页</button>
  31. <button class="action-btn" @click="gotoLogin">跳转到登录页</button>
  32. <button class="action-btn" @click="gotoHome">跳转到首页</button>
  33. </view>
  34. <!-- 日志区域 -->
  35. <view class="section">
  36. <text class="section-title">操作日志</text>
  37. <scroll-view scroll-y class="log-box">
  38. <view v-for="(log, index) in logs" :key="index" class="log-item">
  39. <text class="log-time">{{ log.time }}</text>
  40. <text class="log-text">{{ log.text }}</text>
  41. </view>
  42. </scroll-view>
  43. </view>
  44. </view>
  45. </template>
  46. <script setup>
  47. import { ref, computed, onMounted } from 'vue'
  48. import { getUserInfo, getToken, isLogin, saveUserInfo, clearUserInfo } from '@/utils/userCache.js'
  49. const logs = ref([])
  50. const isLogined = ref(false)
  51. const tokenDisplay = ref('无')
  52. const userInfoDisplay = ref('无')
  53. // 刷新状态
  54. const refreshStatus = () => {
  55. isLogined.value = isLogin()
  56. const token = getToken()
  57. tokenDisplay.value = token ? token.substring(0, 20) + '...' : '无'
  58. const userInfo = getUserInfo()
  59. if (userInfo) {
  60. userInfoDisplay.value = JSON.stringify(userInfo)
  61. } else {
  62. userInfoDisplay.value = '无'
  63. }
  64. addLog('状态已刷新')
  65. }
  66. // 添加日志
  67. const addLog = (text) => {
  68. const now = new Date()
  69. const time = `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}:${now.getSeconds().toString().padStart(2, '0')}`
  70. logs.value.unshift({ time, text })
  71. // 只保留最近 20 条
  72. if (logs.value.length > 20) {
  73. logs.value.pop()
  74. }
  75. }
  76. // 模拟登录
  77. const simulateLogin = () => {
  78. const mockUserInfo = {
  79. id: 'test_user_001',
  80. nickname: '测试用户',
  81. phone: '13800138000',
  82. avatar: ''
  83. }
  84. const mockToken = 'mock_token_' + Date.now()
  85. const success = saveUserInfo(mockUserInfo, mockToken)
  86. if (success) {
  87. addLog('模拟登录成功')
  88. refreshStatus()
  89. uni.showToast({
  90. title: '模拟登录成功',
  91. icon: 'success'
  92. })
  93. } else {
  94. addLog('模拟登录失败')
  95. }
  96. }
  97. // 清除缓存
  98. const clearCache = () => {
  99. uni.showModal({
  100. title: '提示',
  101. content: '确定要清除缓存吗?',
  102. success: (res) => {
  103. if (res.confirm) {
  104. clearUserInfo()
  105. addLog('缓存已清除')
  106. refreshStatus()
  107. uni.showToast({
  108. title: '已清除',
  109. icon: 'success'
  110. })
  111. }
  112. }
  113. })
  114. }
  115. // 跳转
  116. const gotoSplash = () => {
  117. addLog('跳转到启动页')
  118. uni.reLaunch({
  119. url: '/pages/splash/splash'
  120. })
  121. }
  122. const gotoLogin = () => {
  123. addLog('跳转到登录页')
  124. uni.navigateTo({
  125. url: '/pages/login/login'
  126. })
  127. }
  128. const gotoHome = () => {
  129. addLog('跳转到首页')
  130. uni.navigateTo({
  131. url: '/pages/index/index'
  132. })
  133. }
  134. onMounted(() => {
  135. addLog('调试工具已加载')
  136. refreshStatus()
  137. })
  138. </script>
  139. <style lang="scss" scoped>
  140. .test-container {
  141. min-height: 100vh;
  142. background-color: #f5f5f5;
  143. padding: 40rpx;
  144. }
  145. .title {
  146. font-size: 40rpx;
  147. font-weight: 600;
  148. color: #333;
  149. display: block;
  150. text-align: center;
  151. margin-bottom: 40rpx;
  152. }
  153. .section {
  154. background-color: #ffffff;
  155. border-radius: 16rpx;
  156. padding: 32rpx;
  157. margin-bottom: 32rpx;
  158. .section-title {
  159. font-size: 30rpx;
  160. font-weight: 600;
  161. color: #333;
  162. display: block;
  163. margin-bottom: 24rpx;
  164. }
  165. }
  166. .info-card {
  167. background-color: #f9f9f9;
  168. border-radius: 12rpx;
  169. padding: 24rpx;
  170. .info-row {
  171. display: flex;
  172. margin-bottom: 16rpx;
  173. &:last-child {
  174. margin-bottom: 0;
  175. }
  176. .label {
  177. font-size: 26rpx;
  178. color: #666;
  179. width: 160rpx;
  180. flex-shrink: 0;
  181. }
  182. .value {
  183. font-size: 26rpx;
  184. color: #333;
  185. flex: 1;
  186. word-break: break-all;
  187. &.success {
  188. color: #52c41a;
  189. }
  190. &.error {
  191. color: #ff4d4f;
  192. }
  193. &.token {
  194. font-family: monospace;
  195. background-color: #f0f0f0;
  196. padding: 4rpx 12rpx;
  197. border-radius: 8rpx;
  198. }
  199. }
  200. }
  201. }
  202. .action-btn {
  203. width: 100%;
  204. height: 80rpx;
  205. background-color: #1890ff;
  206. color: #ffffff;
  207. font-size: 28rpx;
  208. border-radius: 12rpx;
  209. margin-bottom: 16rpx;
  210. border: none;
  211. &::after {
  212. border: none;
  213. }
  214. &.warn {
  215. background-color: #ff4d4f;
  216. }
  217. }
  218. .log-box {
  219. height: 400rpx;
  220. background-color: #1e1e1e;
  221. border-radius: 12rpx;
  222. padding: 20rpx;
  223. .log-item {
  224. display: flex;
  225. margin-bottom: 12rpx;
  226. &:last-child {
  227. margin-bottom: 0;
  228. }
  229. .log-time {
  230. font-size: 22rpx;
  231. color: #888;
  232. font-family: monospace;
  233. margin-right: 16rpx;
  234. }
  235. .log-text {
  236. font-size: 24rpx;
  237. color: #fff;
  238. flex: 1;
  239. }
  240. }
  241. }
  242. </style>