CmsWing源码分析(11)搜索功能

2021SC@SDUSC

今天分析的是搜索功能

文件位于

网页初始化 

使用一个模板库search_model中的 sort ASC渲染此网页

将网页标题定为“全站搜索”

然后先检查全文搜索的配置

在mysql数据库中搜索variables并赋值给此处的variables变量

再在variables中搜寻名字符合'Variable_name', 'ft_min_word_len'的数值

async indexAction() {
    const list = await this.model('search_model').order('sort ASC').select();
    this.assign('list', list);
    this.meta_title = '全站搜索';
    const variables = await this.model('mysql').query(`show variables`);
    const ft_min_word_len = think._.find(variables, ['Variable_name', 'ft_min_word_len']).Value;
    this.assign('ft_min_word_len', ft_min_word_len);
    return this.display();
  }

添加搜索分类

首先接收需要添加的分类的名称,将其赋值给data变量

安全起见,在数据库cmswing/model中获取extend栏属性

将extend属性修改好的data,添加到search_model数据库中

并对是否添加成功做出反应

async addAction() {
    if (this.isPost) {
      const data = this.post();
      const extend = await this.model('cmswing/model').get_model(data.mod, 'extend');
      data.extend = extend;
      const add = await this.model('search_model').add(data);
      if (add) {
        return this.success({name: '添加成功!'});
      } else {
        return this.fail('添加失败!');
      }
    } else {
      const modlist = await this.model('model').where({status: 1, id: ['>', 1]}).select();
      this.assign('modlist', modlist);
      this.meta_title = '添加搜索分类';
      return this.display();
    }
  }

如果没有接收到需要添加的分类的名称

返回一个添加搜索分类的网页

编辑、删除与排序

编辑是与添加搜索分类如出一辙的操作,仅仅将第五行的add改为update就可以完成

async editAction() {
    if (this.isPost) {
      const data = this.post();
      const extend = await this.model('cmswing/model').get_model(data.mod, 'extend');
      data.extend = extend;
      const up = await this.model('search_model').update(data);
      if (up) {
        return this.success({name: '编辑成功!'});
      } else {
        return this.fail('编辑失败!');
      }
    } else {
      const info = await this.model('search_model').find(this.get('id'));
      this.assign('info', info);
      const modlist = await this.model('model').where({status: 1, id: ['>', 1]}).select();
      this.assign('modlist', modlist);
      this.meta_title = '编辑搜索分类';
      return this.display();
    }
  }
  

删除操作甚至比前面两种都要简单

仅需获取需要删除的分类的ID

不用像之前那样得到一堆列表

async delAction() {
    const id = this.get('id');
    const del = await this.model('search_model').where({id: id}).delete();
    if (del) {
      return this.success({name: '删除成功!'});
    } else {
      return this.fail('删除失败!');
    }
  }

更简略的排序....

就只是使用了父类的构造方法...

async sortAction() {
    await super.sortAction('search_model');
  }

重建索引

首先需要获得所有的分类列表

并检查配置

async createindexAction() {
    const paths = think.resource + '/backup/';
    const lock = paths + 'createindex.lock';
    const variables = await this.model('mysql').query(`show variables`);
    const ft_min_word_len = think._.find(variables, ['Variable_name', 'ft_min_word_len']).Value;

然后在获取到的目标不为空的情况下,试图创建一个新的锁文件

创建的时候注意先检查是否有正在执行的任务,没有才可以创建

然后将search_model中的表赋值给tables数组

并对tables中的每一个对象,将其extend的值修改

修改需要分情况,本身为零时,修改为mod栏的名称

否则,修改为‘doucument’

再将这个tables重新保存为“createindex_index”表格

if (this.isAjax('post') && !think.isEmpty(this.post())) {
      think.mkdir(paths);
      if (think.isFile(lock)) {
        return this.fail(20, '检测到有一个重建任务正在执行,请稍后再试!');
      } else {
        fs.writeFileSync(lock, new Date());
      }
      const tables = await this.model('search_model').select();
      for (const v of tables) {
        if (v.extend == 0) {
          v.table = await this.model('cmswing/model').get_model(v.mod, 'name');
        } else {
          v.table = 'document';
        }
      }
      await this.session('createindex_tables', tables);
     

清空缓存列表,开始索引更新

      await this.model('search').where('1=1').delete();
      const page = {'id': 0, 'page': 1, 'pagesize': this.post('pagesize')};
      return this.json({
        'msg': {progress: 0, info: '开始索引更新'},
        'page': page,
        'status': 1
      });

否则,返回一个“重建索引”的网页

同样需要获取原来的索引列表

else {
      const variables = await this.model('mysql').query(`show variables`);
      const ft_min_word_len = think._.find(variables, ['Variable_name', 'ft_min_word_len']).Value;
      this.assign('ft_min_word_len', ft_min_word_len);
      this.meta_title = '重建索引';
      this.active = 'admin/search/index';
      return this.display();
    }

删除锁文件

和创建时一样需要 检查是否有正在执行的任务

有就可以执行解锁操作

没有就可以直接回应不用这个操作

unlockAction() {
    const paths = think.resource + '/backup/';
    const lock = paths + 'createindex.lock';
    if (think.isFile(lock)) {
      fs.unlinkSync(lock);
      return this.success({name: '解锁成功!'});
    } else {
      // 创建锁文件
      return this.success({name: '无需解锁!'});
    }
  }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值