首先先展示拼团商品列表
拼团表
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"
}
]
}
}