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

2021SC@SDUSC

本周我们来分析center文件夹下的cart.js的前半部分,主部分主要负责管理用户的购物车:

1.

indexAction() {
    // auto render template file index_index.html
    this.meta_title = '购物车';
    this.keywords = this.config('setup.WEB_SITE_KEYWORD') ? this.config('setup.WEB_SITE_KEYWORD') : '';
    this.description = this.config('setup.WEB_SITE_DESCRIPTION') ? this.config('setup.WEB_SITE_DESCRIPTION') : '';
    this.active = this.ctx.controller + '/' + this.ctx.action;
    if (this.isMobile) {
      return this.display(this.mtpl());
    } else {
      return this.display();
    }
  }

cart.js的indexAction()方法负责购物车的展示,先用this.meta_title设置标题,然后this.keywords设置购物车的关键词,this.description展示购物车描述。this.active用来编辑购物车,之后依旧是通过if语句判断客户端,根据客户端的不同返回不同的展示方式。

2.

 async stepperAction() {
    if (!this.is_login) {
      return this.fail('请先登录');
    }
    const data = this.post();
    console.log(data);
    const ids = data.ids.split('||');
    const stock = await this.model('cmswing/order').getstock(ids[0], ids[1]);
    // think.log(stock);
    if (data.qty > stock) {
      return this.fail('无货');
    } else {
      const goods = await this.model('cart').where({product_id: ids[0], type: ids[1] || '', uid: this.user.uid}).find();
      const datas = {
        id: goods.id,
        qty: data.qty,
        price: Number(data.qty) * goods.unit_price
      };
      await this.model('cart').update(datas);
      const res = await this.model('cart').find(goods.id);
      return this.success({name: '有货', data: res});
    }
  }

stepperAction()可以编辑购物车的数量。在编辑前先需要用户登陆,通过if语句来判断,若用户未登录则返回“请先登陆”。之后通过getstock检查商品的库存,结果保存到变量stock,if语句中通过data.qt获取用户需要的数量,若大于库存,则返回”无货“,反之代表用户可以购买添加购物车,返回给用户“有货”。

3.

 async delcartAction() {
    if (!this.is_login) {
      return this.fail('请先登录');
    }
    if (this.isAjax('post')) {
      const ids = this.post('ids');
      for (const val of ids.split('<>')) {
        const id = await this.model('cart').where({product_id: val.split('||')[0], type: val.split('||')[1] || '', uid: this.user.uid}).delete();
      }
      if (this.isMobile) {
        return this.success({name: '删除成功!', url: '/center/cart/index'});
      } else {
        return this.success({name: '删除成功!'});
      }
    } else {
      const ids = this.get('ids');
      console.log(ids);
      if (think.isEmpty(ids)) {
        return this.fail('选择要删除的商品');
      }

      this.assign('ids', decodeURI(ids));
      this.meta_title = '删除';
      this.active = '/center/cart/index';
      if (this.isMobile) {
        return this.display(this.mtpl());
      } else {
        return this.display();
      }
    }
  }

delcartAction()用来删除购物车.首先通过if语句判断用户是否登陆,若用户未登录,则先返回用户提示“请先登录”。之后偶判断用户使用的客户端,根据不同的客户端返回不同的删除方法。

4.

async addcartAction() {
    let data = this.post();
    data = think.extend({}, data);
    const stock = await this.model('cmswing/order').getstock(data.product_id, data.type);
    // think.log(stock);
    if (data.qty > stock) {
      return this.json(false);
    }
    console.log(data);
    // return false;
    const arr = [];
    const cart = this.cart.data;

    if (think.isEmpty(cart)) {
      arr.push(data);
    } else {
      // cart = JSON.parse(cart);
      console.log(cart);
      const typearr = [];
      const idarr = [];
      for (const item of cart) {
        if ((item.type == data.type) && (item.product_id == data.product_id)) {
          item.qty = Number(item.qty) + Number(data.qty);
        }
        arr.push(item);
        idarr.push(item.product_id);
        typearr.push(item.type);
      }
      if (!think.isEmpty(data.type)) {
        if (!in_array(data.type, typearr)) {
          arr.splice(0, 0, data);
        };
      } else {
        if (!in_array(data.product_id, idarr)) {
          arr.splice(0, 0, data);
        };
      }
    }

addcartAction()方法用来添加购物车。在添加之前要先来用getstock检查商品是否有库存,结果保存到变量stock。跟stepperAction()方法中相似地要在if语句中通过data.qt获取用户需要的数量,若大于库存,则不能加入购物车,返回错误。if语句通过之后通过think.isEmpty(cart)判断之前购物车是否为空,不为空则增加已有的购物车的数量,没有的话则直接通过下一个if语句添加商品。之后通过for循环获取并保存商品的价格、数量等信息,保存在相应的购物车中。最后通过if语句缓存购物车到相应的用户id下,完成添加购物车。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值