山东大学软件工程应用与实践-cmswing-第十三周

本文详细分析了CMS系统中publish.js文件的功能,主要涉及在线投稿和文章编辑。文件中的indexAction()处理投稿界面展示,包括权限判断、模型与分组信息获取、面包屑导航等。addAction()则关注新增投稿的处理,涉及用户权限、模型与分组ID验证、表单字段及编辑器钩子的设置。
摘要由CSDN通过智能技术生成

2021SC@SDUSC

最后一周我们来分析center文件夹下的publish.js,它被用来处理cmswing上的文章、数据等内容的投稿与文档的编辑:

1.

与之前的几个类的indexAction()重用返回用户交互界面不同,publish.js的主要操作都在indexAction()中完成,它被用来处理在线投稿,返回用户投稿界面。

    let cate_id = this.get('cate_id') || null;
    // console.log(cate_id);
    const priv = await this.priv(cate_id);
    console.log(priv);
    if (priv) {
      const error = this.controller('cmswing/error');
      return error.noAction('网站禁止投稿!');
    }

首先是变量priv,它通过方法this.priv(cate_id)读取用户的id(cate_id,由方法this.get('cate_id')取得)返回一个布尔变量用来判断用户是否有投稿的权限。

      // console.log(sort);
      this.assign('sort', sort);
      const pid = this.get('pid') || 0;
      if (pid == 0) {
        models = await this.model('cmswing/category').get_category(cate_id, 'model');
        groups = await this.model('cmswing/category').get_category(cate_id, 'groups');
        if (groups) {
          groups = parse_config_attr(groups);
        }
      } else { 
        models = await this.model('cmswing/category').get_category(cate_id, 'model_sub');
      }

变量pid通过方法this.get('pid')获取列表绑定的模型,若pid为0,则代表这是根列表,然后通过this.model('cmswing/category').get_category(cate_id, 'groups')返回一个布尔变量获取分组定义,通过if语句判断分组是否在已有分组中。若pid不为0,则代表这是子文档列表,只用获取models即可。

      const nav = await this.model('cmswing/category').get_parent_category(cate_id || 0);
      this.assign('breadcrumb', nav);
      if (think.isEmpty(model_id) && !think.isNumberString(models)) {
        model = await this.model('model').where({name: 'document'}).find();
        // console.log(model);
      } else {
        model_id = model_id || models;
        model = await this.model('model').where({id: ['IN', [model_id]]}).find();

        if (think.isEmpty(model['list_grid'])) {
          const data = await this.model('model').field('list_grid').where({name: 'document'}).find();
          model.list_grid = data.list_grid;
          // console.log(33);
        }
      }
      this.assign('model', models.split(','));
      _model = models.split(',');

