site.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. const _ = require('lodash');
  3. const meta = require('./.site.js');
  4. const Controller = require('egg').Controller;
  5. const { CrudController } = require('naf-framework-mongoose/lib/controller');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. class SiteController extends Controller {
  8. constructor(ctx) {
  9. super(ctx);
  10. this.service = this.ctx.service.site;
  11. }
  12. // 获得当前分站信息
  13. async content() {
  14. // TODO: 检查用户信息
  15. const site = this.ctx.tenant;
  16. if (!_.isString(site)) throw new BusinessError(ErrorCode.BADPARAM, '租户信息不存在');
  17. const res = await this.service.fetch({ site }, {
  18. projection: { content: 1 }
  19. });
  20. this.ctx.ok({ data: res && res.content });
  21. }
  22. // 获得当前分站信息
  23. async config_fetch() {
  24. // TODO: 检查用户信息
  25. const site = this.ctx.tenant;
  26. if (!_.isString(site)) throw new BusinessError(ErrorCode.BADPARAM, '租户信息不存在');
  27. const res = await this.service.fetch({ site }, { projection: '+content' });
  28. this.ctx.ok({ data: res });
  29. }
  30. // 获得当前分站信息
  31. async config_get() {
  32. // TODO: 检查用户信息
  33. const site = this.ctx.tenant;
  34. if (!_.isString(site)) throw new BusinessError(ErrorCode.BADPARAM, '租户信息不存在');
  35. const res = await this.service.fetch({ site });
  36. this.ctx.ok({ data: res });
  37. }
  38. // 分站配置自身信息
  39. async config_set() {
  40. // TODO: 检查用户信息
  41. const userid = this.ctx.userid;
  42. if (!_.isString(userid)) throw new BusinessError(ErrorCode.NOT_LOGIN);
  43. const site = this.ctx.tenant;
  44. if (!_.isString(site)) throw new BusinessError(ErrorCode.BADPARAM, '租户信息不存在');
  45. const { name, domain, banner, logo,copyright, theme, content, remark } = this.ctx.request.body;
  46. const res = await this.service.update({ site }, { name, domain, banner, logo, copyright, theme, content, remark }, { projection: '+content' });
  47. this.ctx.ok({ data: res });
  48. }
  49. }
  50. module.exports = CrudController(SiteController, meta);