ShopController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Admin\ShopStoreRequest;
  5. use App\Http\Requests\Admin\ShopUpdateRequest;
  6. use App\Models\Goods;
  7. use App\Models\GoodsCategory;
  8. use App\Models\Level;
  9. use Arr;
  10. use Exception;
  11. use Illuminate\Http\JsonResponse;
  12. use Illuminate\Http\RedirectResponse;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Http\UploadedFile;
  15. use Log;
  16. use Redirect;
  17. use Response;
  18. use Str;
  19. class ShopController extends Controller
  20. {
  21. // 商品列表
  22. public function index(Request $request)
  23. {
  24. $query = Goods::query();
  25. foreach (['type', 'status'] as $field) {
  26. $request->whenFilled($field, function ($value) use ($query, $field) {
  27. $query->where($field, $value);
  28. });
  29. }
  30. return view('admin.shop.index', ['goodsList' => $query->orderByDesc('status')->paginate(10)->appends($request->except('page'))]);
  31. }
  32. // 添加商品页面
  33. public function create()
  34. {
  35. return view('admin.shop.info', ['levels' => Level::orderBy('level')->get(), 'categories' => GoodsCategory::all()]);
  36. }
  37. // 添加商品
  38. public function store(ShopStoreRequest $request): RedirectResponse
  39. {
  40. $data = $request->validated();
  41. if (array_key_exists('traffic_unit', $data)) {
  42. $data['traffic'] *= $data['traffic_unit'];
  43. Arr::forget($data, 'traffic_unit');
  44. }
  45. $data['is_hot'] = array_key_exists('is_hot', $data) ? 1 : 0;
  46. $data['status'] = array_key_exists('status', $data) ? 1 : 0;
  47. // 商品LOGO
  48. if ($request->hasFile('logo')) {
  49. $path = $this->fileUpload($request->file('logo'));
  50. if (is_string($path)) {
  51. $data['logo'] = $path;
  52. } else {
  53. return $path;
  54. }
  55. }
  56. try {
  57. if ($good = Goods::create($data)) {
  58. return Redirect::route('admin.goods.edit', $good)->with('successMsg', '添加成功');
  59. }
  60. } catch (Exception $e) {
  61. Log::error('添加商品信息异常:'.$e->getMessage());
  62. return Redirect::back()->withInput()->withErrors('添加商品信息失败:'.$e->getMessage());
  63. }
  64. return Redirect::back()->withInput()->withErrors('添加商品信息失败');
  65. }
  66. // 图片上传
  67. public function fileUpload(UploadedFile $file)
  68. {
  69. $fileName = Str::random(8).time().'.'.$file->getClientOriginalExtension();
  70. $path = $file->storeAs('public', $fileName);
  71. if (! $path) {
  72. return Redirect::back()->withInput()->withErrors('Logo存储失败');
  73. }
  74. return 'upload/'.$fileName;
  75. }
  76. // 编辑商品页面
  77. public function edit(Goods $good)
  78. {
  79. return view('admin.shop.info', [
  80. 'good' => $good,
  81. 'levels' => Level::orderBy('level')->get(),
  82. 'categories' => GoodsCategory::all(),
  83. ]);
  84. }
  85. // 编辑商品
  86. public function update(ShopUpdateRequest $request, Goods $good)
  87. {
  88. $data = $request->validated();
  89. // 商品LOGO
  90. if ($request->hasFile('logo')) {
  91. $path = $this->fileUpload($request->file('logo'));
  92. if (is_string($path)) {
  93. $data['logo'] = $path;
  94. } else {
  95. return $path;
  96. }
  97. }
  98. try {
  99. $data['is_hot'] = array_key_exists('is_hot', $data) ? 1 : 0;
  100. $data['status'] = array_key_exists('status', $data) ? 1 : 0;
  101. if ($good->update($data)) {
  102. return Redirect::back()->with('successMsg', '编辑成功');
  103. }
  104. } catch (Exception $e) {
  105. Log::error('编辑商品信息失败:'.$e->getMessage());
  106. return Redirect::back()->withErrors('编辑商品信息失败:'.$e->getMessage());
  107. }
  108. return Redirect::back()->withInput()->withErrors('编辑失败');
  109. }
  110. // 删除商品
  111. public function destroy(Goods $good): JsonResponse
  112. {
  113. try {
  114. if ($good->delete()) {
  115. return Response::json(['status' => 'success', 'message' => '删除成功']);
  116. }
  117. } catch (Exception $e) {
  118. Log::error('编辑商品失败:'.$e->getMessage());
  119. return Response::json(['status' => 'fail', 'message' => '编辑商品失败:'.$e->getMessage()]);
  120. }
  121. return Response::json(['status' => 'fail', 'message' => '删除失败']);
  122. }
  123. }