CustomerBaseController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Services\LogService;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Response;
  6. use Illuminate\Support\Facades\Gate;
  7. use Illuminate\Support\Facades\Validator;
  8. class CustomerBaseController extends Controller
  9. {
  10. /**
  11. * Display a listing of the resource.
  12. *
  13. * @return Response
  14. */
  15. public function index()
  16. {
  17. if(!Gate::allows('客户-查询')){ return redirect('denied'); }
  18. $customers = app('CustomerService')->paginate();
  19. return response()->view('maintenance.customer.index',compact("customers"));
  20. }
  21. /**
  22. * Show the form for creating a new resource.
  23. *
  24. * @return Response
  25. */
  26. public function create()
  27. {
  28. if(!Gate::allows('客户-录入')){ return redirect('denied'); }
  29. return response()->view('maintenance.customer.create');
  30. }
  31. /**
  32. * Store a newly created resource in storage.
  33. *
  34. * @param Request $request
  35. * @return Response
  36. */
  37. public function store(Request $request)
  38. {
  39. if(!Gate::allows('客户-录入')){ return redirect('denied'); }
  40. $this->validator($request->input())->validate();
  41. app('CustomerService')->create([
  42. "code"=>$request->input("code"),
  43. "name"=>$request->input("name"),
  44. "company_name"=>$request->input("company_name"),
  45. ]);
  46. LogService::log(__METHOD__,"录入客户",json_encode($request->input(),JSON_UNESCAPED_UNICODE));
  47. return response()->redirectTo("maintenance/customer")->with("successTip","成功创建客户“".$request->input("name")."”");
  48. }
  49. /**
  50. * Show the form for editing the specified resource.
  51. *
  52. * @param int $id
  53. * @return Response
  54. */
  55. public function edit($id)
  56. {
  57. if(!Gate::allows('客户-编辑')){ return redirect('denied'); }
  58. $customer = app('CustomerService')->find($id);
  59. return response()->view('maintenance.customer.create',compact("customer"));
  60. }
  61. /**
  62. * Update the specified resource in storage.
  63. *
  64. * @param Request $request
  65. * @param int $id
  66. * @return Response
  67. */
  68. public function update(Request $request, $id)
  69. {
  70. if(!Gate::allows('客户-编辑')){ return redirect('denied'); }
  71. $this->validator($request->input(),$id)->validate();
  72. $result = app('CustomerService')->update(["id"=>$id],[
  73. "code"=>$request->input("code"),
  74. "name"=>$request->input("name"),
  75. "company_name"=>$request->input("company_name"),
  76. ]);
  77. if ($result == 1){
  78. LogService::log(__METHOD__,"修改客户",json_encode($request->input(),JSON_UNESCAPED_UNICODE));
  79. return response()->redirectTo("maintenance/customer")->with("successTip","成功修改客户“".$request->input("name")."”的信息");
  80. }
  81. return response()->view("exception.default",["code"=>"509"]);
  82. }
  83. /**
  84. * Remove the specified resource from storage.
  85. *
  86. * @param int $id
  87. * @return array
  88. */
  89. public function destroy($id)
  90. {
  91. if(!Gate::allows('客户-删除')){ return ["success"=>false,"data"=>"无权操作!"]; }
  92. $result = app('CustomerService')->destroy($id);
  93. if ($result == 1){
  94. LogService::log(__METHOD__,"删除客户",$id);
  95. return ["success"=>true];
  96. }
  97. return ["success"=>false,"data"=>"删除了“".$result."”行"];
  98. }
  99. private function validator(array $params, $id = null)
  100. {
  101. return Validator::make($params,[
  102. 'code'=>['required',$id?"unique:customers,code,$id":'unique:customers,code','max:20'],
  103. 'name'=>['required','max:20'],
  104. ],[
  105. 'required'=>':attribute 为必填项',
  106. 'max'=>':attribute 字符过多或输入值过大',
  107. 'unique'=>':attribute 已存在',
  108. ],[
  109. 'code'=>'客户代码',
  110. 'name'=>'客户名称',
  111. ]);
  112. }
  113. }