Browse Source

新增积分管理,积分配置,积分日志 服务

asd123a20 3 years ago
parent
commit
464f42c862

+ 31 - 0
service-integral/app/controller/integral.js

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

+ 23 - 0
service-integral/app/controller/integralConfig.js

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

+ 23 - 0
service-integral/app/controller/integralLog.js

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

+ 0 - 27
service-integral/app/controller/power.js

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

+ 0 - 27
service-integral/app/controller/vip.js

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

+ 15 - 0
service-integral/app/model/integral.js

@@ -0,0 +1,15 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const SchemaDefine = {
+  // 用户名(帐号)
+  openid: { type: String, required: false },
+  // 姓名
+  name: { type: String, required: false },
+  // 积分
+  integral: { type: Number, required: false },
+};
+const schema = new Schema(SchemaDefine);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('integral', schema, 'integral');
+};

+ 15 - 0
service-integral/app/model/integralConfig.js

@@ -0,0 +1,15 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const SchemaDefine = {
+  // 服务名
+  service: { type: String, required: true },
+  // 模块名
+  module: { type: String, required: true },
+  // 方法名
+  method: { type: String, required: true },
+};
+const schema = new Schema(SchemaDefine);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('integralConfig', schema, 'integralConfig');
+};

+ 17 - 0
service-integral/app/model/integralLog.js

@@ -0,0 +1,17 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const SchemaDefine = {
+  // 用户名(帐号)
+  openid: { type: String, required: false },
+  // 姓名
+  name: { type: String, required: false },
+  // 积分
+  integral: { type: Number, required: false },
+  // 创建时间
+  createTime: { type: String, required: false },
+};
+const schema = new Schema(SchemaDefine);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('integralLog', schema, 'integralLog');
+};

+ 0 - 15
service-integral/app/model/power.js

@@ -1,15 +0,0 @@
-'use strict';
-const Schema = require('mongoose').Schema;
-const SchemaDefine = {
-  // 标识
-  vipTab: { type: String, required: false },
-  // openid
-  openid: { type: String, required: true },
-  // 文章IDlist
-  list: { type: Array, required: true },
-};
-const schema = new Schema(SchemaDefine);
-module.exports = app => {
-  const { mongoose } = app;
-  return mongoose.model('power', schema, 'power');
-};

+ 0 - 29
service-integral/app/model/vip.js

@@ -1,29 +0,0 @@
-'use strict';
-const Schema = require('mongoose').Schema;
-const SchemaDefine = {
-  // 头像
-  thumbnail: { type: String, required: false },
-  // 昵称
-  name: { type: String, required: true },
-  // 手机号
-  phone: { type: String, required: true },
-  // 单位
-  company: { type: String, required: false },
-  // 标识
-  vipTab: { type: String, required: false },
-  // 标识
-  tab: { type: String, required: false },
-  // openid
-  openid: { type: String, required: true },
-  // 状态:0=正常,1=过期
-  status: { type: String, required: true },
-  // 创建时间
-  startTime: { type: Number, required: true },
-  // 结束时间
-  endTime: { type: Number, required: true },
-};
-const schema = new Schema(SchemaDefine);
-module.exports = app => {
-  const { mongoose } = app;
-  return mongoose.model('vip', schema, 'vip');
-};

+ 17 - 11
service-integral/app/router.js

@@ -5,15 +5,21 @@
  */
 module.exports = app => {
   const { router, controller } = app;
-  // vip
-  router.post('/api/clientVip/vip/create', controller.vip.create);
-  router.post('/api/clientVip/vip/update', controller.vip.update);
-  router.post('/api/clientVip/vip/proceed', controller.vip.proceed);
-  router.delete('/api/clientVip/vip/delete/:id', controller.vip.delete);
-  router.get('/api/clientVip/vip/query', controller.vip.query);
-  // 权限
-  router.post('/api/clientVip/power/create', controller.power.create);
-  router.post('/api/clientVip/power/update', controller.power.update);
-  router.delete('/api/clientVip/power/delete/:id', controller.power.delete);
-  router.get('/api/clientVip/power/query', controller.power.query);
+  // 配置
+  router.post('/api/integral/integralConfig/create', controller.integralConfig.create);
+  router.post('/api/integral/integralConfig/update', controller.integralConfig.update);
+  router.delete('/api/integral/integralConfig/delete/:id', controller.integralConfig.delete);
+  router.get('/api/integral/integralConfig/query', controller.integralConfig.query);
+  // 积分日志
+  router.post('/api/integral/integralLog/create', controller.integralLog.create);
+  router.post('/api/integral/integralLog/update', controller.integralLog.update);
+  router.delete('/api/integral/integralLog/delete/:id', controller.integralLog.delete);
+  router.get('/api/integral/integralLog/query', controller.integralLog.query);
+  // 积分管理
+  router.post('/api/integral/integral/create', controller.integral.create);
+  router.post('/api/integral/integral/update', controller.integral.update);
+  router.delete('/api/integral/integral/delete/:id', controller.integral.delete);
+  router.get('/api/integral/integral/query', controller.integral.query);
+  router.post('/api/integral/integral/increase', controller.integral.increase);
+  router.post('/api/integral/integral/reduce', controller.integral.reduce);
 };

