Explorar o código

新增newfetch、newquery方法

reloaded %!s(int64=5) %!d(string=hai) anos
pai
achega
4e4839855e
Modificáronse 3 ficheiros con 19 adicións e 5 borrados
  1. 10 2
      app/controller/product.js
  2. 2 0
      app/router.js
  3. 7 3
      app/service/product.js

+ 10 - 2
app/controller/product.js

@@ -11,8 +11,16 @@ class ProductController extends Controller {
     super(ctx);
     this.service = this.ctx.service.product;
   }
-  async index() {
-    const res = await this.service.query(this.ctx.query);
+
+  async newfetch() {
+    const data = await this.service.newfetch(this.ctx.params);
+    this.ctx.ok({ data });
+  }
+
+  async newquery() {
+    console.log('111111111111111111111111111111111111111111111111');
+
+    const res = await this.service.newquery(this.ctx.query);
     this.ctx.ok({ ...res });
   }
 }

+ 2 - 0
app/router.js

@@ -12,6 +12,8 @@ module.exports = app => {
   router.post('user', '/api/market/user/:id', controller.user.update);
 
   // 产品信息表设置路由
+  router.get('product', '/api/market/product/newquery', controller.product.newquery);
+  router.get('product', '/api/market/product/newfetch/:id', controller.product.newfetch);
   router.resources('product', '/api/market/product', controller.product); // index、create、show、destroy
   router.post('product', '/api/market/product/update/:id', controller.product.update);
 

+ 7 - 3
app/service/product.js

@@ -13,7 +13,7 @@ class ProductService extends CrudService {
     this.model = this.ctx.model.Product;
   }
 
-  async fetch({ id }) {
+  async newfetch({ id }) {
     let product = await this.model.findById(id);
     if (product.is_display) {
       product = JSON.parse(JSON.stringify(product));
@@ -27,12 +27,16 @@ class ProductService extends CrudService {
 
   }
 
-  async query({ skip, limit, ...info }) {
+  async newquery({ skip, limit, ...info }) {
+    console.log('222222222222222222222222222222222222222222222222');
+
     const products = await this.model.find(info);
     const _products = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
     const data = [];
     for (const _product of _products) {
-      const newproduct = await this.fetch({ id: _product._id });
+      console.log(_product);
+
+      const newproduct = await this.newfetch({ id: _product._id });
       data.push(newproduct);
     }
     return { data, total: products.length };