CustomerLogStatusController.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\CustomerLogStatus;
  4. use App\Services\LogService;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Gate;
  7. use Illuminate\Support\Facades\Validator;
  8. class CustomerLogStatusController extends Controller
  9. {
  10. public function __construct()
  11. {
  12. $this->middleware('auth');
  13. }
  14. public function index()
  15. {
  16. if(!Gate::allows('客户-客户状态-查询')){ return redirect(url('denied')); }
  17. $customerLogStatuses = CustomerLogStatus::query()->paginate();
  18. return view('customer.customerLogStatus.index', compact('customerLogStatuses'));
  19. }
  20. public function get()
  21. {
  22. return ["success"=>true,"data"=>app("CustomerLogStatusService")->getSelection()];
  23. }
  24. public function save(Request $request)
  25. {
  26. if(!Gate::allows('客户-客户状态-录入') || !Gate::allows('客户-客户状态-编辑')){ return ["success"=>false,"data"=>"无权操作"]; }
  27. $errors = $this->validator($request->input(),$request->input("id"))->errors();
  28. if (count($errors) > 0)return ["success"=>true,"data"=>["errors"=>$errors]];
  29. if ($request->input("id")){
  30. app("CustomerLogStatusService")->update(["id"=>$request->input("id")],
  31. ["name"=>$request->input("name"),"description"=>$request->input("description")]);
  32. LogService::log(__METHOD__,"修改客户状态",json_encode($request->input(),JSON_UNESCAPED_UNICODE));
  33. return ["success"=>true];
  34. }
  35. /** @var CustomerLogStatus $model */
  36. $model = app("CustomerLogStatusService")->create(["name"=>$request->input("name"),"description"=>$request->input("description")]);
  37. LogService::log(__METHOD__,"录入客户状态",$model->toJson());
  38. return ["success"=>true,"data"=>$model];
  39. }
  40. private function validator($params, $id)
  41. {
  42. return Validator::make($params,[
  43. "name" => ['max:50','required',$id?"unique:customer_log_statuses,name,$id":'unique:customer_log_statuses,name']
  44. ],[
  45. 'required'=>':attribute 为必填项',
  46. 'max'=>':attribute 最大为50个字符',
  47. 'unique'=>':attribute 已存在',
  48. ],[
  49. "name" => "名称"
  50. ]);
  51. }
  52. public function destroy(Request $request)
  53. {
  54. if(!Gate::allows('客户-客户状态-删除')){ return ["success"=>false,"data"=>"无权操作"]; }
  55. $id = $request->input("id");
  56. if (!$id) return ["success"=>false,"data"=>"非法参数!"];
  57. app("CustomerLogStatusService")->destroy($request->input("id"));
  58. LogService::log(__METHOD__,"删除客户状态",$id);
  59. return ["success"=>true];
  60. }
  61. }