ShopController.php 4.4 KB

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