about.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <script lang="ts" setup>
  2. import { isApp, isAppAndroid, isAppHarmony, isAppIOS, isAppPlus, isH5, isMpWeixin, isWeb } from '@uni-helper/uni-env'
  3. import { LOGIN_PAGE } from '@/router/config'
  4. import { useTokenStore } from '@/store'
  5. import { tabbarStore } from '@/tabbar/store'
  6. import RequestOpenApiComp from './components/request-openapi.vue'
  7. import RequestComp from './components/request.vue'
  8. import VBindCss from './components/VBindCss.vue'
  9. definePage({
  10. style: {
  11. navigationBarTitleText: '关于',
  12. },
  13. // 登录授权(可选):跟以前的 needLogin 类似功能,但是同时支持黑白名单,详情请见 arc/router 文件夹
  14. excludeLoginPath: false,
  15. })
  16. const tokenStore = useTokenStore()
  17. // 浏览器打印 isH5为true, isWeb为false,大家尽量用 isH5
  18. console.log({ isApp, isAppAndroid, isAppHarmony, isAppIOS, isAppPlus, isH5, isMpWeixin, isWeb })
  19. function gotoLogin() {
  20. if (tokenStore.hasLogin) {
  21. uni.showToast({
  22. title: '已登录,不能去登录页',
  23. icon: 'none',
  24. })
  25. return
  26. }
  27. uni.navigateTo({
  28. url: `${LOGIN_PAGE}?redirect=${encodeURIComponent('/pages/about/about?a=1&b=2')}`,
  29. })
  30. }
  31. function logout() {
  32. // 清空用户信息
  33. tokenStore.logout()
  34. // 执行退出登录逻辑
  35. uni.showToast({
  36. title: '退出登录成功',
  37. icon: 'success',
  38. })
  39. }
  40. function gotoTabbar() {
  41. uni.switchTab({
  42. url: '/pages/index/index',
  43. })
  44. }
  45. // #region setTabbarBadge
  46. function setTabbarBadge() {
  47. tabbarStore.setTabbarItemBadge(1, 100)
  48. }
  49. // #endregion
  50. function gotoAlova() {
  51. uni.navigateTo({
  52. url: '/pages/about/alova',
  53. })
  54. }
  55. function gotoSubPage() {
  56. uni.navigateTo({
  57. url: '/pages-sub/demo/index',
  58. })
  59. }
  60. // uniLayout里面的变量通过 expose 暴露出来后可以在 onReady 钩子获取到(onLoad 钩子不行)
  61. const uniLayout = ref()
  62. onLoad(() => {
  63. console.log('onLoad:', uniLayout.value) // onLoad: undefined
  64. })
  65. onReady(() => {
  66. console.log('onReady:', uniLayout.value) // onReady: Proxy(Object)
  67. console.log('onReady:', uniLayout.value.testUniLayoutExposedData) // onReady: testUniLayoutExposedData
  68. })
  69. // 结论:第一次通过onShow获取不到,但是可以通过 onReady获取到,后面就可以通过onShow获取到了
  70. onShow(() => {
  71. console.log('onShow:', uniLayout.value) // onReady: Proxy(Object)
  72. console.log('onShow:', uniLayout.value?.testUniLayoutExposedData) // onReady: testUniLayoutExposedData
  73. })
  74. const uniKuRoot = ref()
  75. // 结论:(同上)第一次通过onShow获取不到,但是可以通过 onReady获取到,后面就可以通过onShow获取到了
  76. onReady(() => {
  77. console.log('onReady uniKuRoot exposeRef', uniKuRoot.value?.exposeRef)
  78. })
  79. onShow(() => {
  80. console.log('onShow uniKuRoot exposeRef', uniKuRoot.value?.exposeRef)
  81. })
  82. </script>
  83. <template root="uniKuRoot">
  84. <!-- page-meta 使用范例 -->
  85. <page-meta page-style="overflow: auto" />
  86. <view>
  87. <view class="mt-8 text-center text-xl text-gray-400">
  88. 请求调用、unocss、static图片
  89. </view>
  90. <view class="my-2 text-center">
  91. <image src="/static/images/avatar.jpg" class="h-100px w-100px" />
  92. </view>
  93. <view class="my-2 text-center">
  94. 当前是否登录:{{ tokenStore.hasLogin }}
  95. </view>
  96. <view class="m-auto max-w-600px flex items-center">
  97. <button class="mt-4 w-40 text-center" @click="gotoLogin">
  98. 点击去登录页
  99. </button>
  100. <button class="mt-4 w-40 text-center" @click="logout">
  101. 点击退出登录
  102. </button>
  103. </view>
  104. <button class="mt-4 w-60 text-center" @click="setTabbarBadge">
  105. 设置tabbarBadge
  106. </button>
  107. <RequestOpenApiComp />
  108. <RequestComp />
  109. <VBindCss />
  110. <view class="mb-6 h-1px bg-#eee" />
  111. <view class="text-center">
  112. <button type="primary" size="mini" class="w-160px" @click="gotoAlova">
  113. 前往 alova 示例页面
  114. </button>
  115. </view>
  116. <view class="text-center">
  117. <button type="primary" size="mini" class="w-160px" @click="gotoTabbar">
  118. 切换tabbar
  119. </button>
  120. </view>
  121. <view class="text-center">
  122. <button type="primary" size="mini" class="w-160px" @click="gotoSubPage">
  123. 前往分包页面
  124. </button>
  125. </view>
  126. <view class="mt-6 text-center text-sm">
  127. <view class="inline-block w-80% text-gray-400">
  128. 为了方便脚手架动态生成不同UI模板,本页的按钮统一使用UI库无关的原生button
  129. </view>
  130. </view>
  131. <view class="h-6" />
  132. </view>
  133. </template>