CustomerBaseController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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('customer.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('customer.customer.create');
  30. }
  31. /**
  32. * Store a newly created resource in storage.
  33. *
  34. * @param Request $request
  35. * @return Response
  36. * @throws
  37. */
  38. public function store(Request $request)
  39. {
  40. if(!Gate::allows('客户-录入')){ return redirect('denied'); }
  41. $this->validator($request->input())->validate();
  42. app('CustomerService')->create([
  43. "code"=>$request->input("code"),
  44. "name"=>$request->input("name"),
  45. "company_name"=>$request->input("company_name"),
  46. "invoice_address"=>$request->input("invoice_address"),
  47. "contact_man"=>$request->input("contact_man"),
  48. "phone"=>$request->input("phone"),
  49. "comment"=>$request->input("remark"),
  50. ]);
  51. LogService::log(__METHOD__,"录入客户",json_encode($request->input(),JSON_UNESCAPED_UNICODE));
  52. return response()->redirectTo("customer/customer")->with("successTip","成功创建客户“".$request->input("name")."”");
  53. }
  54. /**
  55. * Show the form for editing the specified resource.
  56. *
  57. * @param int $id
  58. * @return Response
  59. */
  60. public function edit($id)
  61. {
  62. if(!Gate::allows('客户-编辑')){ return redirect('denied'); }
  63. $customer = app('CustomerService')->find($id);
  64. return response()->view('customer.customer.create',compact("customer"));
  65. }
  66. /**
  67. * Update the specified resource in storage.
  68. *
  69. * @param Request $request
  70. * @param int $id
  71. * @return Response
  72. * @throws
  73. */
  74. public function update(Request $request, $id)
  75. {
  76. if(!Gate::allows('客户-编辑')){ return redirect('denied'); }
  77. $this->validator($request->input(),$id)->validate();
  78. $result = app('CustomerService')->update(["id"=>$id],[
  79. "code"=>$request->input("code"),
  80. "name"=>$request->input("name"),
  81. "company_name"=>$request->input("company_name"),
  82. "invoice_address"=>$request->input("invoice_address"),
  83. "contact_man"=>$request->input("contact_man"),
  84. "phone"=>$request->input("phone"),
  85. "remark"=>$request->input("remark"),
  86. ]);
  87. if ($result == 1){
  88. LogService::log(__METHOD__,"修改客户",json_encode($request->input(),JSON_UNESCAPED_UNICODE));
  89. return response()->redirectTo("customer/customer")->with("successTip","成功修改客户“".$request->input("name")."”的信息");
  90. }
  91. return response()->view("exception.default",["code"=>"509"]);
  92. }
  93. /**
  94. * Remove the specified resource from storage.
  95. *
  96. * @param int $id
  97. * @return array
  98. */
  99. public function destroy($id)
  100. {
  101. if(!Gate::allows('客户-删除')){ return ["success"=>false,"data"=>"无权操作!"]; }
  102. $result = app('CustomerService')->destroy($id);
  103. if ($result == 1){
  104. LogService::log(__METHOD__,"删除客户",$id);
  105. return ["success"=>true];
  106. }
  107. return ["success"=>false,"data"=>"删除了“".$result."”行"];
  108. }
  109. private function validator(array $params, $id = null)
  110. {
  111. return Validator::make($params,[
  112. 'code'=>['required',$id?"unique:customers,code,$id":'unique:customers,code','max:20'],
  113. 'name'=>['required','max:20'],
  114. ],[
  115. 'required'=>':attribute 为必填项',
  116. 'max'=>':attribute 字符过多或输入值过大',
  117. 'unique'=>':attribute 已存在',
  118. ],[
  119. 'code'=>'客户代码',
  120. 'name'=>'客户名称',
  121. ]);
  122. }
  123. }