index.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <template>
  2. <z-paging ref="paging" v-model="list" @query="queryList" @onRefresh="onRefresh">
  3. <template #top>
  4. <!-- 用户信息区域 -->
  5. <view class="user-info-section">
  6. <view v-if="userInfo" class="user-card">
  7. <image
  8. :src="userInfo.avatar || '/static/logo.png'"
  9. class="user-avatar"
  10. />
  11. <view class="user-details">
  12. <text class="user-name">{{ userInfo.nickname || userInfo.username }}</text>
  13. <text class="user-roles">{{ userInfo.roles.join(', ') }}</text>
  14. </view>
  15. <button class="logout-btn" @click="handleLogout">退出登录</button>
  16. </view>
  17. <view v-else class="login-prompt">
  18. <text class="prompt-text">请先登录</text>
  19. <button class="login-btn" @click="goToLogin">前往登录</button>
  20. </view>
  21. </view>
  22. </template>
  23. <view class="content">
  24. <image class="logo" src="/static/logo.png" />
  25. <view class="text-area">
  26. <text class="title">{{ title }}</text>
  27. </view>
  28. <view class="mb-80">{{ selectDictLabel(class_type, 1) }}</view>
  29. <view class="bg-blue-500 text-white p-4 rounded">Hello UnoCSS!</view>
  30. <template v-for="(item, index) in list" :key="index">
  31. <up-button
  32. text="渐变色按钮"
  33. color="linear-gradient(to right, rgb(66, 83, 216), rgb(213, 51, 186))"
  34. />
  35. </template>
  36. </view>
  37. </z-paging>
  38. </template>
  39. <script setup lang="ts">
  40. import { computed } from 'vue';
  41. import { useAuthStore } from '@/store/modules/auth';
  42. import { useUserStore } from '@/store/modules/user';
  43. import { checkAuth, logoutAndRedirect } from '@/utils/routeGuard';
  44. const { proxy } = getCurrentInstance() as ComponentInternalInstance;
  45. const { class_type } = toRefs<any>(proxy?.useDict("class_type"));
  46. const title = ref("Hello");
  47. const paging = ref<any>(null);
  48. // Store
  49. const authStore = useAuthStore();
  50. const userStore = useUserStore();
  51. // 计算属性
  52. const userInfo = computed(() => userStore.userInfo);
  53. console.log(import.meta.env);
  54. onMounted(async () => {
  55. // 检查登录状态
  56. await checkAuth({ requireAuth: false });
  57. uni.showToast({
  58. title: "Hello World!",
  59. icon: "none",
  60. duration: 2000,
  61. });
  62. });
  63. const list = ref<any[]>([]);
  64. const queryList = (pageNo: number, pageSize: number) => {
  65. console.log(`请求第${pageNo}页,每页${pageSize}条数据`);
  66. // 这里的pageNo和pageSize会自动计算好,直接传给服务器即可
  67. // 这里的请求只是演示,请替换成自己的项目的网络请求,并在网络请求回调中通过paging.value.complete(请求回来的数组)将请求结果传给z-paging
  68. setTimeout(() => {
  69. const data = Array.from({ length: pageSize }, (_, i) => ({
  70. id: pageNo * pageSize + i + 1,
  71. name: `Item ${pageNo * pageSize + i + 1}`,
  72. }));
  73. paging.value.complete(data);
  74. }, 1000);
  75. };
  76. const onRefresh = () => {
  77. try {
  78. // 这里可以执行一些刷新操作,比如重新请求数据
  79. console.log("页面刷新");
  80. paging.value.refresh(); // 调用z-paging的refresh方法
  81. } catch (error) {
  82. console.error("刷新失败", error);
  83. }
  84. };
  85. /**
  86. * 前往登录页
  87. */
  88. const goToLogin = (): void => {
  89. uni.navigateTo({
  90. url: '/pages/login/login'
  91. });
  92. };
  93. /**
  94. * 处理退出登录
  95. */
  96. const handleLogout = async (): Promise<void> => {
  97. try {
  98. uni.showModal({
  99. title: '确认退出',
  100. content: '确定要退出登录吗?',
  101. success: async (res) => {
  102. if (res.confirm) {
  103. await logoutAndRedirect();
  104. uni.showToast({
  105. title: '已退出登录',
  106. icon: 'success'
  107. });
  108. }
  109. }
  110. });
  111. } catch (error) {
  112. console.error('退出登录失败:', error);
  113. uni.showToast({
  114. title: '退出失败',
  115. icon: 'error'
  116. });
  117. }
  118. };
  119. </script>
  120. <style lang="scss" scoped>
  121. .user-info-section {
  122. padding: 20rpx;
  123. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  124. .user-card {
  125. display: flex;
  126. align-items: center;
  127. padding: 20rpx;
  128. background: rgba(255, 255, 255, 0.95);
  129. border-radius: 12rpx;
  130. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
  131. .user-avatar {
  132. width: 80rpx;
  133. height: 80rpx;
  134. border-radius: 50%;
  135. margin-right: 20rpx;
  136. }
  137. .user-details {
  138. flex: 1;
  139. .user-name {
  140. display: block;
  141. font-size: 32rpx;
  142. font-weight: 600;
  143. color: #333;
  144. margin-bottom: 8rpx;
  145. }
  146. .user-roles {
  147. display: block;
  148. font-size: 24rpx;
  149. color: #666;
  150. }
  151. }
  152. .logout-btn {
  153. padding: 12rpx 24rpx;
  154. background: #f56c6c;
  155. color: #fff;
  156. border: none;
  157. border-radius: 8rpx;
  158. font-size: 24rpx;
  159. }
  160. }
  161. .login-prompt {
  162. display: flex;
  163. align-items: center;
  164. justify-content: space-between;
  165. padding: 20rpx;
  166. background: rgba(255, 255, 255, 0.95);
  167. border-radius: 12rpx;
  168. .prompt-text {
  169. font-size: 28rpx;
  170. color: #666;
  171. }
  172. .login-btn {
  173. padding: 12rpx 24rpx;
  174. background: #667eea;
  175. color: #fff;
  176. border: none;
  177. border-radius: 8rpx;
  178. font-size: 24rpx;
  179. }
  180. }
  181. }
  182. .content {
  183. flex-direction: column;
  184. align-items: center;
  185. justify-content: center;
  186. }
  187. .logo {
  188. height: 200rpx;
  189. width: 200rpx;
  190. margin-top: 200rpx;
  191. margin-left: auto;
  192. margin-right: auto;
  193. margin-bottom: 50rpx;
  194. }
  195. .text-area {
  196. display: flex;
  197. justify-content: center;
  198. }
  199. .title {
  200. font-size: 36rpx;
  201. color: #8f8f94;
  202. }
  203. </style>