Pārlūkot izejas kodu

添加用户行为日志

asd123a20 3 gadi atpakaļ
vecāks
revīzija
8309dd6f2f

+ 0 - 46
service-log/.github/workflows/nodejs.yml

@@ -1,46 +0,0 @@
-# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
-# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
-
-name: Node.js CI
-
-on:
-  push:
-    branches:
-      - main
-      - master
-  pull_request:
-    branches:
-      - main
-      - master
-  schedule:
-    - cron: '0 2 * * *'
-
-jobs:
-  build:
-    runs-on: ${{ matrix.os }}
-
-    strategy:
-      fail-fast: false
-      matrix:
-        node-version: [10]
-        os: [ubuntu-latest, windows-latest, macos-latest]
-
-    steps:
-    - name: Checkout Git Source
-      uses: actions/checkout@v2
-
-    - name: Use Node.js ${{ matrix.node-version }}
-      uses: actions/setup-node@v1
-      with:
-        node-version: ${{ matrix.node-version }}
-
-    - name: Install Dependencies
-      run: npm i -g npminstall && npminstall
-
-    - name: Continuous Integration
-      run: npm run ci
-
-    - name: Code Coverage
-      uses: codecov/codecov-action@v1
-      with:
-        token: ${{ secrets.CODECOV_TOKEN }}

+ 28 - 0
service-log/app/controller/userlog.js

@@ -0,0 +1,28 @@
+'use strict';
+
+const Controller = require('egg').Controller;
+
+class UserlogController extends Controller {
+  async create() {
+    const res = await this.ctx.service.userlog.create(this.ctx.request.body);
+    this.ctx.body = res;
+  }
+  async update() {
+    const res = await this.ctx.service.userlog.update(this.ctx.request.body);
+    this.ctx.body = res;
+  }
+  async delete() {
+    const res = await this.ctx.service.userlog.delete(this.ctx.params);
+    this.ctx.body = res;
+  }
+  async query() {
+    const res = await this.ctx.service.userlog.query(this.ctx.query);
+    this.ctx.body = res;
+  }
+  async fetch() {
+    const res = await this.ctx.service.userlog.fetch(this.ctx.params);
+    this.ctx.body = res;
+  }
+}
+
+module.exports = UserlogController;

+ 23 - 0
service-log/app/model/userlog.js

@@ -0,0 +1,23 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const SchemaDefine = {
+  // 服务名
+  service: { type: String, required: true },
+  // 模块名
+  module: { type: String, required: true },
+  // 方法名
+  method: { type: String, required: true },
+  // 详情
+  details: { type: String, required: true },
+  // 成功失败
+  result: { type: String, required: true },
+  // 用户名(帐号)
+  userName: { type: String, required: false },
+  // 姓名
+  name: { type: String, required: false },
+};
+const schema = new Schema(SchemaDefine);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('userlog', schema, 'userlog');
+};

+ 12 - 5
service-log/app/router.js

@@ -5,9 +5,16 @@
  */
 module.exports = app => {
   const { router, controller } = app;
-  router.post('/api/log/log/create', controller.log.create);
-  router.post('/api/log/log/update', controller.log.update);
-  router.delete('/api/log/log/delete/:id', controller.log.delete);
-  router.get('/api/log/log/query', controller.log.query);
-  router.get('/api/log/log/fetch', controller.log.fetch);
+  // 管理日志
+  router.post('/api/log/adminlog/create', controller.log.create);
+  router.post('/api/log/adminlog/update', controller.log.update);
+  router.delete('/api/log/adminlog/delete/:id', controller.log.delete);
+  router.get('/api/log/adminlog/query', controller.log.query);
+  router.get('/api/log/adminlog/fetch', controller.log.fetch);
+  // 用户行为日志
+  router.post('/api/log/userlog/create', controller.userlog.create);
+  router.post('/api/log/userlog/update', controller.userlog.update);
+  router.delete('/api/log/userlog/delete/:id', controller.userlog.delete);
+  router.get('/api/log/userlog/query', controller.userlog.query);
+  router.get('/api/log/userlog/fetch', controller.userlog.fetch);
 };

+ 75 - 0
service-log/app/service/userlog.js

@@ -0,0 +1,75 @@
+'use strict';
+
+const assert = require('assert');
+const Service = require('egg').Service;
+class UserlogService extends Service {
+  constructor(ctx) {
+    super(ctx);
+    this.model = this.ctx.model.Userlog;
+  }
+  async create({ service, module, method, details, result, userName, name }) {
+    assert(service, '服务不存在');
+    assert(module, '模块不存在');
+    assert(method, '方法不存在');
+    assert(details, '详情不存在');
+    assert(result, '结果不存在');
+    try {
+      const res = await this.model.create({ service, module, method, details, result, userName, name });
+      return { errcode: 0, errmsg: 'ok', data: res };
+    } catch (error) {
+      throw error;
+    }
+  }
+  async update({ id, service, module, method, details, result, userName, name }) {
+    assert(id, 'id不存在');
+    try {
+      await this.model.updateOne({ _id: id }, { service, module, method, details, result, userName, name });
+      return { errcode: 0, errmsg: 'ok', data: '' };
+    } catch (error) {
+      throw error;
+    }
+  }
+  async delete({ id }) {
+    assert(id, 'id不存在');
+    try {
+      await this.model.remove({ _id: id });
+      return { errcode: 0, errmsg: 'ok', data: '' };
+    } catch (error) {
+      throw error;
+    }
+  }
+  async query({ skip, limit, service, module, method, result, userName, name }) {
+    const filter = {};
+    const arr = { service, module, method, result, userName, name };
+    for (const e in arr) {
+      const data = `{ "${e}": "${arr[e]}" }`;
+      if (arr[e]) {
+        filter.$or = [];
+        filter.$or.push(JSON.parse(data));
+      }
+    }
+    try {
+      const total = await this.model.find({ ...filter });
+      let res;
+      if (skip && limit) {
+        res = await this.model.find({ ...filter }).skip(Number(skip) * Number(limit)).limit(Number(limit));
+      } else {
+        res = await this.model.find({ ...filter });
+      }
+      return { errcode: 0, errmsg: 'ok', data: res, total: total.length };
+    } catch (error) {
+      throw error;
+    }
+  }
+  async fetch({ id }) {
+    assert(id, 'id不存在');
+    try {
+      const res = await this.model.findOne({ _id: id });
+      return { errcode: 0, errmsg: 'ok', data: res };
+    } catch (error) {
+      throw error;
+    }
+  }
+}
+
+module.exports = UserlogService;