ContractController.php 4.0 KB

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