2024-01-16 13:06:20 +08:00

126 lines
4.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权购买去版权授权后方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\api\validate;
use think\Db;
use think\Validate;
class Team extends Validate
{
protected $rule = [
'item_id' => 'require|integer|checkTeamGoods',
'goods_num' => 'require|integer',
'action' => 'require',
'pay_way' =>'require|in:1,2,3',
'found_id' => 'checkTeamFound',
'order_id' => 'require',
'team_id' => 'require|checkTeam',
];
protected $message = [
'item_id.require' => '请选择商品规格',
'goods_num.require' => '请选择商品数量',
'goods_num.integer' => '商品数量错误',
'action.require' => '订单错误',
'pay_way.require' => '支付方式错误',
'found_id.require' => '参数缺失',
'order_id.require' => '参数缺失',
'team_id.require' => '参数缺失',
];
protected function sceneAdd()
{
$this->only(['action', 'pay_way', 'goods_num', 'item_id', 'found_id', 'team_id']);
}
protected function sceneCheck()
{
$this->only(['team_id', 'found_id']);
}
//验证是否为拼团商品
protected function checkTeamGoods($value, $rule, $data = [])
{
$team_goods = Db::name('team_activity')->alias('a')
->join('team_goods_item ti', 'ti.team_id = a.team_id')
->join('goods_item gi', 'gi.id = ti.item_id')
->where(['a.status' => 1, 'a.del' => 0])
->whereBetweenTimeField('a.start_time','a.end_time')
->where(['ti.item_id' => $value])
->find();
if (!$team_goods){
return '不在活动时间内或活动已结束';
}
//商品库存是否足够
if ($data['goods_num'] > $team_goods['stock']){
return '库存不足';
}
return true;
}
//参团时验证团
protected function checkTeamFound($value, $rule, $data = [])
{
if (empty($value)){
return true;
}
$team_found = Db::name('team_found')
->where(['id' => $value, 'status' => 0])
->find();
if (!$team_found){
return '该团已结束';
}
// if ($team_found['join'] >= $team_found['need']){
// return ['code' => 20001, 'msg' => '该团已满员'];
// }
//
//当前用户是否已参与团
$follow = Db::name('team_follow')
->where(['found_id' => $value, 'follow_user_id' => $data['user_id']])
->find();
if ($follow){
return '你已参加该团,无法再次参加了!';
}
return true;
}
protected function checkTeam($value, $rule, $data = [])
{
$team = Db::name('team_activity')->where(['team_id' => $value])->find();
if ($team['end_time'] < time()){
return '已过拼团活动截止时间';
}
return true;
}
}