2021SC@SDUSC
栏目作为网站的条理,很重要
整个项目中有不少都是对栏目的描述,这里主要在看的是sp.js这个文件
文件位置:
基础中的基础,构造函数
使用了category_sp这个数据库中的信息
tactive为其他额外的操作,每次都会被定义,但都没有被用到的变量
module.exports = class extends think.cmswing.admin {
constructor(ctx) {
super(ctx); // 调用父级的 constructor 方法,并把 ctx 传递进去
this.tactive = 'article';
this.db = this.model('cmswing/category_sp');
}
对于权限查看,分为了PC和智能手机两种情况
先来看PC中的情况
cate意为珍馐、美食,,但是这里显然是当作“栏目”的意思而存在的
当获取到的栏目id为空时,就可以直接返回错误:栏目不存在
async indexAction() {
const cate_id = this.get('cate_id') || null;
if (think.isEmpty(cate_id)) {
const error = this.controller('cmswing/error');
return error.noAction('该栏目不存在!');
}
如果验证到,刚刚获取到的栏目id未被初始化,得知该用户目前没有权限查看该栏目
提示此问题
否则,获取此栏目的名称
await this.admin_priv('init', cate_id, '您没有权限查看本栏目!');
const name = await this.model('cmswing/category').get_category(cate_id, 'name') || cate_id;
nav的意思或许是导航,此处应当是上级栏目
随后,获取面包屑信息,通过在category_sp这个数据库中翻找cid为本栏目ID的条目记录
而Hook是一种动态修改函数执行流程或返回结果的方法
然后得以实时展示PC单页内容
const nav = await this.model('cmswing/category').get_parent_category(cate_id);
this.assign('breadcrumb', nav);
const info = await this.db.find({where: {cid: cate_id}});
await this.hook('adminEdit', 'sp_html_pc', info.sp_html_pc, {$hook_key: 'sp_html_pc', $hook_type: '2_1'});
this.meta_title = 'PC单页内容';
this.assign({
'navxs': true,
'name': name,
'info': info
});
return this.display();
关于面包屑的用法:面包屑组件 — Vuetify.js
此处博客有较为详细的说明
至于手机方面
实际上并无本质区别,在此不多赘述
只是一些方法在使用时需要稍作修改
例如栏目ID的验证
if (think.isEmpty(cate_id)) {
const error = this.controller('common/error');
return error.noAction('该栏目不存在!');
以及很没存在意义的,网页标题的内容
this.meta_title = '手机单页内容';
最后,对于更新栏目的操作
与之前在前几篇文章中论述的角色、会员组的方法都几乎一样
其中await this.admin_priv('edit', data.cid)是在验证权限
能够成功修改数据库中的条目,就返回一个“编辑成功”
async updateAction() {
const data = this.post();
await this.admin_priv('edit', data.cid);
const isup = await this.db.thenAdd(data, {cid: data.cid});
if (isup.type == 'exist') {
await this.db.update(data, {where: {cid: data.cid}});
}
return this.success({name: '编辑成功!'});
}