SignContractController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace App\Http\Controllers\AdminApi;
  3. use App\Http\Controllers\AdminController;
  4. use App\Models\Contract;
  5. use App\Models\Firm;
  6. use App\Models\Member;
  7. use App\Models\SignContract;
  8. use App\Models\SignRecord;
  9. use App\Servers\ESign\ESignContractServer;
  10. use App\Servers\Wps\WordServer;
  11. class SignContractController extends AdminController
  12. {
  13. /**
  14. * 获取项目合同
  15. * @return \Illuminate\Http\JsonResponse
  16. */
  17. function getContractList()
  18. {
  19. $search=request()->input('search');
  20. $where = [['is_del',0]];
  21. if(!empty($search)){
  22. $where[] = ['name','like','%'.$search.'%'];
  23. }
  24. $list = Contract::where($where)
  25. ->select(['id','name','texts','sign_party'])
  26. ->paginate(10);
  27. return $this->apiResponseSuccess('获取列表成功', [
  28. 'items' => $list->items(),
  29. 'total' => $list->total(),
  30. 'limit' => 10,
  31. ]);
  32. }
  33. /**
  34. * 获取机构列表
  35. * @return \Illuminate\Http\JsonResponse
  36. */
  37. function getFirmList(){
  38. $search=request()->input('search');
  39. $where = [['is_del',0], ['realnameStatus',1]];
  40. if(!empty($search)){
  41. $where[] = ['firm_name','like','%'.$search.'%'];
  42. }
  43. $list = Firm::where($where)
  44. ->select(['id','firm_name','license_number','legal_name','authType','orgId','realnameStatus','authorizeUserInfo','created_at'])
  45. ->paginate(10);
  46. return $this->apiResponseSuccess('获取列表成功', [
  47. 'items' => $list->items(),
  48. 'total' => $list->total(),
  49. 'limit' => 10,
  50. ]);
  51. }
  52. /**
  53. * 获取用户列表
  54. * @return \Illuminate\Http\JsonResponse
  55. */
  56. function getMemberList(){
  57. $search=request()->input('search');
  58. $where = [['is_del',0], ['realnameStatus',1]];
  59. if(!empty($search)){
  60. $where[] = ['name','like','%'.$search.'%'];
  61. }
  62. $list = Member::where($where)
  63. ->select(['id','name','phone','id_number','authType','psnId','realnameStatus','authorizeUserInfo','created_at'])
  64. ->paginate(10);
  65. return $this->apiResponseSuccess('获取列表成功', [
  66. 'items' => $list->items(),
  67. 'total' => $list->total(),
  68. 'limit' => 10,
  69. ]);
  70. }
  71. /**
  72. * 获取签署列表
  73. * @return \Illuminate\Http\JsonResponse
  74. */
  75. function getSignContractList()
  76. {
  77. $search=request()->input('search');
  78. $where = [['is_del',0]];
  79. if(!empty($search)){
  80. $where[] = ['contracts_no','like','%'.$search.'%'];
  81. }
  82. $list = SignContract::where($where)
  83. ->select(['id','contract_no','status','contract_id','sign_url','created_at'])
  84. ->paginate(10);
  85. foreach ($list as $key => $value) {
  86. $value->{'contract_name'} = Contract::where('id',$value->{'contract_id'})->value('name');
  87. //获取用户签署链接
  88. $psnInfo = SignRecord::where('sign_id',$value->{'id'})->where('authType','PSN')->select(['status','sign_url','id_no'])->first();
  89. if (!empty($psnInfo)){
  90. $psnInfo->{'name'} = Member::where('psnId',$psnInfo->{'id_no'})->value('name');
  91. }else{
  92. $psnInfo = [
  93. 'status' => 1,
  94. 'sign_url' => '',
  95. 'id_no' => '',
  96. 'name' => '',
  97. ];
  98. }
  99. //获取企业签署链接
  100. $orgInfo = SignRecord::where('sign_id',$value->{'id'})->where('authType','ORG')->select(['status','sign_url','id_no'])->first();
  101. if (!empty($orgInfo)){
  102. $orgInfo->{'name'} = Firm::where('orgId',$psnInfo->{'id_no'})->value('name');
  103. }else{
  104. $orgInfo = [
  105. 'status' => 1,
  106. 'sign_url' => '',
  107. 'id_no' => '',
  108. 'name' => '',
  109. ];
  110. }
  111. $value->{'psnInfo'} = $psnInfo;
  112. $value->{'orgInfo'} = $orgInfo;
  113. $value->initializeModel();
  114. }
  115. return $this->apiResponseSuccess('获取列表成功', [
  116. 'items' => $list->items(),
  117. 'total' => $list->total(),
  118. 'limit' => 10,
  119. ]);
  120. }
  121. /**
  122. * 创建合同签署
  123. * @return \Illuminate\Http\JsonResponse
  124. * @throws \PhpOffice\PhpWord\Exception\Exception
  125. */
  126. function createSignContract()
  127. {
  128. $contract_id=request()->input('contract_id','');//合同id
  129. $contract_no=request()->input('contract_no','');//合同编号
  130. $text_json=request()->input('textsJson','');//替换文本json
  131. $sign_json=request()->input('signJson','');//签署区域信息
  132. if(empty($contract_id)){
  133. return $this->apiResponseError('请选择合同');
  134. }
  135. if(empty($text_json)){
  136. return $this->apiResponseError('请填写合同信息');
  137. }
  138. if(empty($sign_json)){
  139. return $this->apiResponseError('请选择签署信息');
  140. }
  141. //获取合同信息
  142. $contract = Contract::where(['id'=>$contract_id])->first();
  143. if (empty($contract)){
  144. return $this->apiResponseError('合同不存在');
  145. }
  146. //替换合同信息
  147. $file_path = $contract->{'contracts_file'};
  148. // 获取所有信息
  149. $pathInfo = pathinfo($file_path);
  150. $file_name = $pathInfo['filename'].$contract_no.'.'.$pathInfo['extension'];
  151. //替换合同信息
  152. $local_file = WordServer::creatServer()->replaceWordText($file_path,$file_name,$text_json);
  153. //上传文件到e签宝并获得文件id
  154. $file_id = ESignContractServer::creatServer()->fileUploadUrl($local_file);
  155. $data = compact('contract_id','contract_no','file_id','text_json','sign_json','local_file');
  156. //存入数据库
  157. $sign = SignContract::create($data);
  158. if (empty($sign)){
  159. return $this->apiResponseError('保存失败');
  160. }
  161. return $this->apiResponseSuccess('保存成功');
  162. }
  163. /**
  164. * 下载合同
  165. * @return \Illuminate\Http\JsonResponse
  166. */
  167. function downloadContract()
  168. {
  169. $contract_id=request()->input('contract_id','');//合同id
  170. if(empty($contract_id)){
  171. return $this->apiResponseError('请选择合同');
  172. }
  173. //获取合同信息
  174. $contract = SignContract::where(['id'=>$contract_id])->first();
  175. if (empty($contract)){
  176. return $this->apiResponseError('合同不存在');
  177. }
  178. if ($contract->{'status'} != 2){
  179. return $this->apiResponseError('合同未签署完毕');
  180. }
  181. //上传文件到e签宝并获得文件id
  182. $result = ESignContractServer::creatServer()->downloadFileUrl($contract->{'flow_id'});
  183. if (empty($result)){
  184. return $this->apiResponseError('获取失败');
  185. }
  186. return $this->apiResponseSuccess('成功',$result);
  187. }
  188. }