商城-商品拼团

首先先展示拼团商品列表

 

 拼团表

CREATE TABLE `team_goods` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `goods_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '拼团商品id',
  `team_price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '拼团商品价格',
  `team_num` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '几人团',
  `validity_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '开团有效期(小时)',
  `limit_num` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '已参团人数(添加虚拟数量)',
  `astrict_num` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '限购数量',
  `tc_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '频道id',
  `is_audit` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '0未审核,1未通过,2通过',
  `is_team` tinyint(3) unsigned NOT NULL DEFAULT '1' COMMENT '显示0否 1显示',
  `sort_order` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '排序',
  `team_desc` varchar(255) NOT NULL DEFAULT '' COMMENT '拼团介绍',
  `isnot_aduit_reason` varchar(255) NOT NULL DEFAULT '' COMMENT '审核未通过理由',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

以thinkphp6 为例

拼团模型

<?php
namespace app\model;

use app\model\Base as Model;

class TeamGoods extends Model
{
    //一个拼团对用一个Goods
    public function hasOneGoods(){

        return $this->hasOne(Goods::class,'goods_id','goods_id');
    }
    //商品
    public function goods(){

        return self::hasOneGoods();
    }
}

 控制器代码

<?php

namespace app\api\controller;

use app\BaseController;
use think\Request;

class TeamGoods extends BaseController
{

    public function index()
    {
        $TeamGoods = new \app\model\TeamGoods();
        $limit = 10;
        $team_goods = $TeamGoods->field([
            'id',
            'goods_id',
            'team_price',//团购价格
            'team_num',//几人团
            'limit_num',
        ])->with([
            'goods' => function($query){
                $query->field(['goods_id', 'goods_name', 'goods_img', 'goods_thumb', 'shop_price'])->bind(['goods_name', 'goods_img', 'goods_thumb', 'shop_price']);
            }])->paginate($limit);

        if($team_goods){
            return json(['code' => 200, 'msg' => '操作成功', 'data' => $team_goods]);
        }else{
            return json(['code' => 201, 'msg' => '无此数据']);
        }
    }
}

最终得到数据结构 

{
    "code": 200,
    "msg": "操作成功",
    "data": {
        "total": 4,
        "per_page": 10,
        "current_page": 1,
        "last_page": 1,
        "data": [
            {
                "id": 1,
                "goods_id": 697,
                "team_price": "20.00",
                "team_num": 2,
                "limit_num": 0,
                "goods_name": "",
                "goods_img": "",
                "goods_thumb": "",
                "shop_price": "599.00"
            },
            {
                "id": 2,
                "goods_id": 903,
                "team_price": "12.00",
                "team_num": 2,
                "limit_num": 0,
                "goods_name": "",
                "goods_img": "",
                "goods_thumb": "",
                "shop_price": "798.00"
            },
            {
                "id": 3,
                "goods_id": 622,
                "team_price": "99.00",
                "team_num": 2,
                "limit_num": 0,
                "goods_name": "",
                "goods_img": "",
                "goods_thumb": "",
                "shop_price": "8999.00"
            },
            {
                "id": 4,
                "goods_id": 667,
                "team_price": "555.00",
                "team_num": 3,
                "limit_num": 0,
                "goods_name": "Apple/苹果 iPhone 7",
                "goods_img": "",
                "goods_thumb": "",
                "shop_price": "6288.00"
            }
        ]
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值