lex 2 年之前
父节点
当前提交
ffcb066db8
共有 6 个文件被更改,包括 85 次插入74 次删除
  1. 56 0
      src/helpers/hooks.ts
  2. 1 0
      src/state.ts
  3. 5 0
      src/student/main.ts
  4. 5 0
      src/teacher/main.ts
  5. 18 27
      src/views/coupons/list.tsx
  6. 0 47
      src/views/order-detail/use-coupons/choice-coupon.tsx

+ 56 - 0
src/helpers/hooks.ts

@@ -1,3 +1,6 @@
+import { reactive } from 'vue'
+import request from './request'
+
 // 搜索关键字
 export const SubjectEnum = {
   SEARCH: 'music-songbook-subject',
@@ -24,3 +27,56 @@ export const useSubjectId = (
     return ''
   }
 }
+
+interface IList {
+  params?: any
+  requestType?: 'json' | 'form'
+  loading: boolean
+  list: any[]
+  dataShow: boolean
+  finished: boolean
+}
+
+let lockLoad = false
+/*
+ * 列表获取数据
+ * @param url 请求地址
+ * @returns {Promise<IList>}
+ */
+export const useList = async (
+  url: string,
+  params: IList,
+  method = 'POST' as 'POST' | 'GET'
+) => {
+  try {
+    if (lockLoad) return params
+    // 上锁
+    lockLoad = true
+    const res =
+      method === 'POST'
+        ? await request.post(url, {
+            requestType: params.requestType || 'json',
+            data: {
+              ...params.params
+            }
+          })
+        : await request.get(url, {
+            requestType: params.requestType || 'json',
+            params: {
+              ...params.params
+            }
+          })
+    params.loading = false
+    const result = res.data || {}
+    params.list = params.list.concat(result.rows || [])
+    params.finished = result.pageNo >= result.totalPage
+    params.params.page = result.pageNo + 1
+    params.dataShow = params.list.length > 0
+  } catch {
+    params.dataShow = false
+    params.finished = true
+  }
+  // 解锁
+  lockLoad = false
+  return params
+}

+ 1 - 0
src/state.ts

@@ -9,6 +9,7 @@ export const state = reactive({
     data: {} as any
   },
   platformType: '' as 'STUDENT' | 'TEACHER',
+  platformApi: '/api-student' as '/api-student' | '/api-teacher',
   version: '', // 版本号 例如: 1.0.0
   ossUploadUrl: 'https://ks3-cn-beijing.ksyuncs.com/',
   musicCertStatus: false as boolean, // 是否音乐认证

+ 5 - 0
src/student/main.ts

@@ -33,6 +33,11 @@ if (browser().isTeacher) {
 } else {
   state.platformType = 'STUDENT'
 }
+if (state.platformType === 'TEACHER') {
+  state.platformApi = '/api-teacher'
+} else {
+  state.platformApi = '/api-student'
+}
 
 dayjs.locale('zh-ch')
 app.config.globalProperties.$dayjs = dayjs

+ 5 - 0
src/teacher/main.ts

@@ -45,6 +45,11 @@ if (browser().isTeacher) {
 } else {
   state.platformType = 'TEACHER'
 }
+if (state.platformType === 'TEACHER') {
+  state.platformApi = '/api-teacher'
+} else {
+  state.platformApi = '/api-student'
+}
 
 dayjs.locale('zh-ch')
 app.config.globalProperties.$dayjs = dayjs

+ 18 - 27
src/views/coupons/list.tsx

@@ -1,4 +1,5 @@
 import ColResult from '@/components/col-result'
+import { useList } from '@/helpers/hooks'
 import request from '@/helpers/request'
 import { state } from '@/state'
 import { List } from 'vant'
@@ -18,10 +19,11 @@ export default defineComponent({
   data() {
     return {
       list: [] as any,
-      dataShow: true, // 判断是否有数据
-      loading: false,
-      finished: false,
-      lockLoad: false,
+      listState: {
+        dataShow: true, // 判断是否有数据
+        loading: false,
+        finished: false
+      },
       params: {
         useState: this.useState,
         page: 1,
@@ -35,38 +37,27 @@ export default defineComponent({
   methods: {
     async getList() {
       try {
-        if (this.lockLoad) return
-        // 上锁
-        this.lockLoad = true
-        // 判断是哪个端
-        const url =
-          state.platformType === 'STUDENT' ? '/api-student' : '/api-teacher'
-        const res = await request.post(`${url}/couponInfo/page`, {
-          data: {
-            ...this.params
-          }
+        const result = await useList(`${state.platformApi}/couponInfo/page`, {
+          params: this.params,
+          list: this.list,
+          ...this.listState
         })
-        this.loading = false
-        const result = res.data || {}
-        this.list = this.list.concat(result.rows || [])
-        this.finished = result.pageNo >= result.totalPage
-        this.params.page = result.pageNo + 1
-        this.dataShow = this.list.length > 0
-        // 解锁
-        this.lockLoad = false
+        const { params, list, ...res } = result
+        this.params = params
+        this.list = list
+        this.listState = res
       } catch {
-        this.dataShow = false
-        this.finished = true
+        //
       }
     }
   },
   render() {
     return (
       <>
-        {this.dataShow ? (
+        {this.listState.dataShow ? (
           <List
-            v-model:loading={this.loading}
-            finished={this.finished}
+            v-model:loading={this.listState.loading}
+            finished={this.listState.finished}
             finishedText=" "
             // 为了处理体验问题
             class={[this.list.length > 0 && styles.list]}

+ 0 - 47
src/views/order-detail/use-coupons/choice-coupon.tsx

@@ -6,52 +6,6 @@ import { Button, Loading } from 'vant'
 import { defineComponent } from 'vue'
 import styles from './index.module.less'
 
-export const list = [
-  {
-    couponType: 'FULL_DISCOUNT',
-    discountPrice: '2',
-    useLimit: '30',
-    endTime: new Date(),
-    id: 1,
-    couponName: '小酷Ai充值券',
-    startTime: new Date(),
-    useState: 'USABLE',
-    useTime: new Date()
-  },
-  {
-    couponType: 'FULL_DISCOUNT',
-    discountPrice: '5',
-    useLimit: '20',
-    endTime: new Date(),
-    id: 2,
-    couponName: '小酷Ai充值券',
-    startTime: new Date(),
-    useState: 'USABLE',
-    useTime: new Date()
-  },
-  {
-    couponType: 'FULL_DISCOUNT',
-    discountPrice: '3',
-    useLimit: '10',
-    endTime: new Date(),
-    id: 3,
-    couponName: '小酷Ai充值券',
-    startTime: new Date(),
-    useState: 'USABLE',
-    useTime: new Date()
-  },
-  {
-    couponType: 'FULL_DISCOUNT',
-    discountPrice: '1',
-    useLimit: '29',
-    endTime: new Date(),
-    id: 0,
-    couponName: '小酷Ai充值券',
-    startTime: new Date(),
-    useState: 'USABLE',
-    useTime: new Date()
-  }
-]
 export default defineComponent({
   name: 'choice-coupon',
   props: {
@@ -91,7 +45,6 @@ export default defineComponent({
         const res = await request.post(`${url}/couponInfo/page`, {
           data: {
             useState: 'USABLE',
-
             page: 1,
             rows: 100
           }