ContractController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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','');//文件
  39. if(empty($file_url)){
  40. $file_url =request()->input('file_url','');//文件
  41. }
  42. if (empty($file_url)){
  43. return $this->apiResponseError('请上传文件');
  44. }
  45. if (empty($texts)){
  46. return $this->apiResponseError('请填写变量');
  47. }
  48. $wordData = file_get_contents($file_url);
  49. //检查合同名称是否存在
  50. $info = Contract::where('name',$name)->where('is_del',0)->select(['id'])->first();
  51. if (!empty($info)) {
  52. return $this->apiResponseError('合同名称已存在');
  53. }
  54. $pathInfo = pathinfo($file_url);
  55. $extension = $pathInfo['extension'];//获取文件扩展名
  56. $file_name = $name.'.'.$extension;
  57. $file_path = 'storage/contract/'.date('Ymd').'/';
  58. CommonServer::creatServer()->creatDir($file_path);
  59. $path=public_path($file_path);
  60. file_put_contents($path . $file_name,$wordData);
  61. $contracts_file = $file_path.$file_name;
  62. $data = compact('name','contracts_file','sign_party','texts');
  63. if(empty($id)){//不存在id就创建
  64. $res = Contract::create($data);
  65. }else{//存在就更新
  66. $info = Contract::where('id',$id)->first();
  67. if (empty($info)) {
  68. return $this->apiResponseError('没有找到该记录');
  69. }
  70. $res = $info->update($data);
  71. }
  72. if ($res) {
  73. return $this->apiResponseSuccess('操作成功');
  74. } else {
  75. return $this->apiResponseError('操作失败');
  76. }
  77. }
  78. /**
  79. * 获取项目详情
  80. * @return \Illuminate\Http\JsonResponse
  81. */
  82. function getContractInfo()
  83. {
  84. $id = request()->input('id','');//获取需要删除的id
  85. if(empty($id)) return $this->apiResponseError('缺少必要参数');
  86. $info = Contract::where('id',$id)->first();
  87. if (empty($info)) {
  88. return $this->apiResponseError('没有找到该记录');
  89. }
  90. return $this->apiResponseSuccess('成功',$info);
  91. }
  92. /**
  93. * 删除
  94. * @return \Illuminate\Http\JsonResponse
  95. */
  96. function delContract(){
  97. $id = request()->input('id','');//获取需要删除的id
  98. if(empty($id)) return $this->apiResponseError('缺少必要参数');
  99. $info = Contract::where('id',$id)->select(['id'])->first();
  100. if (empty($info)) {
  101. return $this->apiResponseError('没有找到该记录');
  102. }
  103. $res = Contract::where('id',$id)->update(['is_del'=>1]);
  104. if ($res) {
  105. return $this->apiResponseSuccess('删除成功');
  106. } else {
  107. return $this->apiResponseError('删除失败');
  108. }
  109. }
  110. }