wangxy преди 11 месеца
родител
ревизия
ad447a401d
променени са 1 файла, в които са добавени 54 реда и са изтрити 27 реда
  1. 54 27
      components/mta-scroll-list/mta-scroll-list.vue

+ 54 - 27
components/mta-scroll-list/mta-scroll-list.vue

@@ -32,40 +32,67 @@
 
 	const Emits = defineEmits(['pull-down-refresh', 'reach-buttom']);
 
+	/**
+	 * 页码
+	 */
 	const page = ref(1);
 
-	function transformUniRequestToPromise(config) => new Promise((resolve, reject) => uni.request(...config, success(data) {resolve(data)}, fail(err) {reject(err)})
-
-	async function getData(action) {
-		try {
-			action === 'pull-down-refresh' && (page.value = 1)
-
-			const opt = {
-				url: props.refreshUrl,
-				method: "POST",
-				data: {
-					page: page.value,
-					size: props.size,
-					...props.options
+	/**
+	 * @summary 将请求转换为promise
+	 */
+	function transformUniRequestToPromise(config) => new Promise((resolve, reject) => uni.request(...config, success(
+			data) {
+				resolve(data)
+			}, fail(err) {
+				reject(err)
+			})
+
+			/**
+			 * @summary 更新数据
+			 * @param {String} action  'pull-down-refresh' | 'reach-buttom'
+			 */
+			async function getData(action) {
+				try {
+					// 刷新页码重置
+					action === 'pull-down-refresh' && (page.value = 1);
+					// 触底页码 加1
+					action === 'reach-buttom' && (page.value++);
+
+					const opt = {
+						url: props.refreshUrl,
+						method: "POST",
+						data: {
+							page: page.value,
+							size: props.size,
+							...props.options
+						}
+					}
+
+					const data = await transformUniRequestToPromise(opt)
+
+					action === 'pull-down-refresh' && (uni.stopPullDownRefresh());
+
+					Emit(action, data)
+				} catch (err) {
+					console.log('错误', err)
+					uni.stopPullDownRefresh()
 				}
 			}
 
-			const data = await transformUniRequestToPromise(opt)
-
-			action === 'pull-down-refresh' && (uni.stopPullDownRefresh());
-
-			Emit(action, data)
-		} catch (err) {
-			console.log('错误', err)
-			uni.stopPullDownRefresh()
-		}
-	}
-
-	onPullDownRefresh(() => getData('pull-down-refresh'))
+			/**
+			 * @summary 下拉刷新
+			 */
+			onPullDownRefresh(() => getData('pull-down-refresh'));
 
-	onLoad(() => uni.startPullDownRefresh())
+			/**
+			 * @summary 加载完成 首次进行下拉刷新
+			 */
+			onLoad(() => uni.startPullDownRefresh());
 
-	onReachBottom(() => getData('reach-buttom'))
+			/**
+			 * @summary 触底更新
+			 */
+			onReachBottom(() => getData('reach-buttom'));
 </script>
 
 <style lang="scss">