// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\adminapi\controller\v1\marketing\lottery; use app\adminapi\controller\AuthController; use app\services\activity\lottery\LuckLotteryServices; use think\facade\App; /** * 抽奖活动 * Class LuckLottery * @package app\controller\admin\v1\marketing\lottery */ class LuckLottery extends AuthController { /** * LuckLottery constructor. * @param App $app * @param LuckLotteryServices $services */ public function __construct(App $app, LuckLotteryServices $services) { parent::__construct($app); $this->services = $services; } /** * 抽奖列表 * @return mixed */ public function index() { $where = $this->request->getMore([ ['factor', ''], ['start', ''], ['status', ''], ['time', ''], ['keyword', ''], ]); return app('json')->success($this->services->getList($where)); } /** * 抽奖活动详情 * @param $id * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function detail($id) { if (!$id) { return app('json')->fail('参数错误'); } return app('json')->success($this->services->getLotteryInfo((int)$id)); } /** * 添加抽奖 * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function add() { $data = $this->request->postMore([ ['name', ''], ['desc', ''], ['image', ''], ['factor', 1], ['factor_num', 1], ['attends_user', 1], ['user_level', []], ['user_label', []], ['is_svip', 0], ['period', [0, 0]], ['lottery_num_term', 1], ['lottery_num', 1], ['spread_num', 1], ['is_all_record', 1], ['is_personal_record', 1], ['is_content', 1], ['content', ''], ['status', 1], ['prize', []] ]); if (!$data['name']) { return app('json')->fail('请添加抽奖活动名称'); } if ($data['is_content'] && !$data['content']) { return app('json')->fail('请添加抽奖描述等文案'); } [$start, $end] = $data['period']; unset($data['period']); $data['start_time'] = $start ? strtotime($start) : 0; $data['end_time'] = $end ? strtotime($end) + 86399 : 0; if ($data['start_time'] && $data['end_time'] && $data['end_time'] <= $data['start_time']) { return app('json')->fail('活动结束时间必须大于开始时间'); } if (!$data['prize']) { return app('json')->fail('请添加奖品'); } if (in_array($data['factor'], [1, 2]) && !$data['factor_num']) { return app('json')->fail('请填写消耗数量'); } return app('json')->success($this->services->add($data) ? '保存成功' : '保存失败'); } /** * 修改抽奖 * @param $id * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function edit($id) { $data = $this->request->postMore([ ['name', ''], ['desc', ''], ['image', ''], ['factor', 1], ['factor_num', 1], ['attends_user', 1], ['user_level', []], ['user_label', []], ['is_svip', 0], ['period', [0, 0]], ['lottery_num_term', 1], ['lottery_num', 1], ['spread_num', 1], ['is_all_record', 1], ['is_personal_record', 1], ['is_content', 1], ['content', ''], ['status', 1], ['prize', []] ]); if (!$id) { return app('json')->fail('参数错误'); } if (!$data['name']) { return app('json')->fail('请添加抽奖活动名称'); } [$start, $end] = $data['period']; unset($data['period']); $data['start_time'] = $start ? strtotime($start) : 0; $data['end_time'] = $end ? strtotime($end) + 86399 : 0; if ($data['start_time'] && $data['end_time'] && $data['end_time'] <= $data['start_time']) { return app('json')->fail('活动结束时间必须大于开始时间'); } if ($data['is_content'] && !$data['content']) { return app('json')->fail('请添加抽奖描述等文案'); } if (!$data['prize']) { return app('json')->fail('请添加奖品'); } if (in_array($data['factor'], [1, 2]) && !$data['factor_num']) { return app('json')->fail('请填写消耗数量'); } return app('json')->success($this->services->edit((int)$id, $data) ? '修改成功' : '修改失败'); } /** * 删除抽奖 * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function delete() { list($id) = $this->request->getMore([ ['id', 0], ], true); if (!$id) return app('json')->fail('数据不存在'); $this->services->delLottery((int)$id); return app('json')->success('删除成功'); } /** * 设置活动状态 * @param string $id * @param string $status * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function setStatus($id = '', $status = '') { if ($status == '' || $id == '') return app('json')->fail('参数错误'); $this->services->setStatus((int)$id, (int)$status); return app('json')->success('设置成功'); } public function factorList() { return app('json')->success($this->services->factorList()); } public function factorUse() { $data = $this->request->postMore([ [['point', 'd'], 0], [['pay', 'd'], 0], [['evaluate', 'd'], 0], ]); $this->services->factorUse($data); return app('json')->success('保存成功'); } }