山东大学软件工程应用与实践-cmswing-第十周

2021SC@SDUSC

本周我们来分析center文件夹下的seting.js,主要负责管理用户的相关设置:

 首先依旧是__before()方法,与之前address.js中的一致,本周便不再赘述。之后是seting.js的主体部分:

1.

async indexAction() {
    this.tactive = 'user';
    await this.weblogin();
    const userInfo = await this.model('member').find(this.user.uid);
    console.log(userInfo);
    // console.log(userInfo);
    this.assign('userInfo', userInfo);
    let province, city, county;
    if (this.isMobile) {
      province = await this.model('area').where({id: userInfo.province}).getField('name', true);
      city = await this.model('area').where({id: userInfo.city}).getField('name', true);
      county = await this.model('area').where({id: userInfo.county}).getField('name', true);
    } else {
      province = await this.model('area').where({parent_id: 0}).select();
      city = await this.model('area').where({parent_id: userInfo.province}).select();
      county = await this.model('area').where({parent_id: userInfo.city}).select();
    }

    this.assign('province', province);
    this.assign('city', city);
    this.assign('county', county);
    this.meta_title = '用户设置';
    if (this.isMobile) {
      this.active = 'user/index';
      return this.display(this.mtpl());
    } else {
      return this.display();
    }
  }

相较于address.js中的indexAction()方法,seting.js的在判断是否登录与判断用户所使用的浏览客户端两个if语句的基础之上,又增加了用变量userInfo获取用户信息与用if语句获取用户所在省份的功能。

2.

  async updateinfoAction() {
    await this.weblogin();
    const data = this.post();
    // think.log(data);
    const member = {
      email: data.email,
      mobile: data.mobile,
      real_name: data.real_name,
      sex: data.sex,
      birthday: new Date(data.birthday).getTime(),
      province: data.province,
      city: data.city,
      county: data.county,
      addr: data.addr
    };

    if (this.isMobile) {
      if (!think.isEmpty(data.city_picke)) {
        const city_picke = data.city_picke.split(' ');
        member.province = await this.model('area').where({
          name: ['like', `%${city_picke[0]}%`],
          parent_id: 0
        }).getField('id', true);
        member.city = await this.model('area').where({
          name: ['like', `%${city_picke[1]}%`],
          parent_id: member.province
        }).getField('id', true);
        member.county = await this.model('area').where({
          name: ['like', `%${city_picke[2]}%`],
          parent_id: member.city
        }).getField('id', true);
      }
    }

    const update = await this.model('member').where({id: this.user.uid}).update(member);
    // think.log(customer);
    if (update) {
      return this.success({name: '更新用户资料成功!'});
    } else {
      return this.fail('更新失败!');
    }
  }

updateinfoAction()用来更新用户信息,先用this.weblogin()方法判断用户是否登录,之后将用户相关的信息传给变量member。之后先判断用户使用的客户端,不同的客户端返回相应的改变用户信息的方法。然后将更新的结果传给布尔变量update,通过if语句返回给用户是否修改成功:若upadte值为true,返回“更新用户资料成功!”,否则返回“更新失败”。

3.

  async updatepasswordAction() {
    await this.weblogin();
    const data = this.post();
    if (think.isEmpty(data.password)) {
      return this.fail('请填写新密码!');
    }
    const password = await this.model('member').where({id: this.user.uid}).getField('password', true);
    if (password === encryptPassword(data.oldpassword)) {
      await this.model('member').where({id: this.user.uid}).update({password: encryptPassword(data.password)});
      return this.success({name: '密码修改成功,请用新密码重新登陆!'});
    } else {
      return this.fail('旧密码不正确,请重新输入。');
    }
  }

updatepasswordAction()方法用来修改用户密码,先通过this.weblogin()方法判断用户是否登录,然后用户填写新的密码,若if语句检测到用户没有输入新的密码,则返回“请填写新密码”。修改密码需要用户先输入旧密码,将用户输入的旧密码传给变量password,通过if语句判断用户输入的是否与旧密码一致,成功则返回“密码修改成功,请用新密码重新登陆!”,否则,返回“旧密码不正确,请重新输入。”
4.

  async updateavatarAction() {
    await this.weblogin();
    const file = think.extend({}, this.file('file'));
    // console.log(file);
    // think.log(avatar_data);
    var filepath = file.path;
    var uploadPath = think.resource + '/upload/avatar/' + this.user.uid;
    think.mkdir(uploadPath);
    let res;
    if (this.isMobile) {
      const jimp2 = () => {
        console.log(111);
        const deferred = think.defer();
        const self = this;
        Jimp.read(filepath, function(err, lenna) {
          if (err) throw err;
          lenna.resize(200, 200) // resize
            .quality(60) // set JPEG quality
            .write(uploadPath + '/avatar.png', function(e, r) {
              deferred.resolve('/upload/avatar/' + self.user.uid + '/avatar.png');
            }); // save
        });
        return deferred.promise;
      };
      res = await jimp2();
    } else {
      const post = this.post();
      const avatar_data = JSON.parse(post.avatar_data);
      const jimp = () => {
        const deferred = think.defer();
        const self = this;
        Jimp.read(filepath, function(err, lenna) {
          // console.log(lenna)

          if (err) throw err;
          lenna.crop(avatar_data.x, avatar_data.y, avatar_data.width, avatar_data.height) // resize
            .quality(60)
            .write(uploadPath + '/avatar.png', function(e, r) {
              deferred.resolve('/upload/avatar/' + self.user.uid + '/avatar.png');
            }); // save
        });
        return deferred.promise;
      };
      res = await jimp();
    }

    // think.log(res);
    const data = {
      'result': res,
      'errno': 0,
      'message': '头像上传成功!'
    };
    return this.json(data);
  }
};

updateavatarAction()方法用来上传用户头像,首先依旧是通过 this.weblogin()方法判断用户是否登录,中华通过file.path方法上传图像文件,文件上传后,需要将文件移动到项目其他地方,否则会在请求结束时删除掉该文件。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值