lrf 2 năm trước cách đây
mục cha
commit
19dc78900a

+ 41 - 0
app/controller/user/config/.storeGoods.js

@@ -0,0 +1,41 @@
+module.exports = {
+  create: {
+    requestBody: ['customer', 'goods', 'time'],
+  },
+  destroy: {
+    params: ['!id'],
+    service: 'delete',
+  },
+  update: {
+    params: ['!id'],
+    requestBody: ['customer', 'goods', 'time'],
+  },
+  show: {
+    parameters: {
+      params: ['!id'],
+    },
+    service: 'fetch',
+  },
+  index: {
+    parameters: {
+      query: {
+        'meta.createdAt@start': 'meta.createdAt@start',
+        'meta.createdAt@end': 'meta.createdAt@end',
+        customer: 'customer',
+        goods: 'goods',
+        'time@start': 'time@start',
+        'time@end': 'time@end',
+      },
+      // options: {
+      //   "meta.state": 0 // 默认条件
+      // },
+    },
+    service: 'query',
+    options: {
+      query: ['skip', 'limit'],
+      sort: ['time'],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 42 - 0
app/controller/user/config/.storeShop.js

@@ -0,0 +1,42 @@
+module.exports = {
+  create: {
+    requestBody: ['customer', 'shop', 'time'],
+  },
+  destroy: {
+    params: ['!id'],
+    service: 'delete',
+  },
+  update: {
+    params: ['!id'],
+    requestBody: ['customer', 'shop', 'time'],
+  },
+  show: {
+    parameters: {
+      params: ['!id'],
+    },
+    service: 'fetch',
+  },
+  index: {
+    parameters: {
+      query: {
+        'meta.createdAt@start': 'meta.createdAt@start',
+        'meta.createdAt@end': 'meta.createdAt@end',
+        customer: 'customer',
+        shop: 'shop',
+        time: 'time',
+        'time@start': 'time@start',
+        'time@end': 'time@end',
+      },
+      // options: {
+      //   "meta.state": 0 // 默认条件
+      // },
+    },
+    service: 'query',
+    options: {
+      query: ['skip', 'limit'],
+      sort: ['time'],
+      desc: true,
+      count: true,
+    },
+  },
+};

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

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

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

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

+ 23 - 0
app/model/user/storeGoods.js

@@ -0,0 +1,23 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
+
+// 商品收藏
+const storeGoods = {
+  customer: { type: String, required: false, zh: '顾客', ref: 'User.User' }, //
+  goods: { type: String, required: false, zh: '商品', ref: 'Shop.Goods' }, //
+  time: { type: String, required: false, zh: '时间' }, //
+};
+const schema = new Schema(storeGoods, { toJSON: { getters: true, virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.index({ customer: 1 });
+schema.index({ goods: 1 });
+schema.index({ time: 1 });
+
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('StoreGoods', schema, 'storeGoods');
+};

+ 23 - 0
app/model/user/storeShop.js

@@ -0,0 +1,23 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
+
+// 店铺收藏
+const storeShop = {
+  customer: { type: String, required: false, zh: '顾客', ref: 'User.User' }, //
+  shop: { type: String, required: false, zh: '店铺', ref: 'Shop.Shop' }, //
+  time: { type: String, required: false, zh: '时间' }, //
+};
+const schema = new Schema(storeShop, { toJSON: { getters: true, virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.index({ customer: 1 });
+schema.index({ shop: 1 });
+schema.index({ time: 1 });
+
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('StoreShop', schema, 'storeShop');
+};

+ 33 - 0
app/service/user/storeGoods.js

@@ -0,0 +1,33 @@
+'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');
+const moment = require('moment');
+
+//
+class StoreGoodsService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'storegoods');
+    this.model = this.ctx.model.User.StoreGoods;
+  }
+  async create({ goods }) {
+    assert(goods, '缺少商品信息');
+    const customer = _.get(this.ctx, 'user._id');
+    assert(customer, '缺少用户信息');
+    let data = await this.model.findOne({ customer, goods });
+    if (data) {
+      await this.model.deleteOne({ customer, goods });
+      return { msg: '取消收藏', result: false };
+    }
+    data = await this.model.create({ customer, goods, time: moment().format('YYYY-MM-DD HH:mm:ss') });
+    return { msg: '收藏成功', result: true };
+  }
+  // 检查是否收藏商品
+  async check({ goods, customer }) {
+    const num = await this.model.count({ goods, customer });
+    return num > 0;
+  }
+}
+
+module.exports = StoreGoodsService;

+ 37 - 0
app/service/user/storeShop.js

@@ -0,0 +1,37 @@
+'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');
+const moment = require('moment');
+
+//
+class StoreShopService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'storeshop');
+    this.model = this.ctx.model.User.StoreShop;
+  }
+
+  async create({ shop }) {
+    assert(shop, '缺少店铺信息');
+    const customer = _.get(this.ctx, 'user._id');
+    assert(customer, '缺少用户信息');
+    let data = await this.model.findOne({ customer, shop });
+    if (data) {
+      await this.model.deleteOne({ customer, shop });
+      return { msg: '取消收藏', result: false };
+    }
+    data = await this.model.create({ customer, shop, time: moment().format('YYYY-MM-DD HH:mm:ss') });
+    return { msg: '收藏成功', result: true };
+  }
+
+  // 用户查看收藏店铺
+  async userView() {}
+  // 检查是否收藏店铺
+  async check({ shop, customer }) {
+    const num = await this.model.count({ shop, customer });
+    return num > 0;
+  }
+}
+
+module.exports = StoreShopService;

+ 2 - 0
app/z_router/user/index.js

@@ -7,4 +7,6 @@ module.exports = app => {
   require('./address')(app); // 收货地址
   require('./point')(app); // 积分
   require('./userCoupon')(app); // 用户优惠券
+  require('./storeShop')(app); // 收藏店铺
+  require('./storeGoods')(app); // 收藏商品
 };

+ 16 - 0
app/z_router/user/storeGoods.js

@@ -0,0 +1,16 @@
+'use strict';
+// 路由配置
+const path = require('path');
+const regPath = path.resolve('app', 'public', 'routerRegister');
+const routerRegister = require(regPath);
+const rkey = 'storeGoods';
+const ckey = 'user.storeGoods';
+const keyZh = '收藏商品';
+const routes = [
+  { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
+  { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, name: `${ckey}Create`, zh: `创建${keyZh}` },
+];
+
+module.exports = app => {
+  routerRegister(app, routes, keyZh, rkey, ckey);
+};

+ 16 - 0
app/z_router/user/storeShop.js

@@ -0,0 +1,16 @@
+'use strict';
+// 路由配置
+const path = require('path');
+const regPath = path.resolve('app', 'public', 'routerRegister');
+const routerRegister = require(regPath);
+const rkey = 'storeShop';
+const ckey = 'user.storeShop';
+const keyZh = '收藏店铺';
+const routes = [
+  { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
+  { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, name: `${ckey}Create`, zh: `创建${keyZh}` },
+];
+
+module.exports = app => {
+  routerRegister(app, routes, keyZh, rkey, ckey);
+};