Преглед на файлове

feat: [data-models/quick-monitor] 新增监控业务模型

zhangyuhan преди 1 година
родител
ревизия
e6211ace01

+ 24 - 0
data/data-models/api/index.js

@@ -0,0 +1,24 @@
+// 默认 request
+let globalRequest = {
+  request: function() {
+    console.warn('Stop use default request !!!');
+    return new Promise((resolve, reject) => {
+      setTimeout(() => {
+        resolve({})
+      }, 1000)
+    })
+  }
+}
+
+function injectRequest(server) {
+  globalRequest.request = server
+}
+
+
+function useRequest(...arg) {
+  // 使用传递的 request
+  return globalRequest.request.apply(this, arg)
+}
+
+export { useRequest, injectRequest }
+

+ 1 - 0
data/data-models/index.js

@@ -1 +1,2 @@
 export * from './modules'
+export * from './api'

+ 2 - 1
data/data-models/modules/index.js

@@ -1 +1,2 @@
-export * from './article'
+export * from './article'
+export * from './quick-monitor'

+ 82 - 0
data/data-models/modules/quick-monitor/README.md

@@ -0,0 +1,82 @@
+# useQuickMonitor
+> 监控(项目监控、企业监控、客户监控)业务
+
+## 前置要求
+
+* 使用 `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 useQuickMonitor from '@jy/data-models/modules/quick-monitor/model'
+
+const useMonitor = new useQuickMonitor({
+  type: 'project',
+  params: {
+    id: '项目ID'
+  }
+})
+
+// 监控状态
+// useMonitor.model
+
+// async 初始化,获取当前监控状态信息
+useMonitor.doFetch()
+
+// async 改变监控状态
+useMonitor.doChange()
+```
+
+### useQuickMonitor()
+#### 参数文档
+|  参数  |                  描述                  | 类型 | 默认值 |
+|:----:|:------------------------------------:| :----: |:--:|
+| type |                  用于监控的类型 (project - 项目 | ent - 企业 | client - 客户)                  | String | project |
+| params.id | 对应的监控项目ID、企业ID、客户ID | String | '' |
+
+
+### model 数据模型
+* 可通过 `useMonitor.model` 获取实时数据
+* 或者 `useMonitor.createModel()` 创建初始默认值
+```
+return {
+  // 是否可以展示监控按钮
+  canFollow: false,
+  // 当前监控状态
+  follow: false,
+  // 可扩展字段
+  expands: {
+    // 剩余可用数量
+    surplus: 0,
+    // 已用数量
+    used: 0
+  }
+}
+```
+
+### async doFetch()
+> 初始化,获取当前监控数据
+#### 返回值
+返回值等于 model
+
+### async doChange()
+> 改变监控状
+
+#### 返回值
+```
+{
+  // 操作是否成功
+  success: false,
+  // 接口返回值
+  data: {}
+}
+```

+ 28 - 0
data/data-models/modules/quick-monitor/api/follow-ent.js

@@ -0,0 +1,28 @@
+import qs from 'qs'
+import { useRequest } from '../../../api'
+export function ajaxFollowEntInfo(data) {
+  data = qs.stringify(data)
+  return useRequest({
+    url: '/follow/ent/followCheck',
+    method: 'post',
+    data: data
+  })
+}
+
+export function ajaxFollowEntAdd(data) {
+  data = qs.stringify(data)
+  return useRequest({
+    url: '/follow/ent/addFollow',
+    method: 'post',
+    data
+  })
+}
+
+export function ajaxFollowEntCancel(data) {
+  data = qs.stringify(data)
+  return useRequest({
+    url: '/follow/ent/delFollow',
+    method: 'post',
+    data
+  })
+}

+ 29 - 0
data/data-models/modules/quick-monitor/api/follow-project.js

@@ -0,0 +1,29 @@
+import qs from 'qs'
+import { useRequest } from '../../../api'
+
+export function ajaxFollowProjectInfo(data) {
+  data = qs.stringify(data)
+  return useRequest({
+    url: '/follow/project/check',
+    method: 'post',
+    data
+  })
+}
+
+export function ajaxFollowProjectAdd(data) {
+  data = qs.stringify(data)
+  return useRequest({
+    url: '/follow/project/add',
+    method: 'POST',
+    data
+  })
+}
+
+export function ajaxFollowProjectCancel(data) {
+  data = qs.stringify(data)
+  return useRequest({
+    url: '/follow/project/cancel',
+    method: 'POST',
+    data
+  })
+}

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

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

+ 14 - 0
data/data-models/modules/quick-monitor/model/index.js

@@ -0,0 +1,14 @@
+import MonitorEntApi from '../plugins/ent-api'
+import MonitorProjectApi from '../plugins/project-api'
+
+/**
+ * 一键使用项目监控、企业监控功能
+ * 1. 根据 type 查询当前监控信息
+ * 2. 返回监控信息及改变监控状态的事件钩子 { state, doChange }
+ */
+function useQuickMonitor({ type, params }) {
+  const useMonitorApi = type === 'project' ? MonitorProjectApi : MonitorEntApi
+  return new useMonitorApi(params)
+}
+
+export default useQuickMonitor

