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

2021SC@SDUSC

本周我们来看cmswing中的route文件与extadminbase文件的前半部分:

A.route

 async indexAction() {
    const cate = await this.category(this.get('category').split('-')[0].slice(1));
    if (think.isEmpty(cate)) {
      return cate;
    }
    let type = cate.allow_publish;
    if (Number(cate.mold) === 2) {
      type = 'sp';
    }

    switch (type) {
      case 0:
        if (Number(cate.mold) === 1) {
          await this.action('cmswing/modindexbase', 'index');
        } else {
          await this.action('home/cover', 'index');
        }
        break;
      case 1:
      case 2:
        if (Number(cate.mold) === 1) {
          await this.action('cmswing/modindexbase', 'list');
        } else {
          await this.action('home/list', 'index');
        }
        break;
      case 'sp':
        await this.action('home/sp', 'index');
        break;
      default:
        this.body = 'haha';
    }
    // this.end(cate.allow_publish)
    // 获取当前栏目的模型
    // let models = await this.model("category",{},'admin').get_category(cate.id, 'model');
  }
};

route文件主要是用来解析路由,判断所在页面是频道页面还是列表页面。通过cate.allow_publis获取变量type,然后通过switch语句,若type值为0,则为频道页面,若为2,则为列表页面。

B.extadminbase

extadminbase文件主要是负责配置cmswing的各种插件:

一、插件分类

1.

  async gettype() {
    const data = await this.model('ext_type').where({ext: this.ext.ext}).order('sort ASC').select();
    return data;
  }

通过一个没有回调函数的await消灭异步回调,将插件的地址、命令等信息传给变量data,从而获得插件的分类。

2.

 async typeAction() {
    const data = await this.model('cmswing/ext_type').where({ext: this.ext.ext}).page(this.get('page')).order('sort ASC').countSelect();
    // console.log(data);
    const html = this.pagination(data);
    this.assign('pagerData', html); // 分页展示使用
    this.assign('list', data.data);
    return this.display('ext/type');
  }

分类管理方法同样需要变量data得到插件的信息,然后将其传到HTML上通过网页返回给用户,如果一页装不下所有的内容,typeAction方法还支持分页展示使用。

3.

  async typedelAction() {
    const ids = this.para('ids');
    // console.log(ids);
    const res = await this.model('ext_type').where({typeid: ['IN', ids]}).delete();
    if (res) {
      return this.success({name: '删除成功!'});
    } else {
      return this.fail('删除失败!');
    }
  }

typedlAction是用来删除分类的方法,通过变量res来判断删除操作是否成功,若删除成功,会返回”删除成功!“的提示,反之,会返回失败的提示。

4.

  async typeaddAction() {
    if (this.isPost) {
      const data = this.post();
      data.ext = this.ext.ext;
      const res = await this.model('ext_type').add(data);
      if (res) {
        return this.success({name: '添加成功!'});
      } else {
        return this.fail('添加失败!');
      }
    } else {
      this.meta_title = '添加类别';
      return this.display('ext/type_add');
    }
  }

typeaddAction是用来添加类别的方法,在添加前会先用if语句判断分类是否添加过,然后用res变量判断添加操作是否成功,若添加成功,会返回”添加成功!“的提示,反之,会返回失败的提示。

5.

  async typeeditAction() {
    if (this.isPost) {
      const data = this.post();
      data.ext = this.ext.ext;
      const res = await this.model('ext_type').where({typeid: data.typeid}).update(data);
      if (res) {
        return this.success({name: '修改成功!'});
      } else {
        return this.fail('修改失败!');
      }
    } else {
      const id = this.get('id');
      const type = await this.model('ext_type').where({typeid: id}).find();
      console.log(type);
      this.assign('type', type);
      // 获取当前插件的分类
      this.meta_title = '修改类别';
      return this.display('ext/type_edit');
    }
  }

此用法用来修改分类的友情连接,若得到需要修改的请求后,会通过变量值传递的方法来修改链接,同时也会有res变量来判断操作是否成功。

二、插件配置

1.

async sortAction(table, id = 'id') {
    table = table || 'ext_' + this.ctx.controller.split('/')[1];
    const param = this.para('sort');
    const sort = JSON.parse(param);
    const data = [];
    for (const v of sort) {
      const map = {};
      map[id] = v.id;
      map.sort = v.sort;
      data.push(map);
    }
    await this.model(table).updateMany(data);
    await update_cache('ext');
    await update_cache('hooks');
    return this.success({ name: '更新排序成功!'});
  }

插件在获取之后会通过sortAction方法重新排序所有插件。其中调用了多个await方法,他是可以镶嵌在async方法中的,同步写法,异步操作,且完全没有回调函数,是消灭异步回调的终极武器。当重新排序完成后会返回“更新排序成功!”

2.

  async settingAction() {
    if (this.isPost) {
      const data = this.post();
      if (think.isEmpty(data.ext)) {
        data.ext = this.ext.ext;
      }
      // console.log(data);
      const res = await this.model('ext').where({ext: this.ext.ext}).update({setting: JSON.stringify(data)});
      if (res) {
        await update_cache('ext');
        await update_cache('hooks');
        process.send('think-cluster-reload-workers'); // 给主进程发送重启的指令
        return this.success({ name: '更新成功!'});
      } else {
        return this.fail('更新失败!');
      }
    } else {
      this.assign('setting', this.setting);
      return this.display('ext/setting');
    }
  }

settingAction方法用来配置插件,在开始配置前会用一个if语句来判断插件是否配置过。然后通过await函数配置插件,同时会对插件列表进行更新,若更新成功,会返回”更新成功!“的提示,反之,会返回失败的提示。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值