ContractController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App\Http\Controllers\AdminApi;
  3. use App\Http\Controllers\AdminController;
  4. use App\Models\Contract;
  5. use App\Servers\System\CommonServer;
  6. use Illuminate\Support\Facades\Storage;
  7. class ContractController extends AdminController
  8. {
  9. /**
  10. * 获取项目合同
  11. * @return \Illuminate\Http\JsonResponse
  12. */
  13. function getContractList(){
  14. $where = [['is_del',0]];
  15. $list = Contract::where($where)
  16. ->select(['id','name','created_at'])
  17. ->orderBy('id','desc')
  18. ->paginate(10);
  19. return $this->apiResponseSuccess('获取列表成功', [
  20. 'items' => $list->items(),
  21. 'total' => $list->total(),
  22. 'limit' => 10,
  23. ]);
  24. }
  25. /**
  26. * 添加&编辑项目对应合同
  27. * @return \Illuminate\Http\JsonResponse
  28. * @throws \Throwable
  29. */
  30. function saveContract(){
  31. $id=request()->input('id','');//id
  32. $name=request()->input('name');//合同名称
  33. $sign_party=request()->input('sign_party',[]);//签署区域关键词
  34. $texts=request()->input('texts',[]);//乙方方签署区域关键词
  35. if (empty($name)) return $this->apiResponseError('请填写合同名称');
  36. if (mb_strlen($name) > 100) return $this->apiResponseError('合同名称最大100');
  37. if (empty($sign_party)) return $this->apiResponseError('请填写签署区信息');
  38. $file_url =request()->input('file_url','');//文件
  39. if (empty($file_url)){
  40. return $this->apiResponseError('请上传文件');
  41. }
  42. if (empty($texts)){
  43. return $this->apiResponseError('请填写变量');
  44. }
  45. $wordData = file_get_contents($file_url);
  46. //检查合同名称是否存在
  47. $info = Contract::where('name',$name)->where('is_del',0)->select(['id'])->first();
  48. if (!empty($info)) {
  49. return $this->apiResponseError('合同名称已存在');
  50. }
  51. $pathInfo = pathinfo($file_url);
  52. $extension = $pathInfo['extension'];//获取文件扩展名
  53. $file_name = $name.'.'.$extension;
  54. $file_path = 'storage/contract/'.date('Ymd').'/';
  55. CommonServer::creatServer()->creatDir($file_path);
  56. $path=public_path($file_path);
  57. file_put_contents($path . $file_name,$wordData);
  58. $contracts_file = $file_path.$file_name;
  59. $data = compact('name','contracts_file','sign_party','texts');
  60. if(empty($id)){//不存在id就创建
  61. $res = Contract::create($data);
  62. }else{//存在就更新
  63. $info = Contract::where('id',$id)->first();
  64. if (empty($info)) {
  65. return $this->apiResponseError('没有找到该记录');
  66. }
  67. $res = $info->update($data);
  68. }
  69. if ($res) {
  70. return $this->apiResponseSuccess('操作成功');
  71. } else {
  72. return $this->apiResponseError('操作失败');
  73. }
  74. }
  75. /**
  76. * 获取项目详情
  77. * @return \Illuminate\Http\JsonResponse
  78. */
  79. function getContractInfo()
  80. {
  81. $id = request()->input('id','');//获取需要删除的id
  82. if(empty($id)) return $this->apiResponseError('缺少必要参数');
  83. $info = Contract::where('id',$id)->first();
  84. if (empty($info)) {
  85. return $this->apiResponseError('没有找到该记录');
  86. }
  87. return $this->apiResponseSuccess('成功',$info);
  88. }
  89. /**
  90. * 删除
  91. * @return \Illuminate\Http\JsonResponse
  92. */
  93. function delContract(){
  94. $id = request()->input('id','');//获取需要删除的id
  95. if(empty($id)) return $this->apiResponseError('缺少必要参数');
  96. $info = Contract::where('id',$id)->select(['id'])->first();
  97. if (empty($info)) {
  98. return $this->apiResponseError('没有找到该记录');
  99. }
  100. $res = Contract::where('id',$id)->update(['is_del'=>1]);
  101. if ($res) {
  102. return $this->apiResponseSuccess('删除成功');
  103. } else {
  104. return $this->apiResponseError('删除失败');
  105. }
  106. }
  107. }