'use strict'; const shopController = require('./shop.js'); // 商品接口控制器 module.exports = class ProductsController extends shopController { // 使用模型 get useModel() { const that = this; return that.app.model.Products; } /** * [detailValidate 查看商品详情验证器] * @return {[type]} [description] */ get detailValidate() { const that = this; return { product_id: that.ctx.rules.name('商品ID') .required() .number(), }; } // 商品搜索验证器 get searchValidate() { const that = this; return { keyword: that.ctx.rules.name('商品关键字') .required() .trim() .notEmpty(), }; } /** * [commentValidate 商品评论] * @return {[type]} [description] */ get commentValidate() { const that = this; return { order_id: that.ctx.rules.name('订单ID') .required() .notEmpty() .number(), product_id: that.ctx.rules.name('商品ID') .required() .notEmpty() .number(), product_rate: that.ctx.rules.name('商品评分') .required() .notEmpty() .number(), user_id: that.ctx.rules.default(that.service.shop.getWebUserId()) .number(), comment: that.ctx.rules.name('评论内容') .required(), create_time: that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s')) .required(), }; } /** * [commentListValidate 获取商品评论列表] * @return {[type]} [description] */ get commentListValidate() { const that = this; return { product_id: that.ctx.rules.name('商品ID') .required() .notEmpty() .number(), page: that.ctx.rules.default(1) .number(), limit: that.ctx.rules.default(50) .number(), }; } /** * [homeProduct 获取家用家具首页商品] * @return {[type]} [description] */ async homeProduct() { const that = this; try { const seq = that.app.Sequelize; // 2023/9/19 查询家具分类 const categoryResult = await that.app.model.ProductCategory.findAll({ where: { pid: 4 }, // 2023/9/19 家用家具 }); const categorys = JSON.parse(JSON.stringify(categoryResult)); const cate = []; for (const category of categorys) { cate.push(category.category_id); } const result = await that.useModel.findAll({ // offset: 0, limit: 100, where: { is_home: 1, product_stock: { [seq.Op.gte]: 1 }, is_sale: 1, category_id: { [seq.Op.in]: cate } }, attributes: [ 'product_id', 'category_id', 'product_name', 'product_image', 'shop_price', 'market_price', 'is_new', 'is_hot' ], order: [ [ 'is_top', 'desc' ], [ 'is_hot', 'desc' ], [ 'is_new', 'desc' ], [ 'sale_count', 'desc' ] ], }); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [homeProduct 获取办公家具首页商品] * @return {[type]} [description] * http://192.168.18.188:8106/oneshop/api/officeHomeProducts */ async officeHomeProducts() { const that = this; try { const seq = that.app.Sequelize; // 2023/9/19 查询家具分类 const categoryResult = await that.app.model.ProductCategory.findAll({ where: { pid: 1 }, // 2023/9/19 办公家具 }); const categorys = JSON.parse(JSON.stringify(categoryResult)); const cate = []; for (const category of categorys) { cate.push(category.category_id); } const result = await that.useModel.findAll({ // offset: 0, limit: 100, where: { is_home: 1, product_stock: { [seq.Op.gte]: 1 }, is_sale: 1, category_id: { [seq.Op.in]: cate } }, attributes: [ 'product_id', 'category_id', 'product_name', 'product_image', 'shop_price', 'market_price', 'is_new', 'is_hot' ], order: [ [ 'is_top', 'desc' ], [ 'is_hot', 'desc' ], [ 'is_new', 'desc' ], [ 'sale_count', 'desc' ], [ 'product_stock', 'desc' ], [ 'category_id', 'asc' ] ], }); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [search 商品搜索] * @return {[type]} [description] */ async search() { const that = this; try { const seq = that.app.Sequelize; const data = await that.ctx.validate(that.searchValidate, await that.ctx.getParse()); // 2023/9/19 查询家具分类 1:办公家具 4:家用家具 const opts = data.pid ? { where: { pid: data.pid }, } : {}; console.log(opts); const categoryResult = await that.app.model.ProductCategory.findAll(opts); const categorys = JSON.parse(JSON.stringify(categoryResult)); console.log(categorys); const cate = []; for (const category of categorys) { cate.push(category.category_id); } const result = await that.useModel.findAll({ // offset: 0, limit: 100, order: [ [ 'is_top', 'desc' ], [ 'is_hot', 'desc' ], [ 'is_new', 'desc' ], [ 'product_id', 'desc' ] ], where: { product_name: { [that.app.Sequelize.Op.regexp]: data.keyword }, product_stock: { [seq.Op.gte]: 1 }, is_sale: 1, category_id: { [seq.Op.in]: cate } }, attributes: [ 'product_id', 'category_id', 'product_name', 'product_image', 'shop_price', 'market_price', 'is_new', 'is_hot' ], }); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [detail 查看商品详情] * @return {[type]} [description] */ async detail() { const that = this; try { const seq = that.app.Sequelize; const data = await that.ctx.validate(that.detailValidate, await that.ctx.getParse()); const result = await that.useModel.findOne({ where: { product_id: data.product_id, is_sale: 1 }, include: [ { model: that.app.model.ProductContents, as: 'contents', attributes: [] }, { model: that.app.model.ProductCarousels, as: 'carousels', attributes: [] }, { model: that.app.model.ProductCategory, as: 'categorys', attributes: [] }, { model: that.app.model.ProductSku, as: 'productSkus', attributes: { exclude: [ 'create_time' ] } }, ], attributes: { include: [ [ seq.col('categorys.category_name'), 'category_name' ], [ seq.col('categorys.pid'), 'pid' ], [ seq.col('contents.desction'), 'product_content' ], [ seq.col('carousels.content'), 'product_carouse' ], ], exclude: [ 'carousel_id', 'content_id', 'supplier_id', 'admin_id', 'update_time', 'create_time' ], }, }); if (result) result.setDataValue('spes', await that.detailSpes(data.product_id)); // let real_total = await that.service.shop.productSaleCount(data.product_id); if (result) result.setDataValue('product_sale_count', Number(result.sale_count)); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [detailSpes 获取商品规格] * @param {Number} product_id [description] * @return {[type]} [description] */ async detailSpes(product_id = 0) { const that = this; const seq = that.app.Sequelize; const result = await that.app.model.ProductSpes.findAll({ where: { product_id }, include: [ { model: that.app.model.ProductTypesItem, as: 'product_types_item', attributes: [] }, ], attributes: { include: [ [ seq.col('product_types_item.item_name'), 'item_name' ] ], exclude: [ 'product_id', 'item_id', 'admin_id', 'create_time', 'update_time', 'spe_id' ], }, }); return result; } /** * [comment 商品评论] * @return {[type]} [description] */ async comment() { const that = this; let transaction; try { const data = await that.ctx.validate(that.commentValidate, await that.ctx.postParse()); transaction = await that.app.model.transaction(); const createBean = await that.app.comoBean.instance(data); const result = await that.service.base.create(createBean, that.app.model.ProductComment, '商品评论添加失败,请稍候重试'); const updateBean = await that.app.comoBean.instance({ order_status: 4, success_time: that.app.szjcomo.date('Y-m-d H:i:s'), }, { where: { order_id: data.order_id, }, transaction, }); await that.service.base.update(updateBean, that.app.model.Orders, '更新订单交易完成失败,请稍候重试'); await that.service.order.orderAction({ order_id: data.order_id, admin_id: 0, action_desc: '商品进行评价', }, transaction); await transaction.commit(); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } catch (err) { if (transaction) await transaction.rollback(); return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [commentList 商品评论列表] * @return {[type]} [description] */ async commentList() { const that = this; try { const data = await that.ctx.validate(that.commentListValidate, await that.ctx.getParse()); const seq = that.app.Sequelize; const selectBean = await that.app.comoBean.instance(data, { offset: (data.page - 1) * data.limit, limit: data.limit, where: { product_id: data.product_id, status: 1 }, include: [ { model: that.app.model.Users, as: 'users', attributes: [] }, ], attributes: [ [ seq.col('users.nickname'), 'nickname' ], [ seq.col('users.account_name'), 'account_name' ], [ seq.col('users.headimgurl'), 'headimgurl' ], 'comment', 'create_time', ], order: [ [ 'comment_id', 'desc' ] ], }); const result = await that.service.base.select(selectBean, that.app.model.ProductComment, '查询商品评论失败,请稍候重试', true, true); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } };