Просмотр исходного кода

feat:标讯收藏数据模型

yangfeng 1 год назад
Родитель
Сommit
ba6d60f907

+ 46 - 0
data/data-models/modules/quick-collect-bid/README.md

@@ -0,0 +1,46 @@
+# useQuickCollectBidModel
+> 标讯收藏业务
+
+::: tip
+该业务模型已集成 API 接口请求。
+:::
+
+## 前置要求
+
+* 使用 `interceptors-data-models.js`,将 service axios 实例中注入
+
+```javascript
+// interceptors-data-models.js
+import service from './interceptors-anti'
+import { injectRequest } from '@jy/data-models'
+
+injectRequest(service)
+```
+
+
+## 业务模型使用
+```javascript
+// 导入标讯收藏业务模型
+import useQuickCollectBidModel from '@jy/data-models/modules/quick-collect/model'
+
+const APIModel = useQuickCollectBidModel({})
+
+```
+
+### useQuickCollectBidModel()
+#### 参数文档
+
+|      参数       |  描述   |                        类型                         |     默认值      |
+|:-------------:|:-----:|:-------------------------------------------------:|:------------:|
+|  type  | 类型 | String |  -  |
+|  getParams  | 类型 | Function |  () => {}  |
+
+#### 返回值
+```
+return {
+  success: success,
+  list: res.data?.list || [],
+  total: res.data?.total || 0,
+  origin: res.data
+}
+```

+ 45 - 0
data/data-models/modules/quick-collect-bid/api/collect-api.js

@@ -0,0 +1,45 @@
+import { useRequest } from '../../../api'
+import qs from 'qs'
+/**
+ * 标讯收藏列表
+ */
+export function ajaxCollectBidList(data) {
+  return useRequest({
+    url: '/publicapply/bidcoll/list',
+    method: 'post',
+    data: data
+  })
+}
+
+/**
+ * 获取用户个人标签
+ */
+export function ajaxCollectTagsList() {
+  return useRequest({
+    url: '/publicapply/bidcoll/getLabel',
+    method: 'post'
+  })
+}
+
+/**
+ * 添加标签
+ */
+export function ajaxAddLabel(data) {
+  data = qs.stringify(data)
+  return useRequest({
+    url: '/publicapply/bidcoll/addLabel',
+    method: 'post',
+    data: data
+  })
+}
+
+/**
+ * 删除标签
+ */
+export function ajaxDeleteLabel(data) {
+  return useRequest({
+    url: '/publicapply/bidcoll/label',
+    method: 'post',
+    data: data
+  })
+}

+ 1 - 0
data/data-models/modules/quick-collect-bid/index.js

@@ -0,0 +1 @@
+export * from './model'

+ 96 - 0
data/data-models/modules/quick-collect-bid/model/index.js

@@ -0,0 +1,96 @@
+import { ref, computed, toRefs, reactive } from 'vue'
+import { intersection, without } from 'lodash'
+import CollectBidListApi from '../plugins/collect-list'
+import CollectTagsApi from '../plugins/collect-tags'
+
+function useQuickCollectBidModel({ params }) {
+  const useApiModel = new CollectBidListApi(params)
+  const { doQuery } = useApiModel
+  const { list, loading, finished, total } = toRefs(reactive(useApiModel))
+  const selectIds = ref([])
+  const listIds = computed(() => {
+    return list.value.map((item) => item.id || item._id)
+  })
+  const searchResultCount = computed(() => {
+    return total.value > 100000000 ? '超过' + (total.value / 100000000).toFixed(1) + '亿' : total.value
+  })
+
+  const isSelectSomeCheckbox = computed(() => {
+    return selectIds.value.length > 0
+  })
+  const selectCheckboxCount = computed(() => {
+    return selectIds.value.length
+  })
+
+  const isSelectListAllCheckbox = computed(() => {
+    const result = intersection(listIds.value, selectIds.value)
+    if (listIds.value.length === 0) {
+      return false
+    }
+    return result.length === list.value.length
+  })
+
+  /**
+   * 单个item选中、取消操作
+   * @param id
+   * @param [type] - 选中 true \ 取消 false
+   */
+  function doToggleItemSelection(id, type) {
+    if (typeof type !== 'boolean') {
+      type = !selectIds.value.includes(id)
+    }
+    selectIds.value = without(selectIds.value, id)
+    if (type) {
+      selectIds.value.push(id)
+    }
+  }
+
+  /**
+   * 全选当前列表操作
+   * @param [type] - 选中 true \ 取消 false
+   */
+  function doToggleListSelection(type = true) {
+    // 移除ids
+    selectIds.value = without(selectIds.value, ...listIds.value)
+    if (type) {
+      selectIds.value = selectIds.value.concat(listIds.value)
+    }
+  }
+
+  /**
+   * 清空所有选择项
+   */
+  function doClearAllSelection() {
+    selectIds.value = []
+  }
+
+  /**
+   * 获取用户个人标签
+   */
+  const useTagsListApi = new CollectTagsApi()
+  const { getLabelQuery, addLabelQuery, deleteLabelQuery } = useTagsListApi
+  const { tagsList } = toRefs(reactive(useTagsListApi))
+
+  return {
+    list,
+    total,
+    loading,
+    finished,
+    selectIds,
+    listIds,
+    searchResultCount,
+    isSelectSomeCheckbox,
+    selectCheckboxCount,
+    isSelectListAllCheckbox,
+    doToggleItemSelection,
+    doToggleListSelection,
+    doClearAllSelection,
+    doQuery,
+    getLabelQuery,
+    addLabelQuery,
+    deleteLabelQuery,
+    tagsList
+  }
+}
+
+export default useQuickCollectBidModel

