CmsWing源码分析(10)用户首页(二)

2021SC@SDUSC

继续来分析user.js这个文件

用户首页分为普通用户和管理员用户两种模式

这次主要分析的是普通用户

使用的数据库依然是member

 

会员充值

POST - 向指定的资源提交要被处理的数据。

将当前用户的id、数据等信息传输给服务器后,获取insertId的属性内容

balance意为余额,此处应当代指充值进去的金额

此笔账单note中的内容为管理员XXX为您充值,充值的金额为XX

可用于后续弹窗提醒

async rechargeAction() {
    if (this.isAjax('POST')) {
      const data = this.post();
      const self = this;
      const insertId = await this.db.transaction(async() => {
        await self.db.where({id: data.id}).increment('amount', data.balance);
        const amount_log = await self.db.where({id: data.id}).getField('amount', true);
        return await self.model('balance_log').db(self.db.db()).add({
          admin_id: self.user.uid,
          user_id: data.id,
          type: 2,
          time: new Date().valueOf(),
          amount: data.balance,
          amount_log: amount_log,
          note: `管理员(${await get_nickname(self.user.uid)})为您充值,充值的金额为:${data.balance} 元`
        });
      });

其中,ajax() 方法通过 HTTP 请求加载远程数据。

具体用法可见:jQuery ajax - ajax() 方法

充值成功失败与否,都会给出一份反馈

if (insertId) {
        return this.success({name: '充值成功!'});
      } else {
        return this.fail('充值失败!');
      }

如果向服务器发送用户数据没成功,就加载一个“会员充值”的网页页面

一般会出现这样的情况是因为你还没有登陆账号呢

} else {
      const id = this.get('ids');
      const name = await get_nickname(id);
      this.assign('name', name);
      this.assign('id', id);
      this.meta_title = '会员充值';
      return this.display();
    }

编辑头像

虽然不知道为什么,但是这里的编辑头像需要输入用户密码....?

首先,与之前的方法一致,向服务器传送当前角色的id来方便后续具体的对角色的操作

delavatar的意思直接就是删除用户头像,当这个属性的数据为1时,意味将要执行头像删除的操作。

此时,问用户要一张图片,获取图片的路径,上传给网站,同时删除原头像文件

ync edituserAction() {
    if (this.isPost) {
      const data = this.post();
      // 删除头像
      if (data.delavatar == 1) {
        const uploadPath = think.resource + '/upload/avatar/' + data.id;
        const path = think.isFile(uploadPath + '/avatar.png');
        if (path) {
          think.rmdir(uploadPath, false);
        }
      }

rmdir的用法解释:

(method) ThinkHelper.Think.rmdir(p: String, reserve?: Boolean): Promise<any>
remove dir aync

验证密码、验证会员等级

if (think.isEmpty(data.password) && think.isEmpty(data.repassword)) {
        delete data.password;
      } else {
        if (data.password != data.repassword) {
          return this.fail('两次填入的密码不一致');
        }
        data.password = encryptPassword(data.password);
      }
      if (data.vip == 1) {
        if (think.isEmpty(data.overduedate)) {
          data.overduedate = new Date();
        }
        data.overduedate = new Date(data.overduedate).getTime();
      } else {
        data.overduedate = 0;
        data.vip = 0;
      }

为这个新头像添加一个角色,然后做出反馈

if (data.is_admin == 1) {
        const addrole = await this.model('auth_user_role').where({user_id: data.id}).thenAdd({user_id: data.id, role_id: data.role_id});
        if (addrole.type == 'exist') {
          await this.model('auth_user_role').update({id: addrole.id, role_id: data.role_id});
        }
      }
      const res = await this.db.update(data);

      if (res) {
        return this.success({name: '编辑成功!'});
      } else {
        return this.fail('编辑失败!');
      }

而传输用户数据有两种可能

一是他是个超级管理员

二是他还没注册

超级管理员不能被修改信息

同属管理员组的也不行

直接返回一个编辑用户页面

else {
      const id = this.get('id');
      const user = await this.model('member').find(id);
      if (!this.is_admin) {
        if (in_array(id, this.config('user_administrator'))) {
          const error = this.controller('cmswing/error');
          return error.noAction('您无权操作!');
        }
      }
      this.assign('user', user);
      if (user.is_admin == 1) {
        const roleid = await this.model('auth_user_role').where({user_id: user.id}).getField('role_id', true);
        this.assign('roleid', roleid);
      }
      // 会员组
      const usergroup = await this.model('member_group').select();
      this.assign('usergroup', usergroup);
      // 获取管理组
      const role = await this.model('auth_role').where({status: 1}).select();
      this.assign('role', role);
      this.meta_title = '编辑用户';
      return this.display();
    }

获取头像

对图片的做法反而比编辑头像要细致

uid是比名字、id更加独一无二的,系统给的唯一标识符

获取到了数据库中的图片,就把图片拔下来

否则,使用本地存储着的默认头像

pic就是需要获取的头像

async avatarAction() {
    const uid = this.get('uid') || this.user.uid;
    var uploadPath = think.resource + '/upload/avatar/' + uid;
    const path = think.isFile(uploadPath + '/' + '/avatar.png');
    let pic;
    if (path) {
      pic = fs.readFileSync(uploadPath + '/' + '/avatar.png');
    } else {
      pic = fs.readFileSync(think.resource + '/upload/avatar/avatar.jpg');
    }
    this.header('Content-Type', 'image/png');
    return this.body = pic;
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值