SignController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\ApiController;
  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 SignController extends ApiController
  12. {
  13. /**
  14. * 创建合同签署
  15. * @return \Illuminate\Http\JsonResponse
  16. * @throws \PhpOffice\PhpWord\Exception\Exception
  17. */
  18. function createSignContract()
  19. {
  20. $contract_id=9;
  21. $id_number=request()->input('id_number','');//身份证号
  22. $license_number = request()->input('license_number','');//营业执照编号
  23. $contract_no=request()->input('order_no','');//合同编号为订单编号
  24. $project_name = request()->input('project_name','');//项目名称
  25. $total_money = request()->input('total_money','');//项目总资产
  26. $liability = request()->input('liability','');//负债率
  27. $turnover = request()->input('turnover','');//月均营业额
  28. $profit_rate = request()->input('profit_rate','');//年利润率
  29. $year = request()->input('year','');//消费押金年限
  30. $order_money = request()->input('order_money','');//消费金额
  31. $start_year = request()->input('start_year','');//合作开始年
  32. $start_month = request()->input('start_month','');//合作开始月
  33. $start_day = request()->input('start_day','');//合作开始日
  34. $end_year = request()->input('end_year','');//合作结束始年
  35. $end_month = request()->input('end_month','');//合作结束月
  36. $end_day = request()->input('end_day','');//合作结束日
  37. if(empty($id_number)){
  38. return $this->apiResponseError('请填写身份证号');
  39. }
  40. if(empty($license_number)){
  41. return $this->apiResponseError('请输入营业执照编号');
  42. }
  43. if(empty($project_name)){
  44. return $this->apiResponseError('请填写项目名称');
  45. }
  46. if(empty($total_money)){
  47. return $this->apiResponseError('请输入项目总资产');
  48. }
  49. if(empty($liability)){
  50. return $this->apiResponseError('请输入项目负债率');
  51. }
  52. if(empty($turnover)){
  53. return $this->apiResponseError('请输入月均营业额');
  54. }
  55. if(empty($profit_rate)){
  56. return $this->apiResponseError('请输入年利润率');
  57. }
  58. if(empty($year)){
  59. return $this->apiResponseError('请输入合作年限');
  60. }
  61. if(empty($order_money)){
  62. return $this->apiResponseError('请输入消费金额');
  63. }
  64. if(empty($start_year) || empty($start_month) || empty($start_day) || empty($end_year) || empty($end_month) || empty($end_day)){
  65. return $this->apiResponseError('请输入合作期限日期');
  66. }
  67. //获取企业信息
  68. $firm = Firm::where('license_number',$license_number)->select(['firm_name','license_number','realnameStatus','registered_address','phone','legal_name','orgId'])->first();
  69. if (empty($firm)) {
  70. return $this->apiResponseError('该企业不存在');
  71. }
  72. //获取用户信息
  73. $member = Member::where('id_number',$id_number)->where('is_del',0)->select(['id_number','realnameStatus','phone','name','psnId'])->first();
  74. if (empty($member)){
  75. return $this->apiResponseError('该用户不存在');
  76. }
  77. //合同替换内容
  78. $text_data['jName'] = $firm->firm_name;
  79. $text_data['yName'] = $member->name;
  80. $text_data['projectName'] = $project_name;
  81. $text_data['totalMoney'] = $total_money;
  82. $text_data['liability'] = $liability;
  83. $text_data['turnover'] = $turnover;
  84. $text_data['profitRate'] = $profit_rate;
  85. $text_data['year'] = $year;
  86. $text_data['orderMoney'] = $order_money;
  87. $text_data['startYear'] = $start_year;
  88. $text_data['startMonth'] = $start_month;
  89. $text_data['startDay'] = $start_day;
  90. $text_data['endYear'] = $end_year;
  91. $text_data['endMonth'] = $end_month;
  92. $text_data['endDay'] = $end_day;
  93. $text_data['jLegalName'] = $firm->legal_name;
  94. $text_data['jPhone'] = $firm->phone;
  95. $text_data['jAddress'] = $firm->registered_address;
  96. $text_data['yIdNumber'] = $member->id_number;
  97. $text_data['yPhone'] = $member->phone;
  98. // $text_data['yaddress'] = '暂无';
  99. $text_json = json_encode($text_data);
  100. //签章区域内容
  101. $sign_data = [
  102. [
  103. 'keyword' => '甲方(签字/盖章)',
  104. 'auth_type' => 'ORG',
  105. 'id_no' => $firm->orgId,
  106. ],
  107. [
  108. 'keyword' => '乙方(签字/盖章)',
  109. 'auth_type' => 'PSN',
  110. 'id_no' => $member->psnId,
  111. ],
  112. ];
  113. $sign_json = json_encode($sign_data);
  114. //获取合同信息
  115. $contract = Contract::where(['id'=>$contract_id])->first();
  116. if (empty($contract)){
  117. return $this->apiResponseError('合同不存在');
  118. }
  119. //替换合同信息
  120. $file_path = $contract->{'contracts_file'};
  121. // 获取所有信息
  122. $pathInfo = pathinfo($file_path);
  123. $file_name = $pathInfo['filename'].$contract_no.'.'.$pathInfo['extension'];
  124. //替换合同信息
  125. $local_file = WordServer::creatServer()->replaceWordText($file_path,$file_name,$text_json);
  126. //上传文件到e签宝并获得文件id
  127. $file_id = ESignContractServer::creatServer()->fileUploadUrl($local_file);
  128. $data = compact('contract_id','contract_no','file_id','text_json','sign_json','local_file');
  129. //存入数据库
  130. $sign = SignContract::create($data);
  131. if (empty($sign)){
  132. return $this->apiResponseError('保存失败');
  133. }
  134. $sign_id = $sign->id;
  135. return $this->apiResponseSuccess('保存成功',['sign_id'=>$sign_id]);
  136. }
  137. /**
  138. * 获取签署记录
  139. * @return \Illuminate\Http\JsonResponse
  140. */
  141. function getSignRecord()
  142. {
  143. $sign_id=request()->input('sign_id','');//合同id
  144. if(empty($sign_id)){
  145. return $this->apiResponseError('请选择签订记录');
  146. }
  147. $list = SignRecord::where('sign_id',$sign_id)
  148. ->where('is_del',0)
  149. ->select(['authType','status'])
  150. ->get();
  151. return $this->apiResponseSuccess('成功',$list);
  152. }
  153. /**
  154. * 下载合同
  155. * @return \Illuminate\Http\JsonResponse
  156. */
  157. function downloadContract()
  158. {
  159. $sign_id=request()->input('sign_id','');//合同id
  160. if(empty($sign_id)){
  161. return $this->apiResponseError('请选择签订记录');
  162. }
  163. //获取合同信息
  164. $contract = SignContract::where(['id'=>$sign_id])->first();
  165. if (empty($contract)){
  166. return $this->apiResponseError('合同不存在');
  167. }
  168. if ($contract->{'status'} != 2){
  169. return $this->apiResponseError('合同未签署完毕');
  170. }
  171. //上传文件到e签宝并获得文件id
  172. $result = ESignContractServer::creatServer()->downloadFileUrl($contract->{'flow_id'});
  173. if (empty($result)){
  174. return $this->apiResponseError('获取失败');
  175. }
  176. return $this->apiResponseSuccess('成功',$result);
  177. }
  178. }