diff --git a/.eslintrc-auto-import.json b/.eslintrc-auto-import.json index 27df73d..3024d13 100644 --- a/.eslintrc-auto-import.json +++ b/.eslintrc-auto-import.json @@ -96,6 +96,7 @@ "onWatcherCleanup": true, "useId": true, "useModel": true, - "useTemplateRef": true + "useTemplateRef": true, + "useListPageRequest": true } } diff --git a/env/.env b/env/.env index d520ddc..598fc0a 100644 --- a/env/.env +++ b/env/.env @@ -7,7 +7,7 @@ VITE_WX_APPID = 'wxa2abb91f64032a2b' # h5部署网站的base,配置到 manifest.config.ts 里的 h5.router.base VITE_APP_PUBLIC_BASE=/ -VITE_SERVER_BASEURL = 'https://ukw0y1.laf.run' +VITE_SERVER_BASEURL = 'https://mapi.foxmail555.cc' VITE_UPLOAD_BASEURL = 'https://ukw0y1.laf.run/upload' # h5是否需要配置代理 diff --git a/package.json b/package.json index 313fe1a..ddce8fe 100644 --- a/package.json +++ b/package.json @@ -103,6 +103,7 @@ "@dcloudio/uni-mp-xhs": "3.0.0-4020920240930001", "@dcloudio/uni-quickapp-webview": "3.0.0-4020920240930001", "dayjs": "1.11.10", + "nprogress": "^0.2.0", "pinia": "2.0.36", "pinia-plugin-persistedstate": "3.2.1", "qs": "6.5.3", diff --git a/pages.config.ts b/pages.config.ts index 987dbd4..f7e1c2c 100644 --- a/pages.config.ts +++ b/pages.config.ts @@ -32,17 +32,18 @@ export default defineUniPages({ pagePath: 'pages/index/index', text: '首页', }, - { - iconPath: 'static/tabbar/invest.png', - selectedIconPath: 'static/tabbar/investHL.png', - pagePath: 'pages/invest/index', - text: '投资', - }, + { iconPath: 'static/tabbar/house.png', selectedIconPath: 'static/tabbar/houseHL.png', pagePath: 'pages/invite/index', - text: '房屋', + text: '邀请好友', + }, + { + iconPath: 'static/tabbar/invest.png', + selectedIconPath: 'static/tabbar/investHL.png', + pagePath: 'pages/invest/index', + text: '理财', }, { iconPath: 'static/tabbar/personal.png', diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 17f379b..9aaa382 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -62,6 +62,9 @@ importers: dayjs: specifier: 1.11.10 version: 1.11.10 + nprogress: + specifier: ^0.2.0 + version: 0.2.0 pinia: specifier: 2.0.36 version: 2.0.36(typescript@5.7.2)(vue@3.4.21(typescript@5.7.2)) @@ -4227,6 +4230,9 @@ packages: resolution: {integrity: sha512-ppwTtiJZq0O/ai0z7yfudtBpWIoxM8yE6nHi1X47eFR2EWORqfbu6CnPlNsjeN683eT0qG6H/Pyf9fCcvjnnnQ==} engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} + nprogress@0.2.0: + resolution: {integrity: sha512-I19aIingLgR1fmhftnbWWO3dXc0hSxqHQHQb3H8m+K3TnEn/iSeTZZOyvKXWqQESMwuUVnatlCnZdLBZZt2VSA==} + nth-check@2.1.1: resolution: {integrity: sha512-lqjrjmaOoAnWfMmBPL+XNnynZh2+swxiX3WUE0s4yEHI6m+AwrK2UZOimIRl3X/4QctVqS8AiZjFqyOGrMXb/w==} @@ -10967,6 +10973,8 @@ snapshots: dependencies: path-key: 4.0.0 + nprogress@0.2.0: {} + nth-check@2.1.1: dependencies: boolbase: 1.0.0 diff --git a/src/App.vue b/src/App.vue index f2de0bb..051d51c 100644 --- a/src/App.vue +++ b/src/App.vue @@ -11,6 +11,10 @@ onLaunch((options) => { // redirect: options.path, // }, }) + } else { + if (userStore.isLogged) { + userStore.loadUserInfo() + } } }) @@ -100,15 +104,15 @@ uni-modal { } .uni-modal__title { - @apply p-16 text-left text-16 font-600; + @apply p-15 text-left text-16 font-600; } .uni-modal__bd { - @apply mb-16 px-16 pt-0 text-left text-14 font-medium text-#666; + @apply px-15 pt-0 text-left text-14 font-medium text-#666; } .uni-modal__btn { - @apply px-16 py-8 flex-none rounded-full shadow-none text-14; + @apply px-15 py-6 flex-none rounded-full shadow-none text-14; &::after { content: none; @@ -120,7 +124,7 @@ uni-modal { } .uni-modal__ft { - @apply justify-end p-16 leading-normal; + @apply justify-end px-15 pb-10 leading-normal; &::after { content: none; diff --git a/src/components/Avatar.vue b/src/components/Avatar.vue index 4dd87aa..eda5b81 100644 --- a/src/components/Avatar.vue +++ b/src/components/Avatar.vue @@ -20,8 +20,9 @@ defineProps({ block overflow-hidden rounded-full - style="background: linear-gradient(180deg, #96e517 0%, #8bc135 100%)" + bg-gray-100 class="h-full w-full" + mode="aspectFill" :src="src ?? DefaultAavatar" /> diff --git a/src/hooks/useListPageRequest.ts b/src/hooks/useListPageRequest.ts new file mode 100644 index 0000000..3f51a1d --- /dev/null +++ b/src/hooks/useListPageRequest.ts @@ -0,0 +1,112 @@ +import { formatTime, getImageUrl } from '@/utils' +import { onPullDownRefresh, onReachBottom } from '@dcloudio/uni-app' +import { computed, onMounted, ref } from 'vue' + +interface PaginationRequest { + page?: number + limit?: number + [key: string]: any +} + +function useListPageRequest( + request: (params: PaginationRequest) => Promise, + options: { + autoBind?: boolean + autoRequest?: boolean + } = {}, +) { + const { autoBind = true, autoRequest = true } = options + const list = ref([]) + // 加载中 + const loading = ref(false) + // 加载更多中 + const moreLoading = ref(false) + // 全部数据加载完成 + const totalPage = ref(0) + const finished = computed(() => totalPage.value <= pagingParams.value.page) + const pagingParams = ref({ + page: 1, + limit: 1000, + }) + const getPagingParams = computed(() => { + return { + ...pagingParams.value, + } + }) + let customParams: PaginationRequest = {} + + const statusLoading = computed(() => { + if (moreLoading.value || loading.value) { + return 'loading' + } + if (finished.value) { + return 'noMore' + } + return 'more' + }) + + const loadData = async () => { + const { data } = await request({ + ...getPagingParams.value, + ...customParams, + }) + console.log(data) + data.forEach((item: any) => { + item.createtime = formatTime(item.createtime) + item.image = getImageUrl(item.image) + }) + // totalPage.value = data.count ? Math.ceil(data.count / pagingParams.value.limit!) : 0 + list.value.push(...data) + } + + const onReload = async (params?: AnyObject) => { + loading.value = true + pagingParams.value.page = 1 + if (params) { + customParams = params + } + try { + list.value = [] + await loadData() + } finally { + loading.value = false + } + } + const onMore = async () => { + if (finished.value) { + return + } + pagingParams.value.page = pagingParams.value.page! + 1 + moreLoading.value = true + try { + await loadData() + } finally { + moreLoading.value = false + } + } + + if (autoRequest) { + onMounted(onReload) + } + if (autoBind) { + onPullDownRefresh(async () => { + await onReload() + uni.stopPullDownRefresh() + }) + onReachBottom(onMore) + } + + return { + list, + loading, + moreLoading, + finished, + statusLoading, + totalPage, + + onReload, + onMore, + } +} + +export { useListPageRequest } diff --git a/src/interceptors/request.ts b/src/interceptors/request.ts index 1f84b31..e5f1be5 100644 --- a/src/interceptors/request.ts +++ b/src/interceptors/request.ts @@ -51,9 +51,8 @@ const httpInterceptor = { } // 3. 添加 token 请求头标识 const userStore = useUserStore() - const { token } = userStore.userInfo as unknown as IUserInfo - if (token) { - options.header.Authorization = `Bearer ${token}` + if (userStore.token) { + options.header.token = userStore.token } }, } diff --git a/src/interceptors/route.ts b/src/interceptors/route.ts index b50c59d..f25e512 100644 --- a/src/interceptors/route.ts +++ b/src/interceptors/route.ts @@ -4,14 +4,17 @@ * 可以设置路由白名单,或者黑名单,看业务需要选哪一个 * 我这里应为大部分都可以随便进入,所以使用黑名单 */ +import { getUserInfoAPI } from '@/service' import { useUserStore } from '@/store' import { getNotNeedLoginPages as _getNotNeedLoginPages, notNeedLoginPages as _notNeedLoginPages, - getIsTabbar, } from '@/utils' import type { Router } from 'vue-router' -import { tabBar } from '@/pages.json' + +import NProgress from 'nprogress' +import 'nprogress/nprogress.css' +NProgress.configure({ showSpinner: true, parent: '#app' }) export const loginRoute = '/pages/login/index' @@ -26,14 +29,26 @@ const isDev = import.meta.env.DEV const navigateToInterceptor = { // 注意,这里的url是 '/' 开头的,如 '/pages/index/index',跟 'pages.json' 里面的 path 不同 invoke({ url }: { url: string }) { - const isTabbar = getIsTabbar(url) - console.log(url) - // const redirectRoute = `${loginRoute}?redirect=${encodeURIComponent(url)}` - if (isTabbar) { - uni.switchTab({ url }) - return false + // console.log(url) // /pages/route-interceptor/index?name=feige&age=30 + const path = url.split('?')[0] + let notNeedLoginPages: string[] = [] + // 为了防止开发时出现BUG,这里每次都获取一下。生产环境可以移到函数外,性能更好 + if (isDev) { + notNeedLoginPages = getNotNeedLoginPages() + } else { + notNeedLoginPages = _notNeedLoginPages } - return true + const isNeedLogin = notNeedLoginPages.includes(path) + if (!isNeedLogin) { + return true + } + const hasLogin = isLogged() + if (hasLogin) { + return true + } + // const redirectRoute = `${loginRoute}?redirect=${encodeURIComponent(url)}` + uni.navigateTo({ url: loginRoute }) + return false }, } @@ -58,7 +73,6 @@ export const getNotNeedLoginPages = () => { export const checkLoginUrl = (url: string) => { const path = url.split('?')[0] const isNotNeedLogin = getNotNeedLoginPages().includes(path.replace(/^pages/, '/pages')) - console.log(path) return isNotNeedLogin } @@ -79,15 +93,21 @@ export const checkNotFound = (url: string) => { } export const routerBeforeEachConfig = (router: Router) => { - router.beforeEach((to, from, next) => { + router.beforeEach(async (to, from, next) => { + NProgress.start() if (checkWithRedirectLogin(to.path)) { return next({ path: loginRoute, - // query: { - // redirect: to.fullPath, - // }, }) } + const userStore = useUserStore() + if (!userStore.userInfo && userStore.isLogged) { + await userStore.loadUserInfo() + } return next() }) + + router.afterEach(() => { + NProgress.done() + }) } diff --git a/src/main.ts b/src/main.ts index aeb6a7a..1f21bb8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,27 +1,22 @@ import { createSSRApp } from 'vue' import App from './App.vue' import store from './store' -import i18n from './locale/index' -import { - routeInterceptor, - routerBeforeEachConfig, - requestInterceptor, - prototypeInterceptor, -} from './interceptors' +import { requestInterceptor, prototypeInterceptor, routerBeforeEachConfig } from './interceptors' import 'virtual:uno.css' import '@/style/index.scss' export function createApp() { const app = createSSRApp(App) + app.use(store) - app.use(i18n) - app.use(routeInterceptor) + // app.use(routeInterceptor) app.use(requestInterceptor) app.use(prototypeInterceptor) setTimeout(() => { routerBeforeEachConfig((app as any).router) }, 0) + return { app, } diff --git a/src/pages-sub/asset/index.vue b/src/pages-sub/asset/index.vue index 60aa3fb..f1bf843 100644 --- a/src/pages-sub/asset/index.vue +++ b/src/pages-sub/asset/index.vue @@ -7,6 +7,26 @@ } + +