变量nav通过this.model('cmswing/category').get_parent_category(cate_id || 0)获取面包屑信息(面包屑的相关信息在第七周有介绍)。然后通过assign()这一种操作列的函数,增加一个新的列。之后通过if语句判断当前用户所在的位置的模型是否为空,若不为空,则通过this.model('model').where({name: 'document'}).find()绑定多个模型,取基础模型的列表定义;否则,用方法this.model('model').where({id: ['IN', [model_id]]}).find()获取模型信息,然后通过一个if语句判断模型列表(model['list_grid'])是否为空,若为空,则需要通过变量data进行对list_grid的添加。

   
    let fields = [];
    const ngrids = [];
    // console.log(model);
    const grids = model.list_grid.split('\r\n');
    for (let value of grids) {
      const val = value.split(':');
      const field = val[0].split(',');
      value = {field: field, title: val[1]};

之后开始解析列表的规则。先设置let数组fields与const数组ngrids。投稿先用ngrids保存,然后将正则表达式传递给变量grids,对其进行for循环,将投稿以'字段:标题:链接‘的形式保存。通过数组fields实现对多个字段显示的支持。


    fields.push('category_id');
    fields.push('model_id');
    fields.push('pid');
    fields = unique(fields);
 

之后将模型列表的相关信息保存在fields中,category_id、model_id、pid是文档模型列表始终要获取的数据字段,通过fields.push()方法可以他们以便用于其他用途。之后的unique(fields)可以去除fields中的重复字段。

    this.meta_title = '在线投稿';
    this.assign('modellist', modellist);
    this.assign('cate_id', cate_id);
    this.assign('model_id', model_id);
    this.assign('group_id', group_id);
    this.assign('sortid', sortid);
    this.assign('position', position);
    this.assign('groups', groups);
    this.assign('list', list);
    this.assign('list_grids', ngrids);
    this.assign('model_list', model);
    return this.display();

最后投稿将返回以“在线投稿”为标题,可以获取modellist、cate_id、model_id 、group_id、sortid、position、groups、list、ngrids、model的一个界面。

2.

addAction()方法用来获取新增的投稿。

const priv = await this.priv(cate_id);
    if (priv) {
      const error = this.controller('cmswing/error');
      return error.noAction('您所在的会员组,禁止在本栏目投稿!');
    }
    const model_id = this.get('model_id') || 0;
    const group_id = this.get('group_id') || '';
    let sortid = this.get('sortid') || 0;
    think.isEmpty(cate_id) && this.fail('参数不能为空');
    think.isEmpty(model_id) && this.fail('该分类未绑定模型');

首先依旧是通过priv变量获取用户的权限,若用户不能投稿,则返回'您所在的会员组,禁止在本栏目投稿!';反之,进一步获取模型id与分组id。

let groups = await this.model('cmswing/category').get_category(cate_id, 'groups');

let sort = await this.model('cmswing/category').get_category(cate_id, 'documentsorts');

变量groups与sort分别用来获取分组定义与分类信息。

 const allow_publish = await this.model('cmswing/category').check_category(cate_id);
    // console.log(allow_publish);
    if (!allow_publish) {
      const error = this.controller('cmswing/error');
      return error.noAction('本栏目不允许发布内容!');
    }

然后通过将分类信息传递给方法this.model('cmswing/category').check_category(cate_id)返回一个布尔值给const变量allow_publish,通过if语句检查该分类是否允许发布,若返回的布尔值为false,则给用户返回'本栏目不允许发布内容!'。

const fields = await this.model('cmswing/attribute').get_model_attribute(model.id, true);
    // think.log(fields);
const type_list = await this.model('cmswing/category').get_type_bycate(cate_id);
    // console.log(type_list);
const nav = await this.model('cmswing/category').get_parent_category(cate_id);

fields、type_list、nav分别用来获取表单字段排序、当前分类文档的类型与面包屑信息。

    this.assign('groups', groups);
    this.assign('breadcrumb', nav);
    this.assign('info', info);
    this.assign('fields', fields);
    this.assign('type_list', type_list);
    this.assign('model', model);
    this.meta_title = '新增' + model.title;
    this.active = 'admin/article/index';
    for (const key in parse_config_attr(model.field_group)) {
      for (const f of fields[key]) {
        if (f.type === 'editor') {
          if (model.editor === '0') {
            await this.hook('homeEdit', f.name, f.value, {$hook_key: f.name});
          } else {
            await this.hook('homeEdit', f.name, f.value, {$hook_key: f.name, $hook_type: model.editor});
          }
        } else if (f.type === 'picture') {
          await this.hook('homeUpPic', f.name, 0, {$hook_key: f.name});
        } else if (f.type === 'pics') {
          await this.hook('homeUpPics', f.name, '', {$hook_key: f.name});
        } else if (f.type === 'file') {
          await this.hook('homeUpFile', f.name, 0, {$hook_key: f.name});
        } else if (f.type === 'atlas') {
          await this.hook('homeAtlas', f.name, '', {$hook_key: f.name});
        };
      };
    };
    return this.display();

最后在数据库中增加groups、nav、info、fields、type_list、model这几个列,通过if语句添加编辑器的钩子,然后返回给用户新增投稿的界面。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值