家政小程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ProductController.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Product;
  5. class ProductController extends Controller
  6. {
  7. /**
  8. * 创建活动
  9. * @param Request $request
  10. * @return \Illuminate\Http\JsonResponse
  11. */
  12. public function store(Request $request)
  13. {
  14. $id = $request->input('id');
  15. $name = $request->input('name');
  16. $info = $request->input('info');
  17. $classify = $request->input('classify');
  18. $color = $request->input('color');
  19. $price = $request->input('price');
  20. $status = $request->input('status');
  21. $location = $request->input('location');
  22. if ($id) {
  23. $product = Product::find($id);
  24. $product->status = $status;
  25. } else {
  26. $product = new Product;
  27. $product->status = 0;
  28. $product->collect_num = 0;
  29. $product->sell_num = 0;
  30. }
  31. if ($name && $info && $classify && $color) {
  32. $product->name = $name;
  33. $product->info = $info;
  34. $product->color = $color;
  35. $product->price = $price;
  36. $product->classify = $classify;
  37. $product->location = $location;
  38. $product->save();
  39. return Response()->json([
  40. "status" => 0,
  41. "message" => "保存成功!"
  42. ]);
  43. } else {
  44. return Response()->json([
  45. "status" => -1,
  46. "message" => "信息不能为空!"
  47. ]);
  48. }
  49. }
  50. /**
  51. * 返回给后台列表
  52. * @param Request $request
  53. * @return mixed
  54. */
  55. public function manage_list(Request $request) {
  56. return Product::paginate(20);
  57. }
  58. /**
  59. * 按分类返回给小程序
  60. * @param Request $request
  61. * @return \Illuminate\Http\JsonResponse
  62. */
  63. public function list(Request $request)
  64. {
  65. $products = Product::all()->toArray();
  66. $datas = array_map(function ($product) {
  67. return [
  68. "id" => $product["id"],
  69. "classify" => $product["classify"],
  70. "name" => $product["name"],
  71. "price" => $product["price"],
  72. "photo" => $product["photo"]
  73. ];
  74. }, $products);
  75. $data = [];
  76. foreach ($datas as $d) {
  77. if(!array_key_exists($d["classify"], $data)) {
  78. $data[$d["classify"]] = [];
  79. }
  80. array_push($data[$d["classify"]], $d);
  81. }
  82. return response()->json([
  83. "status" => 0,
  84. "data" => $data
  85. ]);
  86. }
  87. /**
  88. * 每页20进行分页
  89. * @param Request $request
  90. * @return mixed
  91. */
  92. public function home(Request $request)
  93. {
  94. $top = Product::where("location", "top")->limit(3)->get()->toArray();
  95. $bottom = Product::where("location", "bottom")->first();
  96. $tops = array_map(function ($t) {
  97. return [
  98. "id" => $t["id"],
  99. "name" => $t["name"],
  100. "info" => $t["info"],
  101. "photo" => $t["photo"],
  102. "location" => $t["location"]
  103. ];
  104. }, $top);
  105. return Response()->json([
  106. "top" => $tops,
  107. "introduce" => "产品介绍",
  108. "bottom" => [
  109. "id" => $bottom["id"],
  110. "name" => $bottom["name"],
  111. "info" => $bottom["info"],
  112. "photo" => $bottom["photo"],
  113. "location" => $bottom["location"]
  114. ]
  115. ]);
  116. }
  117. }