index.vue 7.4 KB

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