zs 2 vuotta sitten
vanhempi
commit
cfe611fda0

+ 1 - 0
app/controller/program.js

@@ -8,6 +8,7 @@ const {
 // 节目表
 class ProgramController extends Controller {
   constructor(ctx) {
+    console.log(ctx);
     super(ctx);
     this.service = this.ctx.service.program;
   }

+ 14 - 0
app/controller/shop.js

@@ -0,0 +1,14 @@
+'use strict';
+const Controller = require('egg').Controller;
+class Shop extends Controller {
+  async Shop() {
+    const { ctx } = this;
+    const req = ctx.request.body;
+    const res = await ctx.service.shopService.Shop(req);
+    ctx.body = {
+      state: 200,
+      msg: res,
+    };
+  }
+}
+module.exports = Shop;

+ 17 - 0
app/model/ShopModel.js

@@ -0,0 +1,17 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+// 店铺
+const shop = {
+  name: { type: String, zh: '店铺名称' },
+  notice: { type: String, zh: '公告内容' },
+  logo: { type: Object, zh: '首页图片' }, //
+};
+const Shopschema = new Schema(shop, {
+  toJSON: { getters: true, virtuals: true },
+});
+Shopschema.index({ notice: 1 });
+Shopschema.index({ name: 1 });
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('ShopModel', Shopschema, 'shop');
+};

+ 2 - 1
app/router.js

@@ -5,8 +5,9 @@
  */
 module.exports = app => {
   const { router, controller } = app;
-  const { routePrefix } = app.config;
   router.get('/', controller.home.index);
   require('./z_router/admin')(app); // 管理員
   require('./z_router/program')(app); // 节目表
+  // 增加数据
+  router.post('/projectadmin/api/shop', controller.shop.Shop);
 };

+ 21 - 0
app/service/shopService.js

@@ -0,0 +1,21 @@
+'use strict';
+const Service = require('egg').Service;
+class ShopService extends Service {
+  async Shop(obj) {
+    const { ctx } = this;
+    // 存储数据
+    // 留神!!!!ctx.model.xxx中xxx指的是model的文件名首字母大写
+    const ShopList = new ctx.model.ShopModel({
+      name: obj.name,
+      // 姓名
+      notice: obj.notice,
+      // 护照号
+      logo: obj.logo || {},
+    });
+    // 数据保留到数据库
+    ShopList.save();
+    return '增加胜利';
+  }
+
+}
+module.exports = ShopService;

+ 3 - 3
config/config.default.js

@@ -39,9 +39,9 @@ module.exports = appInfo => {
   config.mongoose = {
     url: `mongodb://localhost:27017/${config.dbName}`,
     options: {
-      user: 'admin',
-      pass: 'admin',
-      authSource: 'admin',
+      // user: 'admin',
+      // pass: 'admin',
+      // authSource: 'admin',
       useNewUrlParser: true,
       useCreateIndex: true,
     },

+ 5 - 0
config/plugin.js

@@ -13,3 +13,8 @@ exports.redis = {
 exports.multiTenancy = {
   enable: true,
 };
+
+exports.mongoose = {
+  enable: true,
+  package: 'egg-mongoose',
+};