'use strict'; const shopController = require('./shop.js'); // 购物车控制器 module.exports = class CartController extends shopController { /** * [useModel 使用模型] * @return {[type]} [description] */ get useModel() { const that = this; return that.app.model.Carts; } /** * [createValidate 商品加入购物车] * @return {[type]} [description] */ get createValidate() { const that = this; return { product_id: that.ctx.rules.name('商品ID') .required() .number(), category_id: that.ctx.rules.name('商品分类ID') .required() .number(), pid: that.ctx.rules.name('商品分类父级ID') .required() .number(), user_id: that.ctx.rules.default(that.service.shop.getWebUserId()) .number(), product_count: that.ctx.rules.default(1) .number(), is_select: that.ctx.rules.default(1) .number(), create_time: that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s')) .required(), }; } /** * [updateValidate 更新购物车] * @return {[type]} [description] */ get updateValidate() { const that = this; return { data: that.ctx.rules.name('购物车数据') .required() .is_array(), user_id: that.ctx.rules.default(that.service.shop.getWebUserId()) .number(), update_time: that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s')) .required(), }; } /** * [deleteValidate 删除购物车商品] * @return {[type]} [description] */ get deleteValidate() { const that = this; return { product_id: that.ctx.rules.name('商品ID') .required() .number(), user_id: that.ctx.rules.default(that.service.shop.getWebUserId()) .number(), }; } /** * [selectValidate 查询购物车商品] * @return {[type]} [description] */ get selectValidate() { const that = this; return { user_id: that.ctx.rules.default(that.service.shop.getWebUserId()) .number(), is_select: that.ctx.rules.default(0) .number(), pid: that.ctx.rules.name('商品分类父级ID') .default(4) .required() .number(), }; } /** * [cartTotalValidate 获取购物车商品件数] * @return {[type]} [description] */ get cartTotalValidate() { const that = this; return { user_id: that.ctx.rules.default(0) .number(), pid: that.ctx.rules.name('商品分类父级ID') .default(4) .required() .number(), }; } /** * [create 添加购物车] * @return {[type]} [description] */ async create() { const that = this; try { const data = await that.ctx.validate(that.createValidate, await that.ctx.postParse()); const info = await that.useModel.findOne({ where: { user_id: data.user_id, product_id: data.product_id, volume: data.volume, price: data.price }, attributes: [ 'product_count', 'cart_id' ], raw: true, }); if (!info) { // 2023/2/27 加入购物车 补充产品信息 const res = await that.app.model.Products.findOne({ where: { product_id: data.product_id }, attributes: [ 'category_id', 'dinning_coin_amount', 'dining_partner_id' ], }); data.category_id = res.category_id; data.dinning_coin_amount = res.dinning_coin_amount; data.dining_partner_id = res.dining_partner_id; const createBean = await that.app.comoBean.instance(data); const result = await that.service.base.create(createBean, that.useModel, '购物车商品添加失败,请重试'); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } const updateBean = await that.app.comoBean.instance({ product_count: info.product_count + data.product_count, is_select: 1, update_time: that.app.szjcomo.date('Y-m-d H:i:s'), }, { where: { cart_id: info.cart_id } }); const result = await that.service.base.update(updateBean, that.useModel, '加入购物车失败,请稍候重试'); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [update 更新购物车] * @return {[type]} [description] */ async update() { const that = this; let transaction; try { const data = await that.ctx.validate(that.updateValidate, await that.ctx.anyParse()); transaction = await that.app.model.transaction(); for (let i = 0; i < data.data.length; i++) { const item = data.data[i]; const updateBean = await that.app.comoBean.instance({ product_count: Number(item.product_count), is_select: Number(item.is_select), random_key: that.app.szjcomo.mt_rand(10000, 99999), update_time: that.app.szjcomo.date('Y-m-d H:i:s'), }, { where: { user_id: data.user_id, product_id: item.product_id, volume: item.volume }, transaction }); await that.service.base.update(updateBean, that.useModel, '更新购物车失败,请重试'); } await transaction.commit(); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', true, false)); } catch (err) { if (transaction) await transaction.rollback(); return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [delete 删除购物车商品] * @return {[type]} [description] */ async delete() { const that = this; try { const data = await that.ctx.validate(that.deleteValidate, await that.ctx.anyParse()); const deleteBean = await that.app.comoBean.instance(data, { where: { user_id: data.user_id, product_id: data.product_id, volume: data.volume, }, }); const result = await that.service.base.delete(deleteBean, that.useModel, '删除购物车商品失败,请稍候重试'); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [select 查询购物车商品] * @return {[type]} [description] */ async select() { const that = this; try { const data = await that.ctx.validate(that.selectValidate, await that.ctx.getParse()); const seq = that.app.Sequelize; const options = { where: { user_id: data.user_id, pid: data.pid }, include: [ { model: that.app.model.Products, as: 'products', attributes: [] }, ], attributes: [ [ seq.col('products.product_name'), 'product_name' ], [ seq.col('products.product_image'), 'product_image' ], [ seq.col('products.shop_price'), 'shop_price' ], [ seq.col('products.market_price'), 'market_price' ], 'product_id', 'product_count', 'is_select', 'category_id', 'volume', 'price', 'cart_id', 'pid', ], }; if (data.is_select) options.where.is_select = 1; const selectBean = await that.app.comoBean.instance(data, options); const result = await that.service.base.select(selectBean, that.useModel, '查询购物车商品列表失败,请稍候重试', true); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [cartTotal 获取购物车商品数量] * @return {[type]} [description] */ async cartTotal() { const that = this; try { const data = await that.ctx.validate(that.cartTotalValidate, await that.ctx.getParse()); if (that.ctx.request.header.weblogintoken) { data.user_id = await that.service.shop.getWebUserId(); const total = await that.useModel.count({ where: { user_id: data.user_id, pid: data.pid } }); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', total, false)); } return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', 0, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } };