WelfareController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\User\UserModel;
  6. use App\Models\Welfare\LotteryModel;
  7. use App\Models\Welfare\TicketModel;
  8. use App\Models\Welfare\PrizeModel;
  9. use App\Models\Welfare\StoreModel;
  10. class WelfareController extends Controller
  11. {
  12. /**
  13. * 创建抽奖活动
  14. * @param Request $request
  15. * @return array|\Illuminate\Http\JsonResponse
  16. */
  17. public function createLottery(Request $request)
  18. {
  19. $this->validate($request, [
  20. 'title' => 'required|max:32',
  21. 'drawed_at' => 'required|integer',
  22. 'showed_at' => 'required|integer',
  23. 'cost' => 'required|max:16',
  24. 'cover_url' => 'required|url',
  25. 'banner_url' => 'url',
  26. 'count' => 'required|integer',
  27. 'lottery_explain' => 'required',
  28. ], [
  29. 'title.required' => '请输入标题',
  30. 'title.max' => '字数不能超过32个字符',
  31. 'cost.required' => '请输入价格',
  32. 'cost.max' => '字数不能超过16个字符',
  33. 'drawed_at.*' => '请选择开奖时间',
  34. 'showed_at.*' => '清选择显示时间',
  35. 'cover_url.*' => '上传封面',
  36. 'banner_url.*' => '上传长图',
  37. 'count.*' => '请填写数字',
  38. 'lottery_explain.*' => '抽奖说明',
  39. ]);
  40. $lottery = LotteryModel::create($request->all());
  41. return response()->json([
  42. 'code' => 200,
  43. 'message' => 'OK',
  44. 'data' => $lottery
  45. ]);
  46. }
  47. /**
  48. * 修改活动
  49. * @param Request $request
  50. * @return array|\Illuminate\Http\JsonResponse
  51. */
  52. public function updateLottery(Request $request)
  53. {
  54. $this->validate($request, [
  55. 'id' => 'required|integer',
  56. 'title' => 'max:32',
  57. 'cost' => 'max:16',
  58. 'drawed_at' => 'integer',
  59. 'showed_at' => 'integer',
  60. 'cover_url' => 'url',
  61. 'count' => 'integer',
  62. 'deleted_at' => 'integer',
  63. ], [
  64. 'title.*' => '字数不能超过32个字符',
  65. 'cost.*' => '字数不能超过16个字符',
  66. 'drawed_at.*' => '请选择开奖时间',
  67. 'showed_at.*' => '清选择显示时间',
  68. 'cover_url.*' => '上传图片',
  69. 'count.*' => '请填写数字',
  70. 'lottery_explain.*' => '抽奖说明',
  71. 'receive_explain.*' => '兑奖说明',
  72. ]);
  73. $lottery = LotteryModel::findOrFail($request->id);
  74. $lottery->fill($request->all());
  75. $lottery->save();
  76. return response()->json([
  77. 'code' => 200,
  78. 'message' => 'OK',
  79. 'data' => $lottery
  80. ]);
  81. }
  82. /**
  83. * 抽奖活动列表
  84. * @param Request $request
  85. * @return \Illuminate\Http\JsonResponse
  86. */
  87. public function lotterys(Request $request)
  88. {
  89. $per_page = $request->input('per_page', 10);
  90. $page = $request->input('page', 1);
  91. $offset = ($page - 1) * $per_page;
  92. $lotterys = LotteryModel::when($request->filled('draw'), function ($query) use ($request) {
  93. if ($request->draw) {
  94. return $query->where('drawed_at', '<=', time());
  95. } else {
  96. return $query->where('drawed_at', '>', time());
  97. }
  98. });
  99. $count = $lotterys->count();
  100. $list = $lotterys->orderBy('drawed_at', 'desc')->offset($offset)->limit($per_page)->get();
  101. foreach ($list as $lottery) {
  102. $lottery->ticket_count = TicketModel::where('lottery_id', $lottery->id)->count();
  103. $lottery->ticket_number = TicketModel::where('lottery_id', $lottery->id)->distinct()->count('uid');
  104. }
  105. return response()->json([
  106. 'code' => 200,
  107. 'message' => 'OK',
  108. 'data' => [
  109. 'list' => $list,
  110. 'count' => $count,
  111. 'per_page' => $per_page,
  112. 'page' => $page
  113. ]
  114. ]);
  115. }
  116. /**
  117. * 兑换码列表
  118. * @param Request $request
  119. * @return \Illuminate\Http\JsonResponse
  120. */
  121. public function tickets(Request $request)
  122. {
  123. $per_page = $request->input('per_page', 20);
  124. $page = $request->input('page', 1);
  125. $offset = ($page - 1) * $per_page;
  126. $builder = TicketModel::when($request->filled('lottery_id'), function ($query) use ($request) {
  127. return $query->where('lottery_id', $request->lottery_id);
  128. })->when($request->filled('uid'), function ($query) use ($request) {
  129. return $query->where('uid', $request->uid);
  130. })->when($request->filled('ticket_code'), function ($query) use ($request) {
  131. return $query->where('ticket_code', $request->ticket_code);
  132. })->when($request->filled('lucky'), function ($query) use ($request) {
  133. return $query->where('lucky', $request->lucky);
  134. });
  135. $total = $builder->count();
  136. $tickets = $builder->orderBy('id', 'desc')->offset($offset)->limit($per_page)->get();
  137. foreach ($tickets as $ticket) {
  138. $ticket->username = UserModel::select('uid', 'nickname', 'headimgurl')->where('uid', $ticket->uid)->first();
  139. $ticket->lottery = LotteryModel::select('title')->where('id', $ticket->lottery_id)->first();
  140. }
  141. return response()->json([
  142. 'code' => 200,
  143. 'message' => 'OK',
  144. 'data' => [
  145. 'list' => $tickets,
  146. 'count' => $total,
  147. 'per_page' => $per_page,
  148. 'page' => $page
  149. ]
  150. ]);
  151. }
  152. /**
  153. * 修改ticket
  154. * @param Request $request
  155. * @return \Illuminate\Http\JsonResponse
  156. */
  157. public function updateTicket(Request $request)
  158. {
  159. $this->validate($request, [
  160. 'id' => 'required|integer',
  161. 'lucky' => 'boolean',
  162. ], [
  163. 'id.*' => '请选择一个抽奖码',
  164. 'lucky.*' => '请设置是否中奖',
  165. ]);
  166. $ticket = TicketModel::findOrFail($request->id);
  167. $ticket->fill($request->all());
  168. $ticket->save();
  169. return response()->json([
  170. 'code' => '200',
  171. 'message' => 'OK'
  172. ]);
  173. }
  174. /**
  175. * 商品列表
  176. * @param Request $request
  177. * @return \Illuminate\Http\JsonResponse
  178. */
  179. public function stores(Request $request)
  180. {
  181. $per_page = $request->input('per_page', 10);
  182. $page = $request->input('page', 1);
  183. $offset = ($page - 1) * $per_page;
  184. $builder = new StoreModel();
  185. $total = $builder->count();
  186. $lotterys = $builder->orderBy('id', 'desc')->offset($offset)->limit($per_page)->get();
  187. return response()->json([
  188. 'code' => 200,
  189. 'message' => 'OK',
  190. 'data' => [
  191. 'list' => $lotterys,
  192. 'count' => $total,
  193. 'per_page' => $per_page,
  194. 'page' => $page
  195. ]
  196. ]);
  197. }
  198. /**
  199. * 创建商品
  200. * @param Request $request
  201. * @return array|\Illuminate\Http\JsonResponse
  202. */
  203. public function createStore(Request $request)
  204. {
  205. $this->validate($request, [
  206. 'title' => 'required|max:32',
  207. 'cost' => 'required|max:16',
  208. 'cover_url' => 'required|url',
  209. 'price' => 'required|integer',
  210. 'state' => 'boolean',
  211. 'count' => 'required|integer',
  212. 'receive_explain' => 'required',
  213. ], [
  214. 'title.required' => '请输入标题',
  215. 'title.max' => '字数不能超过32个字符',
  216. 'cost.required' => '请输入价格',
  217. 'cost.max' => '字数不能超过16个字符',
  218. 'drawed_at.*' => '请选择开奖时间',
  219. 'cover_url.*' => '上传封面图片',
  220. 'price.*' => '请设置拼图数量',
  221. 'state.*' => '请设置是否上下架',
  222. 'count.*' => '请填写数字',
  223. 'receive_explain.*' => '兑奖说明',
  224. ]);
  225. $store = StoreModel::create($request->all());
  226. return response()->json([
  227. 'code' => 200,
  228. 'message' => 'OK',
  229. 'data' => $store,
  230. ]);
  231. }
  232. /**
  233. * 修改商品
  234. * @param Request $request
  235. * @return array|\Illuminate\Http\JsonResponse
  236. */
  237. public function updateStore(Request $request)
  238. {
  239. $this->validate($request, [
  240. 'id' => 'required|integer',
  241. 'title' => 'max:32',
  242. 'cost' => 'max:16',
  243. 'cover_url' => 'url',
  244. 'price' => 'integer',
  245. 'state' => 'boolean',
  246. 'count' => 'integer',
  247. ], [
  248. 'id.*' => '请选择一个商品',
  249. 'title.max' => '字数不能超过32个字符',
  250. 'cost.max' => '字数不能超过16个字符',
  251. 'drawed_at.*' => '请选择开奖时间',
  252. 'cover_url.*' => '上传封面图片',
  253. 'price.*' => '请设置拼图数量',
  254. 'state.*' => '请设置是否上下架',
  255. 'count.*' => '请填写数字',
  256. ]);
  257. $store = StoreModel::findOrFail($request->id);
  258. $store->fill($request->all());
  259. $store->save();
  260. return response()->json([
  261. 'code' => 200,
  262. 'message' => 'OK',
  263. 'data' => $store,
  264. ]);
  265. }
  266. /**
  267. * 奖品兑换列表
  268. * @param Request $request
  269. * @return \Illuminate\Http\JsonResponse
  270. */
  271. public function prizes(Request $request)
  272. {
  273. $per_page = $request->input('per_page', 20);
  274. $page = $request->input('page', 1);
  275. $offset = ($page - 1) * $per_page;
  276. $builder = PrizeModel::when($request->filled('title'), function ($query) use ($request) {
  277. return $query->where('title', $request->title);
  278. })->when($request->filled('name'), function ($query) use ($request) {
  279. return $query->where('name', $request->name);
  280. })->when($request->filled('uid'), function ($query) use ($request) {
  281. return $query->where('uid', $request->uid);
  282. })->when($request->filled("state"), function ($query) use ($request) {
  283. return $query->where('state', $request->state);
  284. });
  285. $total = $builder->count();
  286. $prizes = $builder->orderBy('id', 'desc')->offset($offset)->limit($per_page)->get();
  287. return response()->json([
  288. 'code' => 200,
  289. 'message' => 'OK',
  290. 'data' => [
  291. 'list' => $prizes,
  292. 'count' => $total,
  293. 'per_page' => $per_page,
  294. 'page' => $page
  295. ]
  296. ]);
  297. }
  298. /**
  299. * 领取奖品
  300. * @param Request $request
  301. * @return array|\Illuminate\Http\JsonResponse
  302. */
  303. public function updatePrize(Request $request)
  304. {
  305. $this->validate($request, [
  306. 'id' => 'required|integer',
  307. 'name' => 'max:32',
  308. 'weixin' => 'max:32',
  309. 'state' => 'integer',
  310. ], [
  311. 'id.*' => '请选择一个',
  312. 'name.*' => '请填写正确的姓名',
  313. 'weixin.*' => '请填写正确的联系方式',
  314. 'state.*' => '请设置领取状态',
  315. ]);
  316. $prize = PrizeModel::findOrFail($request->id);
  317. $prize->fill($request->all());
  318. $prize->save();
  319. return response()->json([
  320. 'code' => 200,
  321. 'message' => 'OK'
  322. ]);
  323. }
  324. }