Showing
4 changed files
with
93 additions
and
49 deletions
src/@types/message/interface.ts
0 → 100644
| 1 | +export interface commentListType { | ||
| 2 | + id: string; | ||
| 3 | + avatar: string; | ||
| 4 | + name: string; | ||
| 5 | + kg_name: string; | ||
| 6 | + comment_time: string; | ||
| 7 | + note: string; | ||
| 8 | + c_action: string; | ||
| 9 | + c_name: string; | ||
| 10 | + cover: string; | ||
| 11 | + prod_id: string; | ||
| 12 | + perf_id: string; | ||
| 13 | + book_id: string; | ||
| 14 | + perf_name: string; | ||
| 15 | + book_name: string; | ||
| 16 | + localism_type: string; | ||
| 17 | + is_new: number; | ||
| 18 | +} |
| 1 | /* | 1 | /* |
| 2 | * @Date: 2022-06-12 22:51:21 | 2 | * @Date: 2022-06-12 22:51:21 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2022-06-12 23:00:04 | 4 | + * @LastEditTime: 2022-06-13 01:01:33 |
| 5 | * @FilePath: /tswj/src/composables/useMessageList.ts | 5 | * @FilePath: /tswj/src/composables/useMessageList.ts |
| 6 | - * @Description: 文件描述 | 6 | + * @Description: 测试处理列表查询功能中的重复代码问题 |
| 7 | + * 感觉复用意义不大,这么写反而很麻烦 | ||
| 7 | */ | 8 | */ |
| 8 | -import { ref } from 'vue' | 9 | +import { Ref, ref } from 'vue' |
| 9 | import { myCommentAPI } from '@/api/C/me' | 10 | import { myCommentAPI } from '@/api/C/me' |
| 10 | import _ from 'lodash' | 11 | import _ from 'lodash' |
| 12 | +import { commentListType } from '@/@types/message/interface'; | ||
| 13 | +import { | ||
| 14 | + loading, | ||
| 15 | + finished, | ||
| 16 | + limit, | ||
| 17 | + offset, | ||
| 18 | + finishedTextStatus, | ||
| 19 | + emptyStatus, | ||
| 20 | +} from '@/composables/variable'; | ||
| 21 | + | ||
| 22 | +// scrollList的类型是个麻烦,其他地方调用时类型不一定一样 | ||
| 23 | +function fn(data: [], scrollList: Ref<commentListType[]>) { | ||
| 24 | +// function fn(data: [], scrollList: any) { | ||
| 25 | + // 数据全部加载完成 | ||
| 26 | + if (!data.length) { | ||
| 27 | + // 加载状态结束 | ||
| 28 | + finished.value = true; | ||
| 29 | + } | ||
| 30 | + scrollList.value = [...scrollList.value, ...data]; | ||
| 31 | + offset.value = scrollList.value.length; | ||
| 32 | + loading.value = false; | ||
| 33 | + // 隐藏loading标识,空列表图标 | ||
| 34 | + if (!scrollList.value.length) { | ||
| 35 | + finishedTextStatus.value = false; | ||
| 36 | + emptyStatus.value = true; | ||
| 37 | + } else { | ||
| 38 | + emptyStatus.value = false; | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + return { | ||
| 42 | + scrollList, | ||
| 43 | + finished, | ||
| 44 | + loading, | ||
| 45 | + finishedTextStatus, | ||
| 46 | + emptyStatus, | ||
| 47 | + }; | ||
| 48 | +} | ||
| 11 | 49 | ||
| 12 | export const useMessageList = () => { | 50 | export const useMessageList = () => { |
| 13 | // 我的留言接口联调 | 51 | // 我的留言接口联调 |
| 14 | - interface commentListType { | 52 | + const scrollList = ref<commentListType[]>([]); |
| 15 | - id: string; | 53 | + let obj = { |
| 16 | - avatar: string; | 54 | + scrollList, |
| 17 | - name: string; | 55 | + finished, |
| 18 | - kg_name: string; | 56 | + loading, |
| 19 | - comment_time: string; | 57 | + finishedTextStatus, |
| 20 | - note: string; | 58 | + emptyStatus, |
| 21 | - c_action: string; | 59 | + }; |
| 22 | - c_name: string; | 60 | + |
| 23 | - cover: string; | ||
| 24 | - prod_id: string; | ||
| 25 | - perf_id: string; | ||
| 26 | - book_id: string; | ||
| 27 | - perf_name: string; | ||
| 28 | - book_name: string; | ||
| 29 | - localism_type: string; | ||
| 30 | - is_new: number; | ||
| 31 | - } | ||
| 32 | - const commentList = ref<commentListType[]>([]) | ||
| 33 | - const loading = ref(false); | ||
| 34 | - const finished = ref(false); | ||
| 35 | - const limit = ref(10); | ||
| 36 | - const offset = ref(0) | ||
| 37 | - const finishedTextStatus = ref(false); | ||
| 38 | - const emptyStatus = ref(false); | ||
| 39 | const onLoad = async () => { | 61 | const onLoad = async () => { |
| 40 | const { data } = await myCommentAPI({ limit: limit.value, offset: offset.value }); | 62 | const { data } = await myCommentAPI({ limit: limit.value, offset: offset.value }); |
| 41 | data.forEach((item: { target_name: string | null | undefined; c_action: string; c_name: string; }) => { | 63 | data.forEach((item: { target_name: string | null | undefined; c_action: string; c_name: string; }) => { |
| ... | @@ -43,28 +65,16 @@ export const useMessageList = () => { | ... | @@ -43,28 +65,16 @@ export const useMessageList = () => { |
| 43 | item.c_action = arr[0]; // 评论动作 | 65 | item.c_action = arr[0]; // 评论动作 |
| 44 | item.c_name = arr[1]; // 评论姓名 | 66 | item.c_name = arr[1]; // 评论姓名 |
| 45 | }); | 67 | }); |
| 46 | - // 数据全部加载完成 | 68 | + // fn把重复代码抽离 |
| 47 | - if (!data.length) { | 69 | + obj = fn(data, scrollList); |
| 48 | - // 加载状态结束 | ||
| 49 | - finished.value = true; | ||
| 50 | - } | ||
| 51 | - commentList.value = [...commentList.value, ...data]; | ||
| 52 | - offset.value = commentList.value.length; | ||
| 53 | - loading.value = false; | ||
| 54 | - // 隐藏loading标识,空列表图标 | ||
| 55 | - if (!commentList.value.length) { | ||
| 56 | - finishedTextStatus.value = false; | ||
| 57 | - emptyStatus.value = true; | ||
| 58 | - } else { | ||
| 59 | - emptyStatus.value = false; | ||
| 60 | - } | ||
| 61 | } | 70 | } |
| 71 | + | ||
| 62 | return { | 72 | return { |
| 63 | onLoad, | 73 | onLoad, |
| 64 | - commentList, | 74 | + scrollList: obj.scrollList, |
| 65 | - loading, | 75 | + loading: obj.loading, |
| 66 | - finished, | 76 | + finished: obj.finished, |
| 67 | - finishedTextStatus, | 77 | + finishedTextStatus: obj.finishedTextStatus, |
| 68 | - emptyStatus, | 78 | + emptyStatus: obj.emptyStatus, |
| 69 | }; | 79 | }; |
| 70 | } | 80 | } | ... | ... |
src/composables/variable.js
0 → 100644
| 1 | +/* | ||
| 2 | + * @Date: 2022-06-12 23:54:24 | ||
| 3 | + * @LastEditors: hookehuyr hookehuyr@gmail.com | ||
| 4 | + * @LastEditTime: 2022-06-13 00:50:29 | ||
| 5 | + * @FilePath: /tswj/src/composables/variable.js | ||
| 6 | + * @Description: 文件描述 | ||
| 7 | + */ | ||
| 8 | +import { ref } from 'vue'; | ||
| 9 | + | ||
| 10 | +export const loading = ref(false); | ||
| 11 | +export const finished = ref(false); | ||
| 12 | +export const limit = ref(5); | ||
| 13 | +export const offset = ref(0) | ||
| 14 | +export const finishedTextStatus = ref(false); | ||
| 15 | +export const emptyStatus = ref(false); |
| ... | @@ -62,8 +62,9 @@ import goToVideoDetail from '@/router/methods/videoDetail' | ... | @@ -62,8 +62,9 @@ import goToVideoDetail from '@/router/methods/videoDetail' |
| 62 | import { delCommentAPI } from '@/api/C/me' | 62 | import { delCommentAPI } from '@/api/C/me' |
| 63 | import { myCommentTimeAPI } from '@/api/C/perf' | 63 | import { myCommentTimeAPI } from '@/api/C/perf' |
| 64 | import { useMessageList } from '@/composables'; | 64 | import { useMessageList } from '@/composables'; |
| 65 | +// import { commentListType } from '@/@types/message/interface'; | ||
| 65 | 66 | ||
| 66 | -const { onLoad, commentList, loading, finished, finishedTextStatus, emptyStatus, } = useMessageList(); | 67 | +const { onLoad, scrollList:commentList, loading, finished, finishedTextStatus, emptyStatus, } = useMessageList(); |
| 67 | 68 | ||
| 68 | onBeforeRouteLeave(async () => { | 69 | onBeforeRouteLeave(async () => { |
| 69 | // 更新留言阅读情况 | 70 | // 更新留言阅读情况 | ... | ... |
-
Please register or login to post a comment