GoodsController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\Goods;
  4. use Illuminate\Http\Request;
  5. use App\Http\Requests;
  6. use Illuminate\Support\Facades\Config;
  7. class GoodsController extends AdminController
  8. {
  9. /*权限验证规则*/
  10. protected $validateRules = [
  11. 'name' => 'required|max:128',
  12. 'description' => 'sometimes|max:65535',
  13. 'coins' => 'required|integer',
  14. 'remnants' => 'required|integer',
  15. ];
  16. /**
  17. * Display a listing of the resource.
  18. *
  19. * @return \Illuminate\Http\Response
  20. */
  21. public function index(Request $request)
  22. {
  23. $word = $request->input("word",'');
  24. $goods = Goods::where('name','like',"%$word%")->orderBy('updated_at','DESC')->paginate(Config::get('tipask.admin.page_size'));
  25. return view('admin.goods.index')->with('goods',$goods)->with('word',$word);
  26. }
  27. /**
  28. * Show the form for creating a new resource.
  29. *
  30. * @return \Illuminate\Http\Response
  31. */
  32. public function create()
  33. {
  34. return view('admin.goods.create');
  35. }
  36. /**
  37. * Store a newly created resource in storage.
  38. *
  39. * @param \Illuminate\Http\Request $request
  40. * @return \Illuminate\Http\Response
  41. */
  42. public function store(Request $request)
  43. {
  44. $request->flash();
  45. $this->validate($request,$this->validateRules);
  46. $goods = Goods::create($request->all());
  47. if($request->hasFile('logo')){
  48. $savePath = storage_path('app/goods/'.gmdate('ym'));
  49. $file = $request->file('logo');
  50. $fileName = uniqid(str_random(8)).'.'.$file->getClientOriginalExtension();
  51. $target = $file->move($savePath,$fileName);
  52. if($target){
  53. $goods->logo = 'goods-'.gmdate('ym').'-'.$fileName;
  54. $goods->save();
  55. }
  56. }
  57. return $this->success(route('admin.goods.index'),'商品添加成功');
  58. }
  59. /**
  60. * Show the form for editing the specified resource.
  61. *
  62. * @param int $id
  63. * @return \Illuminate\Http\Response
  64. */
  65. public function edit($id)
  66. {
  67. $goods = Goods::find($id);
  68. if(!$goods){
  69. return $this->error(route('admin.goods.index'),'商品不存在,请核实');
  70. }
  71. return view('admin.goods.edit')->with('goods',$goods);
  72. }
  73. /**
  74. * Update the specified resource in storage.
  75. *
  76. * @param \Illuminate\Http\Request $request
  77. * @param int $id
  78. * @return \Illuminate\Http\Response
  79. */
  80. public function update(Request $request, $id)
  81. {
  82. $goods = Goods::find($id);
  83. if(!$goods){
  84. return $this->error(route('admin.goods.index'),'商品不存在,请核实');
  85. }
  86. $this->validate($request,$this->validateRules);
  87. $goods->name = $request->input('name');
  88. $goods->post_type = $request->input('post_type');
  89. $goods->remnants = $request->input('remnants');
  90. $goods->coins = $request->input('coins');
  91. $goods->description = $request->input('description');
  92. $goods->category_id = $request->input('category_id',0);
  93. $goods->status = $request->input('status');
  94. if($request->hasFile('logo')){
  95. $savePath = storage_path('app/goods/'.gmdate('ym'));
  96. $file = $request->file('logo');
  97. $fileName = uniqid(str_random(8)).'.'.$file->getClientOriginalExtension();
  98. $target = $file->move($savePath,$fileName);
  99. if($target){
  100. $goods->logo = 'goods-'.gmdate('ym').'-'.$fileName;
  101. }
  102. }
  103. $goods->save();
  104. return $this->success(route('admin.goods.index'),'商品修改成功');
  105. }
  106. /**
  107. * 删除商品
  108. * @return \Illuminate\Http\Response
  109. */
  110. public function destroy(Request $request)
  111. {
  112. Goods::destroy($request->input('ids'));
  113. return $this->success(route('admin.goods.index'),'商品删除成功');
  114. }
  115. }