Selaa lähdekoodia

用户订单相关统计;商品添加唯一编码

lrf 2 vuotta sitten
vanhempi
commit
7e8cc81970

+ 1 - 0
.gitignore

@@ -12,3 +12,4 @@ run/
 *.un~
 typings/
 .nyc_output/
+.vscode/

+ 3 - 2
app/controller/shop/config/.goods.js

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ['is_cashBack', 'cb_config', 'sort', 'source', 'url', 'act_tags', 'shop', 'name', 'shot_brief', 'send_time', 'brief', 'file', 'tags', 'status'],
+    requestBody: ['code', 'is_cashBack', 'cb_config', 'sort', 'source', 'url', 'act_tags', 'shop', 'name', 'shot_brief', 'send_time', 'brief', 'file', 'tags', 'status'],
   },
   destroy: {
     params: ['!id'],
@@ -8,7 +8,7 @@ module.exports = {
   },
   update: {
     params: ['!id'],
-    requestBody: ['is_cashBack', 'cb_config', 'sort', 'source', 'url', 'act_tags', 'shop', 'name', 'shot_brief', 'send_time', 'brief', 'file', 'tags', 'status'],
+    requestBody: ['code', 'is_cashBack', 'cb_config', 'sort', 'source', 'url', 'act_tags', 'shop', 'name', 'shot_brief', 'send_time', 'brief', 'file', 'tags', 'status'],
   },
   show: {
     parameters: {
@@ -27,6 +27,7 @@ module.exports = {
         view_num: 'view_num',
         tags: 'tags',
         act_tags: 'act_tags',
+        code: '%code%',
       },
       // options: {
       //   "meta.state": 0 // 默认条件

+ 3 - 0
app/controller/view/config/.user.js

@@ -0,0 +1,3 @@
+module.exports = {
+  toDeal: {},
+};

+ 13 - 0
app/controller/view/user.js

@@ -0,0 +1,13 @@
+'use strict';
+const meta = require('./config/.user.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
+
+// 
+class UserController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.view.user;
+  }
+}
+module.exports = CrudController(UserController, meta);

+ 2 - 0
app/model/shop/goods.js

@@ -23,6 +23,7 @@ const goods = {
   url: { type: String, required: false, zh: '网址' }, //
   is_cashBack: { type: String, required: false, zh: '是否返现' }, // 字典:use
   cb_config: { type: Object, required: false, zh: '返现设置' }, // 返现设置
+  code: { type: String, required: false, zh: '商品编码' }, // 随机生成,8位
 };
 const schema = new Schema(goods, { toJSON: { getters: true, virtuals: true } });
 schema.index({ id: 1 });
@@ -34,6 +35,7 @@ schema.index({ view_num: 1 });
 schema.index({ act_tags: 1 });
 schema.index({ sort: 1 });
 schema.index({ is_cashBack: 1 });
+schema.index({ code: 1 });
 
 schema.plugin(metaPlugin);
 

+ 20 - 0
app/service/shop/goods.js

@@ -13,6 +13,14 @@ class GoodsService extends CrudService {
     this.goodsSpecmodel = this.ctx.model.Shop.GoodsSpec;
     this.tran = new Transaction();
   }
+
+  async beforeCreate(data) {
+    // 为商品随机生成不重复的code
+    if (!data.code) data.code = await this.makeCode();
+    return data;
+  }
+
+
   // 标签查询
   async beforeQuery(filter) {
     const { tags } = filter;
@@ -22,6 +30,18 @@ class GoodsService extends CrudService {
     return filter;
   }
 
+  // 生成商品编码
+  async makeCode() {
+    let code = '';
+    let is_has = false;
+    do {
+      code = Math.random().toString(36).substring(3, 11);
+      const num = await this.model.find({ code });
+      is_has = num > 0;
+    } while (is_has);
+    return code;
+  }
+
   async toDuplicate({ id }) {
     const data = await this.model.findById(id);
     if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到数据');

+ 27 - 0
app/service/view/user.js

@@ -0,0 +1,27 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose-free/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const assert = require('assert');
+
+//
+class UserService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'user');
+    this.orderModel = this.ctx.model.Trade.Order;
+    this.orderDetailModel = this.ctx.model.Trade.OrderDetail;
+    this.afterSaleModel = this.ctx.model.Trade.AfterSale;
+  }
+  async toDeal() {
+    const customer = _.get(this.ctx, 'user._id');
+    if (!customer) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到当前用户信息');
+    // 未支付订单/已支付订单/发货中订单/售后
+    const notPay = await this.orderModel.count({ customer, status: '0' });
+    const isPay = await this.orderDetailModel.count({ customer, status: '1' });
+    const allSend = await this.orderDetailModel.count({ customer, status: '2' });
+    const afterSale = await this.afterSaleModel.count({ customer, status: { $in: [ '0', '1', '2', '3', '4', '5' ] } });
+    return { notPay, isPay, allSend, afterSale };
+  }
+}
+
+module.exports = UserService;

+ 1 - 0
app/z_router/view/index.js

@@ -4,4 +4,5 @@
 module.exports = app => {
   require('./goods')(app); // 商品视图
   require('./shop')(app); // 店铺视图
+  require('./user')(app); // 用户视图
 };

+ 13 - 0
app/z_router/view/user.js

@@ -0,0 +1,13 @@
+'use strict';
+// 路由配置
+const path = require('path');
+const regPath = path.resolve('app', 'public', 'routerRegister');
+const routerRegister = require(regPath);
+const rkey = 'viewUser';
+const ckey = 'view.user';
+const keyZh = '用户相关视图';
+const routes = [{ method: 'get', path: `${rkey}/toDeal`, controller: `${ckey}.toDeal`, name: `${ckey}toDeal`, zh: '用户-订单相关未处理统计' }];
+
+module.exports = app => {
+  routerRegister(app, routes, keyZh, rkey, ckey);
+};