CmsWing源码分析(14)用户设置

2021SC@SDUSC

最后一周,分析一下用户设置这一项

涉及的相关文件为setup.js

文件位置:

加载配置

后台获取方面

选择cmswing/setup这个数据库模型

然后搜索是否存在ROOT_PATH这个文件夹,

存在的话,就将数据库中的设置获取出来,然后写入/src/cmswing/config/setup.js

不存在就先创建这个文件夹

async loadsetup() {
    const fs = require('fs');
    const setup = await this.model('cmswing/setup').lists();
    const path1 = think.getPath('common', 'config');
    if (think.isDir(think.ROOT_PATH + '/src')) {
      const data = 'export default' + JSON.stringify(setup);
      fs.writeFileSync(think.ROOT_PATH + '/src/cmswing/config/setup.js', data);
    }
    const data1 = 'exports.__esModule = true;exports.default =' + JSON.stringify(setup);
    fs.writeFileSync(path1 + '/setup.js', data1);
  }

 网页显示方面

向服务器发送请求

罗列这个用户的id、名称、标题等,展示在“网页配置”这个网页中

async indexAction() {
    const id = this.get('id') || 1;
    const type = this.config('setup.CONFIG_GROUP_LIST');
    const list = await this.db.where({'status': 1, 'group': id}).field('id,name,title,extra,value,remark,type').order('sort').select();
    if (list) {
      this.assign('list', list);
    }
    this.assign({
      'meta_title': type[id] + '设置',
      'id': id
    });
    this.meta_title = '网站配置';
    return this.display();
  }

配置管理

获取该用户的start、length、draw和key

区分是否是管理员

是的话不让修改....

 async groupdataAction() {
    if (this.isGet) {
      const map = {};
      map.status = 1;
      const gets = this.get();
      const start = parseInt(gets.start);
      const length = parseInt(gets.length);
      const draw = gets.draw;
      const key = gets['search[value]'];
      map['name|title'] = ['like', '%' + key + '%'];
      if (gets.group) {
        map.group = gets.group || 0;
      }

如果缓存 userList 不存在,则查询数据库,并将值设置到缓存中

 const lists = await this.db.limit(start, length).where(map).order('sort ASC').countSelect();
      lists.data.forEach(v => {
        if (v.group) {
          v.group = (this.config('setup.CONFIG_GROUP_LIST'))[v.group];
        } else {
          v.group = '未分组';
        }
        v.type = (this.config('setup.CONFIG_TYPE_LIST'))[v.type];
      });

      const data = {
        'draw': draw,
        'recordsTotal': lists.count,
        'recordsFiltered': lists.count,
        'data': lists.data
      };
      return this.json(data);
    }
  }

编辑设置 

同样,要先收集用户的状态参数,并且,为了之后能够记录在数据库中这次编辑

需要获取当前的时间日期

然后更新数据库中的data栏

成功了之后,给主进程发送重启的指令

不成功

便成零

async editAction() {
    if (this.isPost) {
      const data = this.post();
      data.status = 1;
      data.create_time = new Date().valueOf();
      const upres = await this.db.update(data);
      if (upres) {
        await this.cache('setup', null);
        process.send('think-cluster-reload-workers'); 
        return this.json(1);
      } else {
        return this.json(0);
      }
    } else {
      const map = {};
      map.id = this.get('id');
      const info = await this.db.where(map).find();
      this.assign('info', info);
      this.active = 'admin/setup/group';
      this.meta_title = '编辑新增';
      return this.display();
    }
  }

同时网页显示的是“编辑新增”与数据库中map项

编辑成功了以后还需要保存此设置

async saveAction() {
    const post = this.post();
    for (const v in post) {
      this.db.where({name: v}).update({value: post[v]});
    }
    think.cache('setup', null);
    process.send('think-cluster-reload-workers'); 
    this.json(1);
  }

添加、删除配置

两个功能和编辑的做法都几乎一样

没什么需要特别说的....

这是添加配置

async addAction() {
    if (this.isPost) {
      const data = this.post();
      data.status = 1;
      data.update_time = new Date().valueOf();
      const addres = await this.db.add(data);
      if (addres) {
        await this.cache('setup', null);
        process.send('think-cluster-reload-workers'); 
        return this.json(1);
      } else {
        return this.json(0);
      }
    } else {
      this.active = 'admin/setup/group';
      this.meta_title = '新增配置';
      return this.display();
    }
  }

这是删除配置

async delAction() {
    const id = this.get('id');
    const res = await this.db.where({id: id}).delete();
    if (res) {
      await this.cache('setup', null);
      process.send('think-cluster-reload-workers'); 
      return this.json(1);
    } else {
      return this.json(0);
    }
  }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值