ShopController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 = 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' => 'required_unless:type,2|min:0',
  46. 'days' => 'required|integer',
  47. ], [
  48. 'traffic.min' => '内含流量不能低于1MB',
  49. 'traffic.max' => '内含流量不能超过10TB',
  50. ]);
  51. if($validator->fails()){
  52. return Redirect::back()->withInput()->withErrors($validator->errors());
  53. }
  54. // 商品LOGO
  55. $logo = null;
  56. if($request->hasFile('logo')){
  57. $logo = $this->uploadFile($request->file('logo'));
  58. if(!$logo){
  59. return Redirect::back()->withInput()->withErrors('LOGO不合法');
  60. }
  61. }
  62. try{
  63. DB::beginTransaction();
  64. $goods = new Goods();
  65. $goods->name = $request->input('name');
  66. $goods->logo = $logo?: null;
  67. $goods->traffic = $request->input('traffic');
  68. $goods->type = $request->input('type');
  69. $goods->price = round($request->input('price'), 2);
  70. $goods->level = $request->input('level');
  71. $goods->renew = round($request->input('renew'), 2);
  72. $goods->period = $request->input('period');
  73. $goods->info = $request->input('info');
  74. $goods->description = $request->input('description');
  75. $goods->days = $request->input('days');
  76. $goods->invite_num = $request->input('invite_num');
  77. $goods->limit_num = $request->input('limit_num');
  78. $goods->color = $request->input('color');
  79. $goods->sort = $request->input('sort');
  80. $goods->is_hot = $request->input('is_hot', 0);
  81. $goods->status = $request->input('status', 0);
  82. $goods->save();
  83. DB::commit();
  84. return Redirect::back()->with('successMsg', '添加成功');
  85. }catch(Exception $e){
  86. DB::rollBack();
  87. Log::info($e);
  88. return Redirect::back()->withInput()->withErrors('添加失败');
  89. }
  90. }else{
  91. $view['levelList'] = Level::query()->orderBy('level')->get();
  92. return Response::view('admin.shop.goodsInfo', $view);
  93. }
  94. }
  95. // 编辑商品
  96. public function editGoods(Request $request) {
  97. $id = $request->input('id');
  98. if($request->isMethod('POST')){
  99. $validator = Validator::make($request->all(), [
  100. 'name' => 'required',
  101. 'traffic' => 'required|integer|min:1024|max:10240000|nullable',
  102. 'price' => 'required|numeric|min:0',
  103. 'type' => 'required',
  104. 'renew' => 'required_unless:type,2|min:0',
  105. 'days' => 'required|integer',
  106. ], [
  107. 'traffic.min' => '内含流量不能低于1MB',
  108. 'traffic.max' => '内含流量不能超过10TB',
  109. ]);
  110. if($validator->fails()){
  111. return Redirect::back()->withInput()->withErrors($validator->errors());
  112. }
  113. $goods = Goods::find($id);
  114. if(!$goods){
  115. Session::flash('errorMsg', '商品不存在');
  116. return Redirect::back();
  117. }
  118. // 商品LOGO
  119. if($request->hasFile('logo')){
  120. $logo = $this->uploadFile($request->file('logo'));
  121. if(!$logo){
  122. Session::flash('errorMsg', 'LOGO不合法');
  123. return Redirect::back()->withInput();
  124. }
  125. Goods::query()->whereId($id)->update(['logo' => $logo]);
  126. }
  127. try{
  128. DB::beginTransaction();
  129. $data = [
  130. 'name' => $request->input('name'),
  131. 'price' => round($request->input('price'), 2) * 100,
  132. 'level' => $request->input('level'),
  133. 'renew' => round($request->input('renew'), 2) * 100,
  134. 'period' => $request->input('period'),
  135. 'info' => $request->input('info'),
  136. 'description' => $request->input('description'),
  137. 'invite_num' => $request->input('invite_num'),
  138. 'limit_num' => $request->input('limit_num'),
  139. 'color' => $request->input('color'),
  140. 'sort' => $request->input('sort'),
  141. 'is_hot' => $request->input('is_hot', 0),
  142. 'status' => $request->input('status', 0)
  143. ];
  144. Goods::query()->whereId($id)->update($data);
  145. Session::flash('successMsg', '编辑成功');
  146. DB::commit();
  147. }catch(Exception $e){
  148. Session::flash('errorMsg', '编辑失败');
  149. DB::rollBack();
  150. }
  151. return Redirect::to('shop/edit?id='.$id);
  152. }
  153. $goods = Goods::find($id);
  154. $view['levelList'] = Level::query()->orderBy('level')->get();
  155. return view('admin.shop.goodsInfo', $view)->with(compact('goods'));
  156. }
  157. // 删除商品
  158. public function delGoods(Request $request): JsonResponse {
  159. try{
  160. Goods::query()->whereId($request->input('id'))->delete();
  161. }catch(Exception $e){
  162. Session::flash('errorMsg', '编辑失败'.$e);
  163. }
  164. return Response::json(['status' => 'success', 'message' => '删除成功']);
  165. }
  166. }