JigsawController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace App\Http\Controllers\Welfare;
  3. use App\Models\Welfare\PrizeModel;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. use App\Http\Controllers\Miniprogram\Auth;
  7. use App\Models\User\UserModel;
  8. use App\Models\Welfare\JigsawFlowModel;
  9. use App\Models\Welfare\StoreModel;
  10. class JigsawController extends Controller
  11. {
  12. /**
  13. * 拼图明细
  14. * @param Request $request
  15. * @return \Illuminate\Http\JsonResponse
  16. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  17. */
  18. public function flows(Request $request)
  19. {
  20. $uid = Auth::auth();
  21. $per_page = $request->input('per_page', 20);
  22. $page = $request->input('page', 1);
  23. $offset = ($page - 1) * $per_page;
  24. $flows = JigsawFlowModel::where('uid', $uid);
  25. $count = $flows->count();
  26. $list = $flows->orderBy('id', 'asc')->offset($offset)->limit($per_page)->get();
  27. return response()->json([
  28. 'code' => 200,
  29. 'data' => [
  30. 'list' => $list,
  31. 'count' => $count,
  32. 'per_page' => $per_page,
  33. 'page' => $page
  34. ]
  35. ]);
  36. }
  37. /**
  38. * 商城列表
  39. * @param Request $request
  40. * @return \Illuminate\Http\JsonResponse
  41. */
  42. public function stores(Request $request)
  43. {
  44. $stores = StoreModel::where('state', 1)->orderBy('sort', 'desc')->get();
  45. return response()->json([
  46. 'code' => 200,
  47. 'data' => [
  48. 'list' => $stores,
  49. ]
  50. ]);
  51. }
  52. /**
  53. * 兑换
  54. * @param Request $request
  55. * @return array|\Illuminate\Http\JsonResponse
  56. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  57. */
  58. public function shopping(Request $request)
  59. {
  60. $uid = Auth::auth();
  61. $validator = \Validator::make($request->all(), [
  62. 'id' => 'required|integer',
  63. ], [
  64. 'id.*' => '请选择一个商品',
  65. ]);
  66. if ($validator->fails()) {
  67. return response()->json([
  68. 'code' => 422,
  69. 'message' => $validator->errors()->first(),
  70. 'errors' => $validator->errors()
  71. ]);
  72. }
  73. $user = UserModel::find($uid);
  74. \DB::beginTransaction();
  75. // 判断商品ID是否正确
  76. $goods = StoreModel::find($request->id);
  77. if (!$goods) {
  78. return response()->json([
  79. 'code' => 404,
  80. 'message' => '此商品不存在'
  81. ]);
  82. }
  83. // 判断是否上下架
  84. if ($goods->state == 0) {
  85. return ['code' => 10002, 'message' => '商品已下架'];
  86. }
  87. // 判断用户水晶是否足够
  88. if ($user->jigsaw_amount < $goods->price) {
  89. return ['code' => 10003, 'message' => '拼图不足'];
  90. }
  91. // 判断库存
  92. if ($goods->count <= 0) {
  93. return ['code' => 10004, 'message' => '商品已兑换完'];
  94. }
  95. // $amount = $goods->price;
  96. // while ($amount > 0) {
  97. // $jigasw = JigsawFlowModel::where('uid',$uid)->where('remains_amount','>',0)->orderBy('created_at','asc')->first();
  98. // if ($amount > $jigasw->remains_amount) {
  99. // $jigasw->decrement('remains_amount', $jigasw->remains_amount);
  100. // $amount -= $jigasw->remains_amount;
  101. // }else{
  102. // $jigasw->decrement('remains_amount', $amount);
  103. // $amount -= $amount;
  104. // }
  105. // }
  106. // 加入奖品
  107. $prize = PrizeModel::create([
  108. 'uid' => $uid,
  109. 'title' => $goods->title,
  110. 'cover_url' => $goods->cover_url,
  111. 'cost' => $goods->cost
  112. ]);
  113. // 扣拼图余额
  114. $user->decrement('jigsaw_amount', $goods->price);
  115. // 扣货存
  116. $goods->decrement('count');
  117. \DB::commit();
  118. return response()->json([
  119. 'code' => 200,
  120. 'message' => 'OK',
  121. 'data' => $prize
  122. ]);
  123. }
  124. }