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

2021SC@SDUSC

本周来解析cmswing文件夹下的error.js和modadminbase.js文件:

一、error

module.exports = class extends think.Controller {
  okAction(message = '成功信息!', status = 0) {
    if (this.isJsonp()) {
      return this.jsonp({
        [this.config('errnoField')]: status,
        [this.config('errmsgField ')]: message
      });
    } else if (this.isAjax()) {
      return this.fail(status, message);
    }
    this.assign({
      status: status,
      message: message
    });

    return this.display('cmswing/error_ok');
  }
  noAction(message = '错误信息!', status = 100) {
    if (this.isJsonp()) {
      return this.jsonp({
        [this.config('errnoField')]: status,
        [this.config('errmsgField ')]: message
      });
    } else if (this.isAjax()) {
      return this.fail(status, message);
    }
    this.assign({
      status: status,
      message: message
    });

    return this.display('cmswing/error_no');
  }
  loginAction(message = '错误信息!', status = 700) {
    if (this.isJsonp()) {
      return this.jsonp({
        [this.config('errnoField')]: status,
        [this.config('errmsgField ')]: message
      });
    } else if (this.isAjax()) {
      return this.fail(status, message);
    }
    this.assign({
      status: status,
      message: message
    });

    return this.display('cmswing/error_login');
  }
};

error中包含okAction()、noAction()、loginAction()三个方法参数分别为:(message = '成功信息!', status = 0)、(message = '错误信息!', status = 100)、(message = '错误信息!', status = 700),分别代表了方法使用正确、错误及加载错误的返回信息。

error.js中的方法在cmswing的其他文件中有多次使用到,比如modadminbase.js中的__before方法中,用来判断模块是否可用:

if (think.isEmpty(this.mod)) {
        const error = this.controller('cmswing/error');
        return error.noAction('模型不存在或被禁用!');
      }

二、modadminbase.js

1.

  async __before() {
    await super.__before();
    if (this.get('cate_id')) {
      this.m_cate = await this.category(this.get('cate_id'));
      if (think.isEmpty(this.m_cate)) {
        return this.m_cate;
      }
      this.mod = await this.model('cmswing/model').get_model(this.m_cate.model);
      if (think.isEmpty(this.mod)) {
        const error = this.controller('cmswing/error');
        return error.noAction('模型不存在或被禁用!');
      }
    }
  }
__before规定了模型公共参数,它先继承父类__before(admin.js中),先执行验证登录、用户信息等代码,然后在父类的基础上通过this.get('cate_id')与this.model('cmswing/model').get_model(this.m_cate.model)分别获取当前模型栏目id与当前模型信息,通过if语句来判断该模型是否可用。
2.
 async indexAction() {
    try {
      return this.action('mod/' + this.mod.name + '/admin', 'index');
    } catch (err) {
      assert(!think.isError(err), err);
    }
  }

  async category(id, field) {
    id = id || 0;
    field = field || '';
    const error = this.controller('cmswing/error');
    if (think.isEmpty(id)) {
      // this.fail('没有指定数据分类!');
      return error.noAction('没有指定数据分类!');
    }
    const cate = await this.model('cmswing/category').info(id, field);
    // console.log(cate);
    if (cate && cate.status == 1) {
      switch (cate.display) {
        case 0:
          // this.fail('该分类禁止显示')
          return error.noAction('该分类禁止显示!');
          // TODO:更多分类显示状态判断
        default:
          return cate;
      }
    } else {
      // this.fail("分类不存在或者被禁用!");
      return error.noAction('该分类禁止显示!');
    }
  }

 indexAction()与category()两个方法用来制作封面入口:先用indexAction()判断方法是否有错,然后通过category()获取分类信息,通过两个个if语句判断分类是否存在/是否可以显示,若条件不允许,则返回“没有指定数据分类!”/“该分类禁止显示!”的提示信息。

3.

  modDisplay(p = this.ctx.action, m = '') {
    let c = this.ctx.controller.split('/');
    if (this.ctx.controller === 'cmswing/modadminbase') {
      c = `mod/${this.mod.name}/admin`.split('/');
    }
    if (p === 'm' || !think.isEmpty(m)) {
      if (p === 'm') {
        p = this.ctx.action;
        if (this.ctx.controller === 'cmswing/modadminbase') {
          p = 'index';
        }
      }
      const pp = path.join(think.ROOT_PATH, 'src', 'controller', 'mod', c[1], 'view', 'mobile', c[2]);
      return this.display(`${pp}_${p}`);
    } else {
      const pp = path.join(think.ROOT_PATH, 'src', 'controller', 'mod', c[1], 'view', 'pc', c[2]);
      return this.display(`${pp}_${p}`);
    }
  }
modDisplay()方法负责独立模型模版的渲染。其主要部分是个if语句的三层嵌套,最外层if语句用来判断该模型是否非空,中间层if语句用来判断this.ctx.action方法的返回值p是否是“ ”,也就是该模型的action是否能运行,最内层if语句用来判断this.ctx.controller方法的返回值是否符合环境要求。 

4.

  async groups(cid = this.m_cate.id) {
    let groups = await this.model('cmswing/category').get_category(cid, 'groups');
    if (groups) {
      groups = parse_config_attr(groups);
    }
    this.assign('groups', groups);
  }
};

groups()方法用来获取分组,与admin.js中的groups()方法相同,都是用if语句与变量groups来判断数据应分到那个组别中。

5.

  async breadcrumb(cid = this.m_cate.id) {
    const breadcrumb = await this.model('cmswing/category').get_parent_category(cid);
    this.assign('breadcrumb', breadcrumb);
  }
breadcrumb()用来负责网页中面包屑的显示,面包屑的具体含义及用法我们将在下周modindexbase.js文件的解析中说明。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值