+ 46 - 0
data/data-models/modules/quick-collect-bid/plugins/base.js

@@ -0,0 +1,46 @@
+class CollectBidListApiBase {
+  constructor(config = {}) {
+    this.list = []
+    this.loading = false
+    this.finished = false
+    this.total = -1
+    this._getParams = config.getParams || (() => {})
+    // 函数 Hooks
+    this.doQuery = this.runQuery.bind(this)
+  }
+
+  /**
+   * 统一查询 API 入口
+   * @param params - 请求参数
+   * @returns {Promise<{success: boolean, total: number, list: []}>}
+   */
+
+  async runQuery(params) {
+    this.loading = true
+    this.finished = false
+    this.list = []
+    const query = Object.assign({}, params, this._getParams(params))
+    const result = await this.ajaxQuery(query)
+    if (result.success) {
+      this.list = result.list
+      this.total = result.total
+    } else {
+      this.list = []
+      this.total = -1
+    }
+    this.finished = true
+    this.loading = false
+    return result
+  }
+
+  // 需要覆写的API处理逻辑
+  async ajaxQuery(params) {
+    return {
+      success: true,
+      list: [],
+      total: -1
+    }
+  }
+}
+
+export default CollectBidListApiBase

+ 23 - 0
data/data-models/modules/quick-collect-bid/plugins/collect-list.js

@@ -0,0 +1,23 @@
+import CollectBidListApiBase from './base'
+import { ajaxCollectBidList } from '../api/collect-api'
+
+export default class CollectBidListApi extends CollectBidListApiBase {
+  constructor(config) {
+    super(config)
+  }
+
+  /**
+   * 覆写请求
+   */
+  async ajaxQuery(params) {
+    return ajaxCollectBidList(params).then((res) => {
+      let success = res?.error_code === 0
+      return {
+        success: success,
+        list: res.data?.res || [],
+        total: res.data?.count || 0,
+        origin: res.data
+      }
+    })
+  }
+}

+ 82 - 0
data/data-models/modules/quick-collect-bid/plugins/collect-tags.js

@@ -0,0 +1,82 @@
+import { isArray } from 'lodash'
+import { ajaxCollectTagsList, ajaxAddLabel, ajaxDeleteLabel } from '../api/collect-api'
+
+class CollectTagsApi {
+  constructor(config = {}) {
+    this.loading = false
+    this.finished = false
+    this.tagsList = []
+    this.getLabelQuery = this.runQuery.bind(this)
+  }
+
+  /**
+   * 查询 API 入口
+   * @returns {Promise<{success: boolean, tagsList: []}>}
+   */
+
+  async runQuery() {
+    this.loading = true
+    this.finished = false
+    this.tagsList = []
+    const result = await this.ajaxQuery()
+    if (result.success) {
+      this.tagsList = result.list && isArray(result.list) ? result.list.map(item => {
+        return {
+          name: item.lanme,
+          value: item.lid,
+          count: item.count
+        }
+      }).reverse() : []
+    } else {
+      this.tagsList = []
+    }
+    this.finished = true
+    this.loading = false
+    return result
+  }
+
+  async ajaxQuery() {
+    return ajaxCollectTagsList().then((res) => {
+      let success = res?.error_code === 0
+
+      return {
+        success: success,
+        list: res.data || []
+      }
+    })
+  }
+
+  /**
+   * 添加用户标签
+   */
+  async addLabelQuery(params) {
+    return ajaxAddLabel(params).then((res) => {
+      let success = res?.error_code === 0
+      let msg = res?.error_msg
+
+      return {
+        success: success,
+        data: res.data,
+        msg
+      }
+    })
+  }
+
+  /**
+   * 删除用户标签
+   */
+  async deleteLabelQuery(params) {
+    return ajaxDeleteLabel(params).then((res) => {
+      let success = res?.error_code === 0
+      let msg = res?.error_msg
+
+      return {
+        success: success,
+        data: res.data,
+        msg
+      }
+    })
+  }
+}
+
+export default CollectTagsApi