hookehuyr

🦄 refactor(书籍详情页): 下拉滚动列表写法简化测试

1 +/*
2 + * @Date: 2022-05-26 19:50:27
3 + * @LastEditors: hookehuyr hookehuyr@gmail.com
4 + * @LastEditTime: 2022-06-18 00:15:08
5 + * @FilePath: /tswj/src/api/C/book.js
6 + * @Description: 文件描述
7 + */
1 import { fn, fetch } from '@/api/fn'; 8 import { fn, fetch } from '@/api/fn';
2 9
3 const Api = { 10 const Api = {
4 ADD_SUBSCRIBE: '/srv/?a=add_subscribe', 11 ADD_SUBSCRIBE: '/srv/?a=add_subscribe',
12 + BOOK_INFO: '/srv/?a=book_info',
5 } 13 }
6 14
7 /** 15 /**
...@@ -10,3 +18,13 @@ const Api = { ...@@ -10,3 +18,13 @@ const Api = {
10 * @returns 18 * @returns
11 */ 19 */
12 export const addSubscribeAPI = (params) => fn(fetch.post(Api.ADD_SUBSCRIBE, params)); 20 export const addSubscribeAPI = (params) => fn(fetch.post(Api.ADD_SUBSCRIBE, params));
21 +
22 +/**
23 + * @description: 书籍详情
24 + * @param {String} book_id 书籍 ID
25 + * @param {String} localism_type 方言类型
26 + * @param {*} limit
27 + * @param {*} offset
28 + * @returns
29 + */
30 +export const bookInfoAPI = (params) => fn(fetch.get(Api.BOOK_INFO, params));
......
1 +/*
2 + * @Date: 2022-05-05 18:07:16
3 + * @LastEditors: hookehuyr hookehuyr@gmail.com
4 + * @LastEditTime: 2022-06-18 00:22:36
5 + * @FilePath: /tswj/src/composables/useVideoList.js
6 + * @Description: 文件描述
7 + */
1 import { ref } from 'vue' 8 import { ref } from 'vue'
2 import axios from '@/utils/axios'; 9 import axios from '@/utils/axios';
3 import _ from 'lodash' 10 import _ from 'lodash'
4 import { Toast } from 'vant'; 11 import { Toast } from 'vant';
5 import { useRoute } from 'vue-router' 12 import { useRoute } from 'vue-router'
13 +import { bookInfoAPI } from '@/api/C/book'
14 +import { flowFn } from '@/hooks/useFlowFn'
6 15
7 export const useVideoList = () => { 16 export const useVideoList = () => {
8 const $route = useRoute(); 17 const $route = useRoute();
...@@ -97,47 +106,13 @@ export const useVideoList = () => { ...@@ -97,47 +106,13 @@ export const useVideoList = () => {
97 /** 106 /**
98 * 向下滚动查询数据 107 * 向下滚动查询数据
99 */ 108 */
100 - const onLoad = () => { 109 + const onLoad = async () => {
101 // 异步更新数据 110 // 异步更新数据
102 - axios.get('/srv/?a=book_info', { 111 + const { data, code } = await bookInfoAPI({ book_id: $route.query.id, localism_type: chooseLanguage.value.text, limit: limit.value, offset: offset.value })
103 - params: { 112 + if (code === 1) {
104 - book_id: $route.query.id, 113 + bookInfo.value = data;
105 - localism_type: chooseLanguage.value.text, 114 + flowFn(data.prod_list, prod_list, offset, loading, finished, finishedTextStatus, emptyStatus);
106 - limit: limit.value,
107 - offset: offset.value
108 - }
109 - })
110 - .then(res => {
111 - if (res.data.code === 1) {
112 - bookInfo.value = res.data.data;
113 - prod_list.value = _.concat(prod_list.value, res.data.data.prod_list);
114 - prod_list.value = _.uniqBy(prod_list.value, 'id');
115 - offset.value = prod_list.value.length;
116 - loading.value = false;
117 - // 数据全部加载完成
118 - if (!res.data.data.prod_list.length) {
119 - // 加载状态结束
120 - finished.value = true;
121 - }
122 - // 空数据提示
123 - if (!prod_list.value.length) {
124 - finishedTextStatus.value = false;
125 - }
126 - emptyStatus.value = Object.is(prod_list.value.length, 0);
127 - } else {
128 - // tslint:disable-next-line: no-console
129 - console.warn(res);
130 - if (!res.data.show) return false;
131 - Toast({
132 - icon: 'close',
133 - message: res.data.msg
134 - });
135 } 115 }
136 - })
137 - .catch(err => {
138 - // tslint:disable-next-line: no-console
139 - console.error(err);
140 - })
141 }; 116 };
142 117
143 /** 118 /**
......
1 +
2 +/**
3 + * @description 封装简化滚动查询列表执行流程
4 + * @param {*} data 接口返回列表数据
5 + * @param {*} list 自定义列表
6 + * @param {*} offset
7 + * @param {*} loading
8 + * @param {*} finished
9 + * @param {*} finishedTextStatus
10 + * @param {*} emptyStatus
11 + */
12 +import _ from 'lodash'
13 +
14 +export const flowFn = (data, list, offset, loading, finished, finishedTextStatus, emptyStatus) => {
15 + list.value = _.concat(list.value, data);
16 + list.value = _.uniqBy(list.value, 'id');
17 + offset.value = list.value.length;
18 + loading.value = false;
19 + // 数据全部加载完成
20 + if (!data.length) {
21 + // 加载状态结束
22 + finished.value = true;
23 + }
24 + // 空数据提示
25 + if (!list.value.length) {
26 + finishedTextStatus.value = false;
27 + }
28 + emptyStatus.value = Object.is(list.value.length, 0);
29 +}