Showing
4 changed files
with
88 additions
and
44 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'; | ||
| 11 | 21 | ||
| 12 | -export const useMessageList = () => { | 22 | +// scrollList的类型是个麻烦,其他地方调用时类型不一定一样 |
| 13 | - // 我的留言接口联调 | 23 | +function fn(data: [], scrollList: Ref<commentListType[]>) { |
| 14 | - interface commentListType { | 24 | +// function fn(data: [], scrollList: any) { |
| 15 | - id: string; | ||
| 16 | - avatar: string; | ||
| 17 | - name: string; | ||
| 18 | - kg_name: string; | ||
| 19 | - comment_time: string; | ||
| 20 | - note: string; | ||
| 21 | - c_action: string; | ||
| 22 | - c_name: string; | ||
| 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 () => { | ||
| 40 | - 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; }) => { | ||
| 42 | - let arr = _.split(item.target_name, '@'); // 分割评论的动作和姓名 | ||
| 43 | - item.c_action = arr[0]; // 评论动作 | ||
| 44 | - item.c_name = arr[1]; // 评论姓名 | ||
| 45 | - }); | ||
| 46 | // 数据全部加载完成 | 25 | // 数据全部加载完成 |
| 47 | if (!data.length) { | 26 | if (!data.length) { |
| 48 | // 加载状态结束 | 27 | // 加载状态结束 |
| 49 | finished.value = true; | 28 | finished.value = true; |
| 50 | } | 29 | } |
| 51 | - commentList.value = [...commentList.value, ...data]; | 30 | + scrollList.value = [...scrollList.value, ...data]; |
| 52 | - offset.value = commentList.value.length; | 31 | + offset.value = scrollList.value.length; |
| 53 | loading.value = false; | 32 | loading.value = false; |
| 54 | // 隐藏loading标识,空列表图标 | 33 | // 隐藏loading标识,空列表图标 |
| 55 | - if (!commentList.value.length) { | 34 | + if (!scrollList.value.length) { |
| 56 | finishedTextStatus.value = false; | 35 | finishedTextStatus.value = false; |
| 57 | emptyStatus.value = true; | 36 | emptyStatus.value = true; |
| 58 | } else { | 37 | } else { |
| 59 | emptyStatus.value = false; | 38 | emptyStatus.value = false; |
| 60 | } | 39 | } |
| 61 | - } | 40 | + |
| 62 | return { | 41 | return { |
| 63 | - onLoad, | 42 | + scrollList, |
| 64 | - commentList, | 43 | + finished, |
| 65 | loading, | 44 | loading, |
| 45 | + finishedTextStatus, | ||
| 46 | + emptyStatus, | ||
| 47 | + }; | ||
| 48 | +} | ||
| 49 | + | ||
| 50 | +export const useMessageList = () => { | ||
| 51 | + // 我的留言接口联调 | ||
| 52 | + const scrollList = ref<commentListType[]>([]); | ||
| 53 | + let obj = { | ||
| 54 | + scrollList, | ||
| 66 | finished, | 55 | finished, |
| 56 | + loading, | ||
| 67 | finishedTextStatus, | 57 | finishedTextStatus, |
| 68 | emptyStatus, | 58 | emptyStatus, |
| 69 | }; | 59 | }; |
| 60 | + | ||
| 61 | + const onLoad = async () => { | ||
| 62 | + const { data } = await myCommentAPI({ limit: limit.value, offset: offset.value }); | ||
| 63 | + data.forEach((item: { target_name: string | null | undefined; c_action: string; c_name: string; }) => { | ||
| 64 | + let arr = _.split(item.target_name, '@'); // 分割评论的动作和姓名 | ||
| 65 | + item.c_action = arr[0]; // 评论动作 | ||
| 66 | + item.c_name = arr[1]; // 评论姓名 | ||
| 67 | + }); | ||
| 68 | + // fn把重复代码抽离 | ||
| 69 | + obj = fn(data, scrollList); | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + return { | ||
| 73 | + onLoad, | ||
| 74 | + scrollList: obj.scrollList, | ||
| 75 | + loading: obj.loading, | ||
| 76 | + finished: obj.finished, | ||
| 77 | + finishedTextStatus: obj.finishedTextStatus, | ||
| 78 | + emptyStatus: obj.emptyStatus, | ||
| 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