+ 0 - 17
service-integral/app/schedule/inspect.js

@@ -1,17 +0,0 @@
-'use strict';
-const moment = require('moment');
-module.exports = {
-  schedule: {
-    interval: '1m', // 1 分钟间隔
-    type: 'all', // 指定所有的 worker 都需要执行
-  },
-  async task(ctx) {
-    const res = await ctx.model.Vip.find({ status: 0, vipTab: 'svip' });
-    for (let i = 0; i < res.length; i++) {
-      const time = moment().valueOf();
-      if (res[i].endTime <= time) {
-        await ctx.model.Vip.updateOne({ _id: res[i]._id }, { endTime: 0, status: '1' });
-      }
-    }
-  },
-};

+ 92 - 0
service-integral/app/service/integral.js

@@ -0,0 +1,92 @@
+'use strict';
+
+const assert = require('assert');
+const Service = require('egg').Service;
+class integralService extends Service {
+  constructor(ctx) {
+    super(ctx);
+    this.model = this.ctx.model.Integral;
+  }
+  async create({ openid, name, integral }) {
+    assert(name, '姓名不存在');
+    assert(openid, 'openid不存在');
+    assert(integral, '积分不存在');
+    try {
+      const res = await this.model.create({ openid, name, integral });
+      return { errcode: 0, errmsg: 'ok', data: res };
+    } catch (error) {
+      throw error;
+    }
+  }
+  async update({ id, openid, name, integral }) {
+    assert(id, 'id不存在');
+    try {
+      await this.model.updateOne({ _id: id }, { openid, name, integral });
+      return { errcode: 0, errmsg: 'ok', data: '' };
+    } catch (error) {
+      throw error;
+    }
+  }
+  async delete({ id }) {
+    assert(id, 'id不存在');
+    try {
+      await this.model.deleteOne({ _id: id });
+      return { errcode: 0, errmsg: 'ok', data: '' };
+    } catch (error) {
+      throw error;
+    }
+  }
+  async query({ skip, limit, openid, name, integral }) {
+    const filter = {};
+    const arr = { openid, name, integral };
+    for (const e in arr) {
+      const data = `{ "${e}": { "$regex": "${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 increase({ openid, integral }) {
+    assert(openid, 'openid不存在');
+    try {
+      const one = await this.model.findOne({ openid });
+      if (one) {
+        const total = integral + one.integral;
+        await this.model.updateOne({ openid }, { integral: total });
+        return { errcode: 0, errmsg: 'ok', data: '' };
+      }
+      return { errcode: -1001, errmsg: '未查询到用户', data: '' };
+    } catch (error) {
+      throw error;
+    }
+  }
+  async reduce({ openid, integral }) {
+    assert(openid, 'openid不存在');
+    try {
+      const one = await this.model.findOne({ openid });
+      if (one) {
+        const total = one.integral - integral;
+        await this.model.updateOne({ openid }, { integral: total });
+        return { errcode: 0, errmsg: 'ok', data: '' };
+      }
+      return { errcode: -1001, errmsg: '未查询到用户', data: '' };
+    } catch (error) {
+      throw error;
+    }
+  }
+}
+
+module.exports = integralService;

+ 61 - 0
service-integral/app/service/integralConfig.js

@@ -0,0 +1,61 @@
+'use strict';
+
+const assert = require('assert');
+const Service = require('egg').Service;
+class integralConfigService extends Service {
+  constructor(ctx) {
+    super(ctx);
+    this.model = this.ctx.model.IntegralConfig;
+  }
+  async create({ service, module, method }) {
+    try {
+      const res = await this.model.create({ service, module, method });
+      return { errcode: 0, errmsg: 'ok', data: res };
+    } catch (error) {
+      throw error;
+    }
+  }
+  async update({ id, service, module, method }) {
+    assert(id, 'id不存在');
+    try {
+      await this.model.updateOne({ _id: id }, { service, module, method });
+      return { errcode: 0, errmsg: 'ok', data: '' };
+    } catch (error) {
+      throw error;
+    }
+  }
+  async delete({ id }) {
+    assert(id, 'id不存在');
+    try {
+      await this.model.deleteOne({ _id: id });
+      return { errcode: 0, errmsg: 'ok', data: '' };
+    } catch (error) {
+      throw error;
+    }
+  }
+  async query({ skip, limit, service, module, method }) {
+    const filter = {};
+    const arr = { service, module, method };
+    for (const e in arr) {
+      const data = `{ "${e}": { "$regex": "${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;
+    }
+  }
+}
+
+module.exports = integralConfigService;

+ 13 - 11
service-integral/app/service/power.js

@@ -2,26 +2,28 @@
 
 const assert = require('assert');
 const Service = require('egg').Service;
-class PowerService extends Service {
+const moment = require('moment');
+class integralLogService extends Service {
   constructor(ctx) {
     super(ctx);
-    this.model = this.ctx.model.Power;
+    this.model = this.ctx.model.IntegralLog;
   }
-  async create({ vipTab, openid, list }) {
-    assert(list, 'ID列表不存在');
+  async create({ openid, name, integral }) {
+    assert(name, '姓名不存在');
     assert(openid, 'openid不存在');
-    assert(vipTab, '会员标识不存在');
+    assert(integral, '积分不存在');
     try {
-      const res = await this.model.create({ vipTab, openid, list });
+      const createTime = moment().valueOf();
+      const res = await this.model.create({ openid, name, integral, createTime });
       return { errcode: 0, errmsg: 'ok', data: res };
     } catch (error) {
       throw error;
     }
   }
-  async update({ id, vipTab, list }) {
+  async update({ id, openid, name, integral }) {
     assert(id, 'id不存在');
     try {
-      await this.model.updateOne({ _id: id }, { vipTab, list });
+      await this.model.updateOne({ _id: id }, { openid, name, integral });
       return { errcode: 0, errmsg: 'ok', data: '' };
     } catch (error) {
       throw error;
@@ -36,9 +38,9 @@ class PowerService extends Service {
       throw error;
     }
   }
-  async query({ skip, limit, vipTab, openid, list }) {
+  async query({ skip, limit, openid, name, integral }) {
     const filter = {};
-    const arr = { vipTab, openid, list };
+    const arr = { openid, name, integral };
     for (const e in arr) {
       let datas;
       if (e === 'list') {
@@ -66,4 +68,4 @@ class PowerService extends Service {
   }
 }
 
-module.exports = PowerService;
+module.exports = integralLogService;

+ 0 - 92
service-integral/app/service/vip.js

@@ -1,92 +0,0 @@
-'use strict';
-
-const assert = require('assert');
-const Service = require('egg').Service;
-const moment = require('moment');
-class VipService extends Service {
-  constructor(ctx) {
-    super(ctx);
-    this.model = this.ctx.model.Vip;
-  }
-  async create({ name, thumbnail, phone, company, tab, openid, vipTab }) {
-    assert(name, '昵称不存在');
-    assert(phone, '电话不存在');
-    assert(openid, 'openid不存在');
-    assert(vipTab, '会员标识不存在');
-    const filter = {};
-    const arr = { phone, openid };
-    for (const e in arr) {
-      const data = `{ "${e}": "${arr[e]}" }`;
-      if (arr[e]) {
-        filter.$or = [];
-        filter.$or.push(JSON.parse(data));
-      }
-    }
-    const total = await this.model.find({ ...filter });
-    if (total.length > 0) {
-      return { errcode: -1001, errmsg: '用户手机号或微信已存在' };
-    }
-    try {
-      const startTime = moment().valueOf();
-      const endTime = vipTab === 'svip' ? moment().add(1, 'Y').valueOf() : 0;
-      const res = await this.model.create({ name, thumbnail, phone, company, tab, openid, status: '0', startTime, endTime, vipTab });
-      return { errcode: 0, errmsg: 'ok', data: res };
-    } catch (error) {
-      throw error;
-    }
-  }
-  async update({ id, name, thumbnail, phone, company, tab, status }) {
-    assert(id, 'id不存在');
-    try {
-      await this.model.updateOne({ _id: id }, { name, thumbnail, phone, company, tab, status });
-      return { errcode: 0, errmsg: 'ok', data: '' };
-    } catch (error) {
-      throw error;
-    }
-  }
-  // 续费 修改结束时间
-  async proceed({ id }) {
-    assert(id, 'id不存在');
-    try {
-      const endTime = moment().add(1, 'Y').valueOf();
-      await this.model.updateOne({ _id: id }, { endTime });
-      return { errcode: 0, errmsg: 'ok', data: '' };
-    } catch (error) {
-      throw error;
-    }
-  }
-  async delete({ id }) {
-    assert(id, 'id不存在');
-    try {
-      await this.model.deleteOne({ _id: id });
-      return { errcode: 0, errmsg: 'ok', data: '' };
-    } catch (error) {
-      throw error;
-    }
-  }
-  async query({ skip, limit, name, phone, company, tab, openid, status, startTime, endTime, vipTab }) {
-    const filter = {};
-    const arr = { name, phone, company, tab, openid, status, startTime, endTime, vipTab };
-    for (const e in arr) {
-      const data = `{ "${e}": { "$regex": "${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;
-    }
-  }
-}
-
-module.exports = VipService;

+ 1 - 1
service-integral/package.json

@@ -10,7 +10,7 @@
     "egg": "^2.15.1",
     "egg-mongoose": "^3.3.1",
     "egg-scripts": "^2.11.0",
-    "moment": "^2.29.1"
+    "moment": "^2.29.3"
   },
   "devDependencies": {
     "autod": "^3.0.1",