CustomerBaseController.php 4.6 KB

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