ShopController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. if (! $file->storeAs('public', $fileName)) {
  71. return Redirect::back()->withInput()->withErrors('Logo存储失败');
  72. }
  73. return 'upload/'.$fileName;
  74. }
  75. // 编辑商品页面
  76. public function edit(Goods $good)
  77. {
  78. return view('admin.shop.info', [
  79. 'good' => $good,
  80. 'levels' => Level::orderBy('level')->get(),
  81. 'categories' => GoodsCategory::all(),
  82. ]);
  83. }
  84. // 编辑商品
  85. public function update(ShopUpdateRequest $request, Goods $good)
  86. {
  87. $data = $request->validated();
  88. // 商品LOGO
  89. if ($request->hasFile('logo')) {
  90. $path = $this->fileUpload($request->file('logo'));
  91. if (is_string($path)) {
  92. $data['logo'] = $path;
  93. } else {
  94. return $path;
  95. }
  96. }
  97. try {
  98. $data['is_hot'] = array_key_exists('is_hot', $data) ? 1 : 0;
  99. $data['status'] = array_key_exists('status', $data) ? 1 : 0;
  100. if ($good->update($data)) {
  101. return Redirect::back()->with('successMsg', '编辑成功');
  102. }
  103. } catch (Exception $e) {
  104. Log::error('编辑商品信息失败:'.$e->getMessage());
  105. return Redirect::back()->withErrors('编辑商品信息失败:'.$e->getMessage());
  106. }
  107. return Redirect::back()->withInput()->withErrors('编辑失败');
  108. }
  109. // 删除商品
  110. public function destroy(Goods $good): JsonResponse
  111. {
  112. try {
  113. if ($good->delete()) {
  114. return Response::json(['status' => 'success', 'message' => '删除成功']);
  115. }
  116. } catch (Exception $e) {
  117. Log::error('编辑商品失败:'.$e->getMessage());
  118. return Response::json(['status' => 'fail', 'message' => '编辑商品失败:'.$e->getMessage()]);
  119. }
  120. return Response::json(['status' => 'fail', 'message' => '删除失败']);
  121. }
  122. }