123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- <?php
- namespace App\Http\Controllers\Admin;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use App\Models\User\UserModel;
- use App\Models\Welfare\LotteryModel;
- use App\Models\Welfare\TicketModel;
- use App\Models\Welfare\PrizeModel;
- use App\Models\Welfare\StoreModel;
- class WelfareController extends Controller
- {
- /**
- * 创建抽奖活动
- * @param Request $request
- * @return array|\Illuminate\Http\JsonResponse
- */
- public function createLottery(Request $request)
- {
- $this->validate($request, [
- 'title' => 'required|max:32',
- 'drawed_at' => 'required|integer',
- 'showed_at' => 'required|integer',
- 'cost' => 'required|max:16',
- 'cover_url' => 'required|url',
- 'banner_url' => 'url',
- 'count' => 'required|integer',
- 'lottery_explain' => 'required',
- ], [
- 'title.required' => '请输入标题',
- 'title.max' => '字数不能超过32个字符',
- 'cost.required' => '请输入价格',
- 'cost.max' => '字数不能超过16个字符',
- 'drawed_at.*' => '请选择开奖时间',
- 'showed_at.*' => '清选择显示时间',
- 'cover_url.*' => '上传封面',
- 'banner_url.*' => '上传长图',
- 'count.*' => '请填写数字',
- 'lottery_explain.*' => '抽奖说明',
- ]);
- $lottery = LotteryModel::create($request->all());
- return response()->json([
- 'code' => 200,
- 'message' => 'OK',
- 'data' => $lottery
- ]);
- }
- /**
- * 修改活动
- * @param Request $request
- * @return array|\Illuminate\Http\JsonResponse
- */
- public function updateLottery(Request $request)
- {
- $this->validate($request, [
- 'id' => 'required|integer',
- 'title' => 'max:32',
- 'cost' => 'max:16',
- 'drawed_at' => 'integer',
- 'showed_at' => 'integer',
- 'cover_url' => 'url',
- 'count' => 'integer',
- 'deleted_at' => 'integer',
- ], [
- 'title.*' => '字数不能超过32个字符',
- 'cost.*' => '字数不能超过16个字符',
- 'drawed_at.*' => '请选择开奖时间',
- 'showed_at.*' => '清选择显示时间',
- 'cover_url.*' => '上传图片',
- 'count.*' => '请填写数字',
- 'lottery_explain.*' => '抽奖说明',
- 'receive_explain.*' => '兑奖说明',
- ]);
- $lottery = LotteryModel::findOrFail($request->id);
- $lottery->fill($request->all());
- $lottery->save();
- return response()->json([
- 'code' => 200,
- 'message' => 'OK',
- 'data' => $lottery
- ]);
- }
- /**
- * 抽奖活动列表
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- */
- public function lotterys(Request $request)
- {
- $per_page = $request->input('per_page', 10);
- $page = $request->input('page', 1);
- $offset = ($page - 1) * $per_page;
- $lotterys = LotteryModel::when($request->filled('draw'), function ($query) use ($request) {
- if ($request->draw) {
- return $query->where('drawed_at', '<=', time());
- } else {
- return $query->where('drawed_at', '>', time());
- }
- });
- $count = $lotterys->count();
- $list = $lotterys->orderBy('drawed_at', 'desc')->offset($offset)->limit($per_page)->get();
- foreach ($list as $lottery) {
- $lottery->ticket_count = TicketModel::where('lottery_id', $lottery->id)->count();
- $lottery->ticket_number = TicketModel::where('lottery_id', $lottery->id)->distinct()->count('uid');
- }
- return response()->json([
- 'code' => 200,
- 'message' => 'OK',
- 'data' => [
- 'list' => $list,
- 'count' => $count,
- 'per_page' => $per_page,
- 'page' => $page
- ]
- ]);
- }
- /**
- * 兑换码列表
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- */
- public function tickets(Request $request)
- {
- $per_page = $request->input('per_page', 20);
- $page = $request->input('page', 1);
- $offset = ($page - 1) * $per_page;
- $builder = TicketModel::when($request->filled('lottery_id'), function ($query) use ($request) {
- return $query->where('lottery_id', $request->lottery_id);
- })->when($request->filled('uid'), function ($query) use ($request) {
- return $query->where('uid', $request->uid);
- })->when($request->filled('ticket_code'), function ($query) use ($request) {
- return $query->where('ticket_code', $request->ticket_code);
- })->when($request->filled('lucky'), function ($query) use ($request) {
- return $query->where('lucky', $request->lucky);
- });
- $total = $builder->count();
- $tickets = $builder->orderBy('id', 'desc')->offset($offset)->limit($per_page)->get();
- foreach ($tickets as $ticket) {
- $ticket->username = UserModel::select('uid', 'nickname', 'headimgurl')->where('uid', $ticket->uid)->first();
- $ticket->lottery = LotteryModel::select('title')->where('id', $ticket->lottery_id)->first();
- }
- return response()->json([
- 'code' => 200,
- 'message' => 'OK',
- 'data' => [
- 'list' => $tickets,
- 'count' => $total,
- 'per_page' => $per_page,
- 'page' => $page
- ]
- ]);
- }
- /**
- * 修改ticket
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- */
- public function updateTicket(Request $request)
- {
- $this->validate($request, [
- 'id' => 'required|integer',
- 'lucky' => 'boolean',
- ], [
- 'id.*' => '请选择一个抽奖码',
- 'lucky.*' => '请设置是否中奖',
- ]);
- $ticket = TicketModel::findOrFail($request->id);
- $ticket->fill($request->all());
- $ticket->save();
- return response()->json([
- 'code' => '200',
- 'message' => 'OK'
- ]);
- }
- /**
- * 商品列表
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- */
- public function stores(Request $request)
- {
- $per_page = $request->input('per_page', 10);
- $page = $request->input('page', 1);
- $offset = ($page - 1) * $per_page;
- $builder = new StoreModel();
- $total = $builder->count();
- $lotterys = $builder->orderBy('id', 'desc')->offset($offset)->limit($per_page)->get();
- return response()->json([
- 'code' => 200,
- 'message' => 'OK',
- 'data' => [
- 'list' => $lotterys,
- 'count' => $total,
- 'per_page' => $per_page,
- 'page' => $page
- ]
- ]);
- }
- /**
- * 创建商品
- * @param Request $request
- * @return array|\Illuminate\Http\JsonResponse
- */
- public function createStore(Request $request)
- {
- $this->validate($request, [
- 'title' => 'required|max:32',
- 'cost' => 'required|max:16',
- 'cover_url' => 'required|url',
- 'price' => 'required|integer',
- 'state' => 'boolean',
- 'count' => 'required|integer',
- 'receive_explain' => 'required',
- ], [
- 'title.required' => '请输入标题',
- 'title.max' => '字数不能超过32个字符',
- 'cost.required' => '请输入价格',
- 'cost.max' => '字数不能超过16个字符',
- 'drawed_at.*' => '请选择开奖时间',
- 'cover_url.*' => '上传封面图片',
- 'price.*' => '请设置拼图数量',
- 'state.*' => '请设置是否上下架',
- 'count.*' => '请填写数字',
- 'receive_explain.*' => '兑奖说明',
- ]);
- $store = StoreModel::create($request->all());
- return response()->json([
- 'code' => 200,
- 'message' => 'OK',
- 'data' => $store,
- ]);
- }
- /**
- * 修改商品
- * @param Request $request
- * @return array|\Illuminate\Http\JsonResponse
- */
- public function updateStore(Request $request)
- {
- $this->validate($request, [
- 'id' => 'required|integer',
- 'title' => 'max:32',
- 'cost' => 'max:16',
- 'cover_url' => 'url',
- 'price' => 'integer',
- 'state' => 'boolean',
- 'count' => 'integer',
- ], [
- 'id.*' => '请选择一个商品',
- 'title.max' => '字数不能超过32个字符',
- 'cost.max' => '字数不能超过16个字符',
- 'drawed_at.*' => '请选择开奖时间',
- 'cover_url.*' => '上传封面图片',
- 'price.*' => '请设置拼图数量',
- 'state.*' => '请设置是否上下架',
- 'count.*' => '请填写数字',
- ]);
- $store = StoreModel::findOrFail($request->id);
- $store->fill($request->all());
- $store->save();
- return response()->json([
- 'code' => 200,
- 'message' => 'OK',
- 'data' => $store,
- ]);
- }
- /**
- * 奖品兑换列表
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- */
- public function prizes(Request $request)
- {
- $per_page = $request->input('per_page', 20);
- $page = $request->input('page', 1);
- $offset = ($page - 1) * $per_page;
- $builder = PrizeModel::when($request->filled('title'), function ($query) use ($request) {
- return $query->where('title', $request->title);
- })->when($request->filled('name'), function ($query) use ($request) {
- return $query->where('name', $request->name);
- })->when($request->filled('uid'), function ($query) use ($request) {
- return $query->where('uid', $request->uid);
- })->when($request->filled("state"), function ($query) use ($request) {
- return $query->where('state', $request->state);
- });
- $total = $builder->count();
- $prizes = $builder->orderBy('id', 'desc')->offset($offset)->limit($per_page)->get();
- return response()->json([
- 'code' => 200,
- 'message' => 'OK',
- 'data' => [
- 'list' => $prizes,
- 'count' => $total,
- 'per_page' => $per_page,
- 'page' => $page
- ]
- ]);
- }
- /**
- * 领取奖品
- * @param Request $request
- * @return array|\Illuminate\Http\JsonResponse
- */
- public function updatePrize(Request $request)
- {
- $this->validate($request, [
- 'id' => 'required|integer',
- 'name' => 'max:32',
- 'weixin' => 'max:32',
- 'state' => 'integer',
- ], [
- 'id.*' => '请选择一个',
- 'name.*' => '请填写正确的姓名',
- 'weixin.*' => '请填写正确的联系方式',
- 'state.*' => '请设置领取状态',
- ]);
- $prize = PrizeModel::findOrFail($request->id);
- $prize->fill($request->all());
- $prize->save();
- return response()->json([
- 'code' => 200,
- 'message' => 'OK'
- ]);
- }
- }
|