ShopController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Goods;
  5. use App\Models\Level;
  6. use DB;
  7. use Exception;
  8. use Illuminate\Http\JsonResponse;
  9. use Illuminate\Http\Request;
  10. use Log;
  11. use Redirect;
  12. use Response;
  13. use Session;
  14. use Validator;
  15. /**
  16. * 商店控制器
  17. *
  18. * Class ShopController
  19. *
  20. * @package App\Http\Controllers\Controller
  21. */
  22. class ShopController extends Controller {
  23. // 商品列表
  24. public function goodsList(Request $request): \Illuminate\Http\Response {
  25. $type = $request->input('type');
  26. $status = $request->input('status');
  27. $query = Goods::query();
  28. if(isset($type)){
  29. $query->whereType($type);
  30. }
  31. if(isset($status)){
  32. $query->whereStatus($status);
  33. }
  34. $view['goodsList'] = $query->orderByDesc('status')->paginate(10)->appends($request->except('page'));
  35. return Response::view('admin.shop.goodsList', $view);
  36. }
  37. // 添加商品
  38. public function addGoods(Request $request) {
  39. if($request->isMethod('POST')){
  40. Validator::make($request->all(), [
  41. 'name' => 'required',
  42. 'traffic' => 'required|integer|min:1024|max:10240000|nullable',
  43. 'price' => 'required|numeric|min:0',
  44. 'type' => 'required',
  45. 'renew' => 'require_unless:type,2|min:0',
  46. 'days' => 'required|integer',
  47. ], [
  48. 'traffic.min' => '内含流量不能低于1MB',
  49. 'traffic.max' => '内含流量不能超过10TB',
  50. ]);
  51. // 商品LOGO
  52. $logo = null;
  53. if($request->hasFile('logo')){
  54. $logo = $this->uploadFile($request->file('logo'));
  55. if(!$logo){
  56. return Redirect::back()->withInput()->withErrors('LOGO不合法');
  57. }
  58. }
  59. try{
  60. DB::beginTransaction();
  61. $goods = new Goods();
  62. $goods->name = $request->input('name');
  63. $goods->logo = $logo?: null;
  64. $goods->traffic = $request->input('traffic');
  65. $goods->type = $request->input('type');
  66. $goods->price = round($request->input('price'), 2);
  67. $goods->level = $request->input('level');
  68. $goods->renew = round($request->input('renew'), 2);
  69. $goods->period = $request->input('period');
  70. $goods->info = $request->input('info');
  71. $goods->description = $request->input('description');
  72. $goods->days = $request->input('days');
  73. $goods->invite_num = $request->input('invite_num');
  74. $goods->limit_num = $request->input('limit_num');
  75. $goods->color = $request->input('color');
  76. $goods->sort = $request->input('sort');
  77. $goods->is_hot = $request->input('is_hot', 0);
  78. $goods->status = $request->input('status', 0);
  79. $goods->save();
  80. DB::commit();
  81. return Redirect::back()->with('successMsg', '添加成功');
  82. }catch(Exception $e){
  83. DB::rollBack();
  84. Log::info($e);
  85. return Redirect::back()->withInput()->withErrors('添加失败');
  86. }
  87. }else{
  88. $view['level_list'] = Level::query()->orderBy('level')->get();
  89. return Response::view('admin.shop.goodsInfo', $view);
  90. }
  91. }
  92. // 编辑商品
  93. public function editGoods(Request $request) {
  94. $id = $request->input('id');
  95. if($request->isMethod('POST')){
  96. Validator::make($request->all(), [
  97. 'name' => 'required',
  98. 'traffic' => 'required|integer|min:1024|max:10240000|nullable',
  99. 'price' => 'required|numeric|min:0',
  100. 'type' => 'required',
  101. 'renew' => 'require_unless:type,2|min:0',
  102. 'days' => 'required|integer',
  103. ], [
  104. 'traffic.min' => '内含流量不能低于1MB',
  105. 'traffic.max' => '内含流量不能超过10TB',
  106. ]);
  107. $goods = Goods::query()->whereId($id)->first();
  108. if(!$goods){
  109. Session::flash('errorMsg', '商品不存在');
  110. return Redirect::back();
  111. }
  112. // 商品LOGO
  113. if($request->hasFile('logo')){
  114. $logo = $this->uploadFile($request->file('logo'));
  115. if(!$logo){
  116. Session::flash('errorMsg', 'LOGO不合法');
  117. return Redirect::back()->withInput();
  118. }
  119. Goods::query()->whereId($id)->update(['logo' => $logo]);
  120. }
  121. try{
  122. DB::beginTransaction();
  123. $data = [
  124. 'name' => $request->input('name'),
  125. 'price' => round($request->input('price'), 2) * 100,
  126. 'level' => $request->input('level'),
  127. 'renew' => round($request->input('renew'), 2) * 100,
  128. 'period' => $request->input('period'),
  129. 'info' => $request->input('info'),
  130. 'description' => $request->input('description'),
  131. 'invite_num' => $request->input('invite_num'),
  132. 'limit_num' => $request->input('limit_num'),
  133. 'color' => $request->input('color'),
  134. 'sort' => $request->input('sort'),
  135. 'is_hot' => $request->input('is_hot', 0),
  136. 'status' => $request->input('status', 0)
  137. ];
  138. Goods::query()->whereId($id)->update($data);
  139. Session::flash('successMsg', '编辑成功');
  140. DB::commit();
  141. }catch(Exception $e){
  142. Session::flash('errorMsg', '编辑失败');
  143. DB::rollBack();
  144. }
  145. return Redirect::to('shop/edit?id='.$id);
  146. }
  147. $goods = Goods::query()->whereId($id)->first();
  148. $view['level_list'] = Level::query()->orderBy('level')->get();
  149. return view('admin.shop.goodsInfo', $view)->with(compact('goods'));
  150. }
  151. // 删除商品
  152. public function delGoods(Request $request): JsonResponse {
  153. try{
  154. Goods::query()->whereId($request->input('id'))->delete();
  155. }catch(Exception $e){
  156. Session::flash('errorMsg', '编辑失败'.$e);
  157. }
  158. return Response::json(['status' => 'success', 'data' => '', 'message' => '删除成功']);
  159. }
  160. }