|
@@ -143,19 +143,19 @@ module.exports = class extends Base {
|
|
|
|
|
|
// 统计商品总价
|
|
|
const orderGoodsData = [];
|
|
|
- for (const goodsItem of checkedGoodsList) {
|
|
|
+ for (const checkedGoods of checkedGoodsList) {
|
|
|
orderGoodsData.push({
|
|
|
order_id: orderId,
|
|
|
- goods_id: goodsItem.goods_id,
|
|
|
- goods_sn: goodsItem.goods_sn,
|
|
|
- product_id: goodsItem.product_id,
|
|
|
- goods_name: goodsItem.goods_name,
|
|
|
- list_pic_url: goodsItem.list_pic_url,
|
|
|
- market_price: goodsItem.market_price,
|
|
|
- retail_price: goodsItem.retail_price,
|
|
|
- number: goodsItem.number,
|
|
|
- goods_specifition_name_value: goodsItem.goods_specifition_name_value,
|
|
|
- goods_specifition_ids: goodsItem.goods_specifition_ids
|
|
|
+ goods_id: checkedGoods.goods_id,
|
|
|
+ goods_sn: checkedGoods.goods_sn,
|
|
|
+ product_id: checkedGoods.product_id,
|
|
|
+ goods_name: checkedGoods.goods_name,
|
|
|
+ list_pic_url: checkedGoods.list_pic_url,
|
|
|
+ market_price: checkedGoods.market_price,
|
|
|
+ retail_price: checkedGoods.retail_price,
|
|
|
+ number: checkedGoods.number,
|
|
|
+ goods_specifition_name_value: checkedGoods.goods_specifition_name_value,
|
|
|
+ goods_specifition_ids: checkedGoods.goods_specifition_ids
|
|
|
});
|
|
|
}
|
|
|
|
|
@@ -177,4 +177,101 @@ module.exports = class extends Base {
|
|
|
const latestExpressInfo = await this.model('order_express').getLatestOrderExpress(orderId);
|
|
|
return this.success(latestExpressInfo);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 立即购买(一件商品)
|
|
|
+ * @returns {Promise.<void>}
|
|
|
+ * post:
|
|
|
+ * brandId: 商户的id
|
|
|
+ * goodsId: 购买的商品的id
|
|
|
+ * goodsCount: 购买数量
|
|
|
+ */
|
|
|
+ async buynowAction() {
|
|
|
+ // 获取收货地址信息和计算运费
|
|
|
+ const brandId = this.post('brandId'); // 商户id
|
|
|
+ let checkedAddress = await this.model('address').where({ brandId: brandId }).find(); // 商户地址
|
|
|
+ if (think.isEmpty(checkedAddress)) {
|
|
|
+ checkedAddress = await this.model('address').where({ id: 1 }).find(); // 默认地址
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取要购买的商品
|
|
|
+ const checkedGoodsId = this.post('goodsId');
|
|
|
+ const checkedGoods = await this.model('goods').where({ id: checkedGoodsId }).find(); // 获取商品信息
|
|
|
+ if (think.isEmpty(checkedGoods)) {
|
|
|
+ return this.fail('商品已下架');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 统计商品总价
|
|
|
+ const goodsTotalPrice = parseInt(this.post('goodsCount')) * Math.ceil(checkedGoods.retail_price / 50) * 50; // 价格取整
|
|
|
+
|
|
|
+ const currentTime = parseInt(this.getTime() / 1000);
|
|
|
+ // 更新用户余额
|
|
|
+ const user = await this.model('user').where({id: this.getLoginUserId()}).find();
|
|
|
+ if (think.isEmpty(user) || parseInt(user.isAuthen) === 0) return this.fail('用户未注册');
|
|
|
+ if (user.balance < goodsTotalPrice) return this.fail('余额不足');
|
|
|
+ think.logger.debug(user.balance + '-=' + goodsTotalPrice);
|
|
|
+ await this.model('user').where({id: this.getLoginUserId()}).update({
|
|
|
+ balance: user.balance - goodsTotalPrice
|
|
|
+ });
|
|
|
+ // 更新商品数量
|
|
|
+ if (checkedGoods.goods_number < this.post('goodsCount')) this.fail('库存不足');
|
|
|
+ await this.model('goods').where({id: checkedGoodsId}).update({
|
|
|
+ goods_number: checkedGoods.goods_number - this.post('goodsCount')
|
|
|
+ });
|
|
|
+
|
|
|
+ const orderInfo = {
|
|
|
+ order_sn: this.model('order').generateOrderNumber(),
|
|
|
+ user_id: this.getLoginUserId(),
|
|
|
+
|
|
|
+ // 收货地址和运费
|
|
|
+ consignee: checkedAddress.name,
|
|
|
+ mobile: checkedAddress.mobile,
|
|
|
+ province: checkedAddress.province_id,
|
|
|
+ city: checkedAddress.city_id,
|
|
|
+ district: checkedAddress.district_id,
|
|
|
+ address: checkedAddress.address,
|
|
|
+ freight_price: 0.00,
|
|
|
+
|
|
|
+ // 留言
|
|
|
+ postscript: '',
|
|
|
+
|
|
|
+ // 使用的优惠券
|
|
|
+ coupon_id: 0,
|
|
|
+ coupon_price: 0,
|
|
|
+
|
|
|
+ add_time: currentTime,
|
|
|
+ goods_price: goodsTotalPrice,
|
|
|
+ order_price: goodsTotalPrice,
|
|
|
+ actual_price: goodsTotalPrice
|
|
|
+ };
|
|
|
+
|
|
|
+ // 开启事务,插入订单信息和订单商品
|
|
|
+ const orderId = await this.model('order').add(orderInfo);
|
|
|
+ orderInfo.id = orderId;
|
|
|
+ if (!orderId) {
|
|
|
+ return this.fail('订单提交失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 统计商品总价
|
|
|
+ const orderGoodsData = [];
|
|
|
+ for (let i = 0; i < this.post('goodsCount'); i++) {
|
|
|
+ orderGoodsData.push({
|
|
|
+ order_id: orderId,
|
|
|
+ goods_id: checkedGoods.goods_id,
|
|
|
+ goods_sn: checkedGoods.goods_sn,
|
|
|
+ product_id: checkedGoods.product_id,
|
|
|
+ goods_name: checkedGoods.goods_name,
|
|
|
+ list_pic_url: checkedGoods.list_pic_url,
|
|
|
+ market_price: checkedGoods.market_price,
|
|
|
+ retail_price: checkedGoods.retail_price,
|
|
|
+ number: checkedGoods.number,
|
|
|
+ goods_specifition_name_value: checkedGoods.goods_specifition_name_value,
|
|
|
+ goods_specifition_ids: checkedGoods.goods_specifition_ids
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ await this.model('order_goods').addMany(orderGoodsData);
|
|
|
+
|
|
|
+ return this.success({ orderInfo: orderInfo });
|
|
|
+ }
|
|
|
};
|