Browse Source

完成服务和测试

dygapp 6 years ago
parent
commit
c6ab8084ee

+ 8 - 2
app/controller/.news.js

@@ -4,20 +4,26 @@ module.exports = {
     "parameters": {
     "parameters": {
       "query": ["!column"],
       "query": ["!column"],
     },
     },
-    "requestBody": ["!title", "!content", "picurl", "top", "tags", "attachment", "issuer"],
+    "requestBody": ["!title", "!content", "picurl", "top", "tags", "attachment", "issuer", "remark"],
   },
   },
   // 修改新闻信息
   // 修改新闻信息
   "update": {
   "update": {
     "parameters": {
     "parameters": {
       "query": ["!id"],
       "query": ["!id"],
     },
     },
-    "requestBody": ["title", "content", "picurl", "top", "tags", "attachment", "issuer"],
+    "requestBody": ["title", "content", "picurl", "top", "tags", "attachment", "issuer", "remark"],
+    "options": {
+      "projection": "+content",
+    },
   },
   },
   // 获取新闻详情
   // 获取新闻详情
   "fetch": {
   "fetch": {
     "parameters": {
     "parameters": {
       "query": ["!id"],
       "query": ["!id"],
     },
     },
+    "options": {
+      "projection": "+content",
+    },
   },
   },
   // 删除新闻
   // 删除新闻
   "delete": {
   "delete": {

+ 48 - 0
app/controller/.site.js

@@ -0,0 +1,48 @@
+module.exports = {
+  // 添加分站信息
+  "create": {
+    "parameters": {
+      "requestBody": ["!name", "!site", "!domain", "banner", "!copyright", "theme", "!content", "remark"],
+    },
+  },
+  // 修改分站信息
+  "update": {
+    "parameters": {
+      "query": ["!id"],
+    },
+    "requestBody": ["name", "domain", "banner", "copyright", "theme", "content", "remark"],
+    "options": {
+      "projection": "+content",
+    },
+  },
+  // 获取分站详情
+  "fetch": {
+    "parameters": {
+      "query": ["!site"],
+    },
+    "options": {
+      "projection": "+content",
+    },
+  },
+  // 删除分站信息
+  "delete": {
+    "parameters": {
+      "query": ["!id"],
+    },
+  },
+  // 查询分站列表
+  "query": {
+    "parameters": {
+      "query": ["status"],
+    },
+    "options": {
+      "query": ["skip", "limit"],
+      "sort": ["meta.createdAt"],
+      "desc": true,
+      "count": true,
+      "projection": {
+        "copyright": 0
+      }
+    }
+  },
+};

+ 62 - 0
app/controller/site.js

@@ -0,0 +1,62 @@
+'use strict';
+
+const _ = require('lodash');
+const meta = require('./.site.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+
+class SiteController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.site;
+  }
+
+  // 获得当前分站信息
+  async content() {
+    // TODO: 检查用户信息
+    const site = this.ctx.tenant;
+    if (!_.isString(site)) throw new BusinessError(ErrorCode.BADPARAM, '租户信息不存在');
+
+    const res = await this.service.fetch({ site }, {
+      projection: { content: 1 }
+    });
+    this.ctx.ok({ data: res && res.content });
+  }
+
+  // 获得当前分站信息
+  async config_fetch() {
+    // TODO: 检查用户信息
+    const site = this.ctx.tenant;
+    if (!_.isString(site)) throw new BusinessError(ErrorCode.BADPARAM, '租户信息不存在');
+
+    const res = await this.service.fetch({ site }, { projection: '+content' });
+    this.ctx.ok({ data: res });
+  }
+
+  // 获得当前分站信息
+  async config_get() {
+    // TODO: 检查用户信息
+    const site = this.ctx.tenant;
+    if (!_.isString(site)) throw new BusinessError(ErrorCode.BADPARAM, '租户信息不存在');
+
+    const res = await this.service.fetch({ site });
+    this.ctx.ok({ data: res });
+  }
+
+  // 分站配置自身信息
+  async config_set() {
+    // TODO: 检查用户信息
+    const userid = this.ctx.userid;
+    if (!_.isString(userid)) throw new BusinessError(ErrorCode.NOT_LOGIN);
+    const site = this.ctx.tenant;
+    if (!_.isString(site)) throw new BusinessError(ErrorCode.BADPARAM, '租户信息不存在');
+
+    const { name, domain, banner, copyright, theme, content, remark } = this.ctx.request.body;
+    console.log('*********content**********\n', content);
+    const res = await this.service.update({ site }, { name, domain, banner, copyright, theme, content, remark }, { projection: '+content' });
+    this.ctx.ok({ data: res });
+  }
+}
+
+module.exports = CrudController(SiteController, meta);

+ 22 - 0
app/model/site.js

@@ -0,0 +1,22 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+
+// 站点信息
+const SchemaDefine = {
+  site: { type: String, required: true, maxLength: 64, unique: true }, // 分站标识
+  name: { type: String, required: true, maxLength: 64 }, // 站点名称
+  status: { type: String, default: '0', maxLength: 64 }, // 站点状态:0-正常;1-未激活;2-注销
+  domain: { type: String, required: true, maxLength: 128 }, // 域名
+  banner: { type: String, required: false, maxLength: 256 }, // 网站Banner图片URL
+  copyright: { type: String, required: false, maxLength: 256 }, // 版权声明
+  theme: { type: String, default: 'default', maxLength: 64 }, // 样式主题
+  content: { type: String, required: true, maxLength: 102400, select: false }, // 站点介绍/联系方式
+  remark: { type: String, maxLength: 500 }, // 备注
+};
+const schema = new Schema(SchemaDefine, { 'multi-tenancy': false, toJSON: { virtuals: true } });
+schema.plugin(metaPlugin);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('SiteInfo', schema, 'cms_site');
+};

+ 23 - 0
app/router.js

@@ -6,4 +6,27 @@
 module.exports = app => {
 module.exports = app => {
   const { router, controller } = app;
   const { router, controller } = app;
   router.get('/', controller.home.index);
   router.get('/', controller.home.index);
+
+  // 网站数据接口
+  router.get('/api/news/list', controller.news.list); // 新闻信息列表,隐藏删除信息,按照置顶和时间排序
+  router.get('/api/news/fetch', controller.news.fetch); // 获取新闻详情
+  router.get('/api/site/config', controller.site.config_get); // 获取站点基本信息
+  router.get('/api/site/content', controller.site.content); // 获取站点详细信息(关于我们、联系方式)
+
+  // 管理接口
+  // 【分站】新闻接口
+  router.get('/admin/news/query', controller.news.query);// 查询新闻信息
+  router.get('/admin/news/fetch', controller.news.fetch);// 获得新闻详情
+  router.post('/admin/news/create', controller.news.create);// 发布新闻信息
+  router.post('/admin/news/update', controller.news.update);// 修改新闻信息
+  router.post('/admin/news/delete', controller.news.delete);// 删除新闻信息
+  // 分站配置接口,用于分站管理员配置本分站信息
+  router.get('/admin/site/config', controller.site.config_fetch);// 分站获取自身配置
+  router.post('/admin/site/config', controller.site.config_set);// 分站配置自身
+  // 系统管理员管理接口
+  router.get('/admin/site/query', controller.site.query);// 查询分站信息
+  router.get('/admin/site/fetch', controller.site.fetch);// 获得分站详情
+  router.post('/admin/site/create', controller.site.create);// 发布分站信息
+  router.post('/admin/site/update', controller.site.update);// 修改分站信息
+  router.post('/admin/site/delete', controller.site.delete);// 删除分站信息
 };
 };

+ 3 - 6
app/service/news.js

@@ -61,8 +61,8 @@ class NewsService extends CrudService {
 
 
     // TODO:保存数据
     // TODO:保存数据
     const data = trimData(payload);
     const data = trimData(payload);
-    const res = await this.model.findByIdAndUpdate(doc.id, { ...data, 'meta.updatedBy': userid }, { new: true }).exec();
-
+    await this.model.findByIdAndUpdate(doc.id, { ...data, 'meta.updatedBy': userid }, { new: true }).exec();
+    const res = this.model.findById(id, '+content').exec();
     return res;
     return res;
   }
   }
 
 
@@ -76,11 +76,8 @@ class NewsService extends CrudService {
 
 
     doc.meta.state = 1;
     doc.meta.state = 1;
     await doc.save();
     await doc.save();
-  }
 
 
-  async fetch({ id }) {
-    const doc = await this.model.findById(id, '+content').exec();
-    return doc;
+    return 'deleted';
   }
   }
 }
 }
 
 

+ 13 - 0
app/service/site.js

@@ -0,0 +1,13 @@
+'use strict';
+
+const _ = require('lodash');
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+
+class SiteService extends CrudService {
+  constructor(ctx) {
+    super(ctx);
+    this.model = this.ctx.model.Site;
+  }
+}
+
+module.exports = SiteService;

+ 1 - 1
config/plugin.js

@@ -4,5 +4,5 @@
 // exports.static = true;
 // exports.static = true;
 
 
 exports.multiTenancy = {
 exports.multiTenancy = {
-  enable: false,
+  enable: true,
 };
 };

+ 37 - 0
test/http/admin/news.http

@@ -0,0 +1,37 @@
+# 查询新闻信息
+GET http://localhost:8202/admin/news/query?column=notice&skip=0&limit=10 HTTP/1.1
+Accept: application/json
+X-Tenant: 99991
+
+###
+# 获取新闻详情
+GET http://localhost:8202/admin/news/fetch?id=5c81ebf0d036ca95f826deeb HTTP/1.1
+Accept: application/json
+X-Tenant: 99991
+
+###
+# 发布新闻信息
+POST http://localhost:8202/admin/news/create?column=notice HTTP/1.1
+Content-Type: application/json
+Accept: application/json
+X-Tenant: 99991
+X-UserID: test
+
+{
+  "title": "“吉人生根工程”专场招聘会",
+  "content": "<p>“吉人生根工程”2019届吉林省普通高校毕业生专场招聘会</p>"
+}
+
+###
+# 修改新闻信息
+POST http://localhost:8202/admin/news/update?column=notice&id=5c81ebf0d036ca95f826deeb HTTP/1.1
+Content-Type: application/json
+Accept: application/json
+X-Tenant: 99991
+X-UserID: test
+
+{
+  "content": "<p>吉人生根工程”2019届吉林省普通高校毕业生专场招聘会<span style=\"color:red;\">福瑞科技</span></p>"
+}
+
+

+ 67 - 0
test/http/admin/site.http

@@ -0,0 +1,67 @@
+# 查询分站信息
+GET http://localhost:8202/admin/site/query?column=notice&skip=0&limit=10 HTTP/1.1
+Accept: application/json
+X-Tenant: master
+
+###
+# 获取分站详情
+GET http://localhost:8202/admin/site/fetch?site=99991 HTTP/1.1
+Accept: application/json
+X-Tenant: master
+
+###
+# 发布分站信息
+POST http://localhost:8202/admin/site/create HTTP/1.1
+Content-Type: application/json
+X-Tenant: master
+X-UserID: test
+Accept: application/json
+
+{
+  "site": "99991",
+  "name": "测试一校",
+  "domain": "99991.smart.chinahuian.cn",
+  "copyright": "吉林省高等学校毕业生就业指导中心 版权所有 | 域名备案信息:吉ICP备xxxxxxxx号",
+  "content": "<p>“吉人生根工程”2019届吉林省普通高校毕业生专场招聘会</p>"
+}
+
+###
+# 修改分站信息
+POST http://localhost:8202/admin/site/update?id=5c81f20f1dc1d09d00626a1d HTTP/1.1
+Content-Type: application/json
+X-Tenant: master
+X-UserID: test
+Accept: application/json
+
+{
+  "content": "<p>吉人生根工程”2019届吉林省普通高校毕业生专场招聘会<span style=\"color:red;\">福瑞科技</span></p>"
+}
+
+###
+# 删除分站信息
+POST http://localhost:8202/admin/site/delete?id=5c81f20f1dc1d09d00626a1d HTTP/1.1
+Content-Type: application/json
+X-Tenant: master
+X-UserID: test
+Accept: application/json
+
+{
+  "content": "<p>吉人生根工程”2019届吉林省普通高校毕业生专场招聘会<span style=\"color:red;\">福瑞科技</span></p>"
+}
+
+###
+# 获取分站配置
+GET http://localhost:8202/admin/site/config HTTP/1.1
+Accept: application/json
+X-Tenant: 99991
+
+###
+# 设置分站配置
+POST http://localhost:8202/admin/site/config HTTP/1.1
+Content-Type: application/json
+Accept: application/json
+X-Tenant: 99991
+
+{
+  "content": "<p>分站联系方式:<span style=\"color:red;\">1234567890</span></p>"
+}

+ 22 - 0
test/http/api/news.http

@@ -0,0 +1,22 @@
+
+###
+# 查询新闻信息
+GET http://localhost:8202/api/news/list?column=notice&skip=0&limit=10 HTTP/1.1
+Accept: application/json
+
+###
+# 获取新闻详情
+GET http://localhost:8202/api/news/fetch?id=5bd233253931128db02d9849 HTTP/1.1
+Accept: application/json
+
+###
+# 获取站点配置
+GET http://localhost:8202/api/site/config HTTP/1.1
+Accept: application/json
+X-Tenant: 99991
+
+###
+# 获取站点详情
+GET http://localhost:8202/api/site/content HTTP/1.1
+Accept: application/json
+X-Tenant: 99991