CustomerTagController.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\CustomerTag;
  4. use App\Services\LogService;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Gate;
  7. use Illuminate\Support\Facades\Validator;
  8. class CustomerTagController extends Controller
  9. {
  10. public function index()
  11. {
  12. $tags = CustomerTag::query()->orderByDesc("id")->get();
  13. return view("customer.customer.tag.index",compact("tags"));
  14. }
  15. public function save(Request $request)
  16. {
  17. if(!Gate::allows('客户-客户标签-编辑')){ return ["success"=>false,"data"=>"无权操作!"]; }
  18. $errors = $this->validator($request->input(),$request->input("id"))->errors();
  19. if (count($errors) > 0)return ["success"=>true,"data"=>["errors"=>$errors]];
  20. if ($request->input("id")){
  21. CustomerTag::query()->where("id",$request->input("id"))->update([
  22. "name"=>$request->input("name"),
  23. "explanation"=>$request->input("explanation")
  24. ]);
  25. LogService::log(__METHOD__,"录入客户标签",json_encode($request->input(),JSON_UNESCAPED_UNICODE));
  26. return ["success"=>true];
  27. }
  28. $model = CustomerTag::query()->create([
  29. "name"=>$request->input("name"),
  30. "explanation"=>$request->input("explanation")
  31. ]);
  32. LogService::log(__METHOD__,"录入客户标签",$model->toJson());
  33. return ["success"=>true,"data"=>$model];
  34. }
  35. public function destroy($id)
  36. {
  37. if(!Gate::allows('客户-客户标签-删除')){ return ["success"=>false,"data"=>"无权操作!"]; }
  38. CustomerTag::destroy($id);
  39. LogService::log(__METHOD__,"删除客户标签",$id);
  40. return ["success"=>true];
  41. }
  42. private function validator($params, $id)
  43. {
  44. return Validator::make($params,[
  45. "name" => ['max:50','required',$id?"unique:customer_tags,name,$id":'unique:customer_tags,name']
  46. ],[
  47. 'required'=>':attribute 为必填项',
  48. 'max'=>':attribute 最大为50个字符',
  49. 'unique'=>':attribute 已存在',
  50. ],[
  51. "name" => "标签名"
  52. ]);
  53. }
  54. public function get()
  55. {
  56. return ["success"=>true,"data"=>CustomerTag::query()->get(["id","name"])];
  57. }
  58. }