PrizeController.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Http\Controllers\Welfare;
  3. use App\Http\Controllers\Miniprogram\Auth;
  4. use App\Models\Welfare\PrizeModel;
  5. use Illuminate\Http\Request;
  6. use App\Http\Controllers\Controller;
  7. class PrizeController extends Controller
  8. {
  9. /**
  10. * 奖品列表
  11. * @param Request $request
  12. * @return \Illuminate\Http\JsonResponse
  13. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  14. */
  15. public function prizes(Request $request)
  16. {
  17. $uid = Auth::auth();
  18. $per_page = $request->input('per_page', 20);
  19. $page = $request->input('page', 1);
  20. $offset = ($page - 1) * $per_page;
  21. $prizes = PrizeModel::where('uid', $uid);
  22. $count = $prizes->count();
  23. $list = $prizes->orderBy('created_at', 'desc')->offset($offset)->limit($per_page)->get();
  24. return response()->json([
  25. 'code' => 200,
  26. 'message' => 'OK',
  27. 'data' => [
  28. 'list' => $list
  29. ]
  30. ]);
  31. }
  32. /**
  33. * 填写资料
  34. * @param Request $request
  35. * @return array|\Illuminate\Http\JsonResponse
  36. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  37. */
  38. public function fill(Request $request)
  39. {
  40. $uid = Auth::auth();
  41. $validator = \Validator::make($request->all(), [
  42. 'id' => 'required|integer',
  43. 'name' => 'required',
  44. 'weixin' => 'required'
  45. ], [
  46. 'id.*' => '请选择一个奖品',
  47. 'name.*' => '请输入姓名',
  48. 'weixin.*' => '请输入微信账号,以便于联系你',
  49. ]);
  50. if ($validator->fails()) {
  51. return ['code' => 422, 'message' => $validator->errors()->first()];
  52. }
  53. $prize = PrizeModel::find($request->id);
  54. if (!$prize) {
  55. return ['code' => 404, 'message' => '不存在'];
  56. }
  57. if ($prize->uid != $uid) {
  58. return ['code' => 403, 'message' => '无权限'];
  59. }
  60. $prize->name = $request->name;
  61. $prize->weixin = $request->weixin;
  62. $prize->state = 1;
  63. $prize->save();
  64. return response()->json([
  65. 'code' => 200,
  66. 'message' => 'OK'
  67. ]);
  68. }
  69. }