2021SC@SDUSC
之前的文章都在围绕着全局函数讨论,其中仅有一部分函数浅略地涉及了一些用户行为相关。
而这一章,我将全面围绕用户日志,分析src/controller/admin/action.js这一文件
具体位置
首先 action这个类是继承自cmswing.admin的模板类
目录
构造函数
在初始化构造中, 它调用父级的 constructor 方法,并把 ctx 传递进去
而tactive则代表了其他用户行为
module.exports = class extends think.cmswing.admin {
constructor(ctx) {
super(ctx);
this.tactive = 'user';
}
虽然这边提及了其他用户行为,但tactive这个属性在本类中没有再次被使用,推测tactive只能在其他需要使用的时候再使用全局函数obj_values或类似功能的函数提取出来使用
用户日志
根据注释,本函数的主要作用是自动渲染模板文件index_index.html
先是临时创建一个变量 action,定义为以“action”为模型参数,状态大于-1为条件,“ID DESC”为编号,并有一个获取到的(没怎么找到这个函数....)页号
再根据action中的数据,初始化一个页面(pagination 明显为页面化的意思,但这个函数具体在哪里被定义的,我没有找到)
修改页面数据 pageData的数据为html
修改list的数据为action.data
页面标题为“用户行为”
async indexAction() {
// auto render template file index_index.html
const action = await this.model('action').where({'status': ['>', -1]}).order('id DESC').page(this.get('page')).countSelect();
const html = this.pagination(action);
this.assign('pagerData', html);
this.assign('list', action.data);
this.meta_title = '用户行为';
return this.display();
}
display为javascript DOM 中的一种属性,用于设置元素如何显示
其中,await表达式会暂停整个async函数的执行进程并出让其控制权,只有当其等待的基于promise的异步操作被兑现或被拒绝之后才会恢复进程。
日志列表
除开变量为action_log而非action,在获取页号、初始化页面方面,与用户行为并无本质区别
list为需要获取的数据列表
之后,不同的一步就是,对列表中每一个元素进行捕获
使用到的get_action和get_nickname都为我之前在全局函数中介绍过的
async logAction() {
const map = {};
map.status = ['>', -1];
const list = await this.model('action_log').where({'status': ['>', -1]}).order('id DESC').page(this.get('page')).countSelect();
const html = this.pagination(list);
this.assign('pagerData', html);
for (const itme of list.data) {
itme.action_id = await this.model('cmswing/action').get_action(itme.action_id, 'title');
itme.user_id = await this.model('cmswing/member').get_nickname(itme.user_id);
}
this.assign('list', list.data);
this.meta_title = '行为日志';
return this.display();
}
新增日志
增加一个active
修改了data属性为null
等待具体行为
addAction() {
this.meta_title = '新增行为';
this.active = 'admin/action/index';
this.assign('data', null);
return this.display();
}
删除日志
获取本网页中自带的属性“ids”的具体值,并赋值给一个新的名为ids的变量
如果未能获取到,或发生其他错误,则提示“参数错误”
当获取到的ids为数组时,分别录入数组map中键值为ids的地方
否则为单个数据时,直接赋值给map中键值为ids的项
删除模型为“action_log ”、条件为符合获取到的ids的行为数据
并做相应弹窗提示
async removeAction() {
const ids = this.param('ids');
think.isEmpty(ids) && this.fail('参数错误');
const map = {};
if (think.isArray(ids)) {
map.id = ['IN', ids];
} else if (think.isNumberString(ids)) {
map.id = ids;
}
const res = await this.model('action_log').where(map).delete();
if (res) {
this.success({name: '删除成功!', url: '/admin/action/log'});
} else {
this.fail('删除失败!');
}
}
清空日志
与删除几乎差不多的操作,只不过做的很绝
async clearAction() {
const res = await this.model('action_log').where('1=1').delete();
if (res) {
this.success({name: '日志清空成功!', url: '/admin/action/log'});
} else {
this.fail('日志清空失败!');
}
}
编辑日志
在获取到网页ID的基础上,
直接修改data栏为“data”参数
由于是纯手动的
其实不怎么用得到.....
async editAction() {
const id = this.get('id');
think.isEmpty(id) && this.fail('参数不能为空!');
const data = await this.model('action').find(id);
this.active = 'admin/action/index';
this.meta_title = '编辑行为';
this.assign('data', data);
return this.display('admin/action_add');
}
更新日志
全自动日志更新装置
若此前没有日志,则获取到的用户ID为空,根据这一特性,决定是否对数据状态参数做修改
是全新的,则修改,获取当前日期时间,并在无错误时弹窗“新增成功”
不是全新的用户,则直接更新,同样地获取当前日期时间,并在无错误时弹窗“更新成功”
async updateAction() {
const data = this.post();
if (think.isEmpty(data.id)) {
data.status = 1;
data.update_time = Date.now();
const res = await this.model('action').add(data);
if (res) {
this.success({name: '新增成功!', url: '/admin/action/index'});
} else {
this.fail('添加失败!');
}
} else {
data.update_time = Date.now();
const res = await this.model('action').update(data);
if (res) {
this.success({name: '更新成功!', url: '/admin/action/index'});
} else {
this.fail('更新失败!');
}
}
}
至于用户日志中需要记载的具体内容,需要等以后再挖掘