2021SC@SDUSC
本周我们来解析cmswing文件夹下最后一个核心文件:modindexbase.js。
1.
async __before() {
await super.__before();
const getCategory = this.get('category') ? this.get('category').split('-')[0].slice(1) : false;
if (this.get('category') || this.get('cid')) {
this.m_cate = await this.category(getCategory || this.get('cid'));
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('模型不存在或被禁用!');
}
this.meta_title = this.m_cate.meta_title ? this.m_cate.meta_title : this.m_cate.title;
this.keywords = this.m_cate.keywords ? this.m_cate.keywords : '';
this.description = this.m_cate.description ? this.m_cate.description : '';
}
}
modindexbase.js中的__before()与modadminbase.js的__before()一样,规定了模型公共参数,继承了父类__before(admin.js中),执行验证登录、用户信息等操作的代码,不同的是,modindexbase.js中的__before()在这之后是用了一个嵌套的if语句,通过this.category(getCategory || this.get('cid'))来获取当前模型栏目id,通过this.model('cmswing/model').get_model(this.m_cate.model)来获取当前模型信息,然后通过内部的if语句来判断this.mod方法的返回值是否为空,以此来判断当前模型是否存在或被禁用。