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