site.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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, copyright, theme, content, remark } = this.ctx.request.body;
  46. console.log('*********content**********\n', content);
  47. const res = await this.service.update({ site }, { name, domain, banner, copyright, theme, content, remark }, { projection: '+content' });
  48. this.ctx.ok({ data: res });
  49. }
  50. }
  51. module.exports = CrudController(SiteController, meta);