+ 75 - 0
data/data-models/modules/quick-monitor/plugins/base.js

@@ -0,0 +1,75 @@
+/**
+ * 监控业务基础类
+ * 1. 统一业务入口
+ * 2. 统一接口返回值
+ */
+class MonitorApiBase {
+  constructor() {
+    this.model = this.createModel()
+    // 通用函数Hooks
+    this.doFetch = this.runFetch.bind(this)
+    this.doChange = this.runChange.bind(this)
+  }
+
+  /**
+   * 获取监控状态并赋值
+   */
+  async runFetch() {
+    let result = this.createModel()
+    this.model = result = await this.ajaxGetState()
+    return result
+  }
+
+  /**
+   * 改变监控状态并返回结果
+   * @param follow
+   * @return {Promise<{}>}
+   */
+  async runChange(follow = this.model.follow) {
+    let result = {
+      // 操作是否成功
+      success: false,
+      // 接口返回值
+      data: {}
+    }
+
+    const doAction = follow ? this.ajaxRemove : this.ajaxAdd
+    result = await doAction()
+    if (result.success) {
+      this.model.follow = !follow
+    }
+    return result
+  }
+
+  createModel() {
+    return {
+      // 是否可以展示监控按钮
+      canFollow: false,
+      // 当前监控状态
+      follow: false,
+      // 可扩展字段
+      expands: {
+        // 剩余可用数量
+        surplus: 0,
+        // 已用数量
+        used: 0
+      }
+    }
+  }
+
+  /**
+   * 提供覆盖的ajax请求
+   * @return {{}}
+   */
+  async ajaxGetState() {
+    return {}
+  }
+  async ajaxAdd() {
+    return {}
+  }
+  async ajaxRemove() {
+    return {}
+  }
+}
+
+export default MonitorApiBase

+ 36 - 0
data/data-models/modules/quick-monitor/plugins/ent-api.js

@@ -0,0 +1,36 @@
+import MonitorApiBase from './base'
+import {
+  ajaxFollowEntInfo,
+  ajaxFollowEntAdd,
+  ajaxFollowEntCancel
+} from '../api/follow-ent'
+
+export default class MonitorEntApi extends MonitorApiBase {
+  constructor({ id }) {
+    super()
+    this.id = id
+  }
+
+  /**
+   * 提供覆盖的ajax请求
+   * @return {{}}
+   */
+  async ajaxGetState() {
+    return ajaxFollowEntInfo({ endId: this.id }).then((res) => {
+      const result = this.createModel()
+      result.canFollow = res?.data?.isShow || false
+      result.follow = res?.data?.followed || false
+
+      if (res?.data?.info) {
+        result.expands = res.data.info
+      }
+      return result
+    })
+  }
+  async ajaxAdd() {
+    return ajaxFollowEntAdd({ entId: this.id })
+  }
+  async ajaxRemove() {
+    return ajaxFollowEntCancel({ entId: this.id })
+  }
+}

+ 61 - 0
data/data-models/modules/quick-monitor/plugins/project-api.js

@@ -0,0 +1,61 @@
+import MonitorApiBase from './base'
+import {
+  ajaxFollowProjectInfo,
+  ajaxFollowProjectAdd,
+  ajaxFollowProjectCancel
+} from '../api/follow-project'
+
+export default class MonitorProjectApi extends MonitorApiBase {
+  constructor({ id, fid = '' }) {
+    super()
+    this.id = id
+    this.fid = fid
+  }
+
+  /**
+   * 提供覆盖的ajax请求
+   * @return {{}}
+   */
+  async ajaxGetState() {
+    return await ajaxFollowProjectInfo({ sid: this.id }).then((res) => {
+      const result = this.createModel()
+      result.canFollow = res?.data?.showFollow || false
+      result.follow = res?.data?.flag || false
+
+      if (res?.data?.info) {
+        result.expands = res.data.info
+      }
+      if (res?.data?.fig) {
+        this.fid = res.data.fig
+      }
+      return result
+    })
+  }
+  async ajaxAdd() {
+    return ajaxFollowProjectAdd({ sid: this.id }).then((res) => {
+      const result = {
+        success: false,
+        data: {}
+      }
+      result.success = res?.error_code === 0 && res?.data?.status === 0
+      result.data = res?.data
+      if (res?.data?.fig) {
+        this.fid = res.data.fig
+      }
+      return result
+    })
+  }
+  async ajaxRemove() {
+    return ajaxFollowProjectCancel({ sid: this.id, fid: this.fid }).then(
+      (res) => {
+        const result = {
+          success: false,
+          data: {}
+        }
+        result.success = res?.error_code === 0 && res?.data?.status === 0
+        result.data = res?.data
+        return result
+      }
+    )
+  }
+}

+ 2 - 1
data/data-models/package.json

@@ -11,7 +11,8 @@
     "axios"
   ],
   "dependencies": {
-    "@jy/util": "workspace:^"
+    "@jy/util": "workspace:^",
+    "qs": "^6.11.2"
   },
   "devDependencies": {
     "vue": "^2.7.16"