家政小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProductController.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. $show = $request->input('show');
  22. if ($id) {
  23. $product = Product::find($id);
  24. $product->status = $status;
  25. $product->show = 0;
  26. } else {
  27. $product = new Product;
  28. $product->status = 0;
  29. $product->collect_num = 0;
  30. $product->sell_num = 0;
  31. }
  32. if ($name && $info && $classify && $color) {
  33. $product->name = $name;
  34. $product->info = $info;
  35. $product->color = $color;
  36. $product->price = $price;
  37. $product->classify = $classify;
  38. $product->show = $show;
  39. $product->save();
  40. return Response()->json([
  41. "status" => 0,
  42. "message" => "保存成功!"
  43. ]);
  44. } else {
  45. return Response()->json([
  46. "status" => -1,
  47. "message" => "信息不能为空!"
  48. ]);
  49. }
  50. }
  51. /**
  52. * 返回给后台列表
  53. * @param Request $request
  54. * @return mixed
  55. */
  56. public function manage_list(Request $request) {
  57. return Product::paginate(20);
  58. }
  59. /**
  60. * 按分类返回给小程序
  61. * @param Request $request
  62. * @return \Illuminate\Http\JsonResponse
  63. */
  64. public function list(Request $request)
  65. {
  66. $products = Product::all()->toArray();
  67. $datas = array_map(function ($product) {
  68. return [
  69. "id" => $product["id"],
  70. "classify" => $product["classify"],
  71. "name" => $product["name"],
  72. "price" => $product["price"],
  73. "photo" => $product["photo"]
  74. ];
  75. }, $products);
  76. $data = [];
  77. foreach ($datas as $d) {
  78. if(!array_key_exists($d["classify"], $data)) {
  79. $data[$d["classify"]] = [];
  80. }
  81. array_push($data[$d["classify"]], $d);
  82. }
  83. return response()->json([
  84. "status" => 0,
  85. "data" => $data
  86. ]);
  87. }
  88. /**
  89. * 每页20进行分页
  90. * @param Request $request
  91. * @return mixed
  92. */
  93. public function home(Request $request)
  94. {
  95. $top = Product::where("show", 1)->orderBy("updated_at", "desc")->limit(3)->get()->toArray();
  96. $tops = array_map(function ($t) {
  97. return [
  98. "id" => $t["id"],
  99. "name" => $t["name"],
  100. "photo" => $t["photo"]
  101. ];
  102. }, $top);
  103. return Response()->json([
  104. "data" => $tops,
  105. "status" => 0,
  106. ]);
  107. }
  108. }