Files
liche/api/controllers/wxapp/liche/Contract.php
T
2021-10-27 11:03:43 +08:00

335 lines
15 KiB
PHP

<?php
defined('WXAPP_APP') OR exit('No direct script access allowed');
/**
* Created by Vim
* User: lcc
* Date: 2021/07/12
* Time: 16:08
*/
require_once APPPATH.'controllers/wxapp/Wxapp.php';
class Contract extends Wxapp{
function __construct($inputs, $app_key){
parent::__construct($inputs, $app_key);
$this->login_white = array();//登录白名单
$this->check_status = array();//用户状态校验
$this->check_mobile = array();//需要手机号
$this->check_headimg =array();//授权微信信息
$this->load->model('receiver/order/receiver_orders_model','orders_model');
$this->load->model('receiver/order/receiver_order_signs_model','signs_model');
$this->load->model('receiver/order/receiver_order_contracts_model','contracts_model');
$this->load->model('receiver/order/receiver_order_ckcars_model','ckcars_model');
$this->load->model('receiver/order/receiver_order_bills_model','bills_model');
$this->load->model('receiver/order/receiver_order_deliverys_model','deliverys_model');
$this->load->model('receiver/order/receiver_order_oplogs_model');
$this->load->model('receiver/receiver_service_package_model','package_model');
$this->load->model("biz/biz_model");
$this->load->model("sys/sys_company_model");
$this->load->library('receiver/orders_entity');
}
protected function get(){
$id = $this->input_param('id');
$type = $this->input_param('type');
!strlen($type) && $type = 0;
$row = $this->orders_model->get(['id'=>$id,'mobile'=>$this->session['mobile']]);
$type_arr = $this->contracts_model->get_types();
if(!$row || !$type_arr[$type]){
throw new Exception('参数错误', API_CODE_INVILD_PARAM);
}
$contract = $this->contracts_model->get(['type'=>$type,'o_id'=>$id]);
$this->load->library('pdf');
list($h5_url,$title) = $this->orders_entity->get_contract_h5($id,$type,1);
list($pdf2img_url,$title) = $this->orders_entity->get_contract_h5($id,$type);
if(!$contract){
$add_data = [
'o_id' => $id,
'type' => $type,
'c_time' => time()
];
$con_id = $this->contracts_model->add($add_data);
if(!$con_id){
throw new Exception('创建合同失败', API_CODE_INVILD_PARAM);
}
$cid = create_contract_no(350200,$con_id,$type);
$this->contracts_model->update(['cid'=>$cid],['id'=>$con_id]);
//html转pdf
$save_path = 'data/contracts/'.date('Ymd');
$filename = $cid.'.pdf';
$c_res = $this->pdf->html2pdf($pdf2img_url,FCPATH.$save_path,$filename);
$update = [
'file' => $save_path.'/'.$filename,
'cid' => $cid
];
$this->contracts_model->update($update,['id'=>$con_id]);
}else{
if(!$contract['status'] && date('Ymd')!=date('Ymd',$contract['c_time'])){ //更新合同文件日期
//html转pdf
@unlink(FCPATH.$contract['file']);
$save_path = 'data/contracts/'.date('Ymd');
$filename = $contract['cid'].'.pdf';
$c_res = $this->pdf->html2pdf($h5_url,FCPATH.$save_path,$filename);
$update = [
'file' => $save_path.'/'.$filename,
'c_time' => time()
];
$this->contracts_model->update($update,['id'=>$contract['id']]);
}
}
$next_path = ''; //下一步合同
switch($type){
case 0: //整车
$next_path = '/pages/mine/signContract/daiLi?id='.$row['id'];
break;
case 1: //协议
break;
case 4://车辆买卖合同补充协议
$opt_row = $this->receiver_order_oplogs_model->select(['order_id'=>$row['id'],'type'=>3],'id desc',1,1);//最后修改日志
$opt_row = json_decode($opt_row[0]['log_json'],true);
if($opt_row['pack_id']!=$row['pack_id']){
//需要签服务补充协议
$next_path = '/pages/mine/signContract/weiTuoBu?id='.$row['id'];
}
break;
case 5://委托服务补充协议
break;
default:
}
$data = [
'title' => $title,
'url' => $h5_url,
'img' => 'https://qs.haodian.cn/wechat_app/liche/mine/he-demo-2.jpg',
'netx_path' => $next_path
];
return $data;
}
protected function post(){
$uid = $this->session['uid'];
$id = $this->input_param('id');
$type = $this->input_param('type');
$img = $this->input_param('img');
$row = $this->contracts_model->get(['type'=>$type,'o_id'=>$id]);
$order = $this->orders_model->get(['id'=>$id],'brand_id,biz_id,ifentrust,pack_id,info_json');
if(!$row['file'] || !$img){
throw new Exception('参数错误', API_CODE_INVILD_PARAM);
}
$jsondata = json_decode($order['info_json'],true);
if($row['status']==1){
throw new Exception('已签名', API_CODE_INVILD_PARAM);
}
//获取公司印章
if($type==1 || $type==5){
$company = $this->sys_company_model->get(["title like '%服务%'"=>null,'status'=>1],'img_seal,id');
}else{
$biz = $this->biz_model->get(['id'=>$order['biz_id']],'company_id');
if($order['brand_id']==4){ //哪吒品牌 公司固定厦门狸车销售公司
$biz['company_id'] = 1;
}
$company = $this->sys_company_model->get(['id'=>$biz['company_id']],'img_seal,id');
}
$img_seal = $company['img_seal'] ? build_qiniu_image_url($company['img_seal']) : '';
if(!$img_seal){
throw new Exception('公司公章未上传', API_CODE_FAIL);
}
//pdf转图片
$pdf_url = http_host_com('api').'/'.$row['file'];
$this->load->library('pdf');
$pdf = new Pdf(); //正式
$imgs = $pdf->pdf2img($pdf_url);
if(!$imgs){
throw new Exception('签名失败,图片转换失败', API_CODE_INVILD_PARAM);
}
$redis = load_cache('redis');
$cache_key = "CONTRACT_{$id}_{$uid}_{$type}";
if($redis->get($cache_key)){
throw new Exception('请求太频繁,请刷新查看结果', API_CODE_INVILD_PARAM);
}
$redis->save($cache_key,1,60*1);
$this->load->library('receiver/sign_entity');
$sign_entity = new Sign_entity(['comp_img'=>$img_seal]);
switch($type){
case 0: //整车
$sign_img = array_pop($imgs);
$height = $order['ifentrust'] ? 480 : 380;
$jsondata['c_cardid'] && $height += 30;
$res = $sign_entity->merge($sign_img,$img,1070,$height,230);
if(!$res){
throw new Exception('签名失败', API_CODE_INVILD_PARAM);
}
$imgs[] = http_host_com('api').'/'.$res;
$imgs = $this->up_qiniu($imgs,$res);
$update = [
'imgs' => json_encode($imgs,JSON_UNESCAPED_UNICODE),
'sign_time' => date('Y-m-d H:i:s'),
'status' => 1
];
$result = $this->contracts_model->update($update,['id'=>$row['id']]);
if($result){
$redis->delete($cache_key);
throw new Exception('签名成功', API_CODE_SUCCESS);
}else{
throw new Exception('签名失败', API_CODE_INVILD_PARAM);
}
break;
case 1: //协议
$sign_img = array_pop($imgs);
$package = $this->package_model->get(['id'=>$order['pack_id']],'srv_ids');
$srv_count = $package ? count(explode(',',$package['srv_ids'])) : 0;
$height = $order['ifentrust'] ? 1400 : 1280;
$jsondata['c_cardid'] && $height += 30;
$height += $srv_count*52;
$res = $sign_entity->merge($sign_img,$img,1050,$height,300);
if(!$res){
throw new Exception('签名失败', API_CODE_INVILD_PARAM);
}
$imgs[] = http_host_com('api').'/'.$res;
$imgs = $this->up_qiniu($imgs,$res);
$update = [
'imgs' => json_encode($imgs,JSON_UNESCAPED_UNICODE),
'sign_time' => date('Y-m-d H:i:s'),
'status' => 1
];
$result = $this->contracts_model->update($update,['id'=>$row['id']]);
if($result){
$this->orders_entity->sign_after_v2($id,$this->session);
$redis->delete($cache_key);
throw new Exception('签名成功', API_CODE_SUCCESS);
}else{
throw new Exception('签名失败', API_CODE_INVILD_PARAM);
}
break;
case 2: //确认信息
$sign_img = array_pop($imgs);
$height = $order['ifentrust'] ? 600 : 480;
$jsondata['c_cardid'] && $height += 30;
$res = $sign_entity->merge($sign_img,$img,1050,$height,300);
if(!$res){
throw new Exception('签名失败', API_CODE_INVILD_PARAM);
}
$imgs[] = http_host_com('api').'/'.$res;
$imgs = $this->up_qiniu($imgs,$res);
$update = [
'imgs' => json_encode($imgs,JSON_UNESCAPED_UNICODE),
'sign_time' => date('Y-m-d H:i:s'),
'status' => 1
];
$result = $this->contracts_model->update($update,['id'=>$row['id']]);
if($result){
$this->ckcars_model->update(['status'=>2],['o_id'=>$id]);
//生成支付订单
$this->orders_entity->check_finish_v2($id,$this->session);
$redis->delete($cache_key);
throw new Exception('签名成功', API_CODE_SUCCESS);
}else{
throw new Exception('签名失败', API_CODE_INVILD_PARAM);
}
break;
case 3: //车辆交接
$sign_img = array_pop($imgs);
$height = $order['ifentrust'] ? 1470 : 1350;
$res = $sign_entity->merge($sign_img,$img,1050,$height,350);
if(!$res){
throw new Exception('签名失败', API_CODE_INVILD_PARAM);
}
$imgs[] = http_host_com('api').'/'.$res;
$imgs = $this->up_qiniu($imgs,$res);
$update = [
'imgs' => json_encode($imgs,JSON_UNESCAPED_UNICODE),
'sign_time' => date('Y-m-d H:i:s'),
'status' => 1
];
$result = $this->contracts_model->update($update,['id'=>$row['id']]);
if($result){
$this->deliverys_model->update(['status'=>1],['o_id'=>$id]);
$redis->delete($cache_key);
throw new Exception('签名成功', API_CODE_SUCCESS);
}else{
throw new Exception('签名失败', API_CODE_INVILD_PARAM);
}
break;
case 4://车辆买卖合同补充协议
$sign_img = array_pop($imgs);
$height = $order['ifentrust'] ? 1470 : 1350;
$res = $sign_entity->merge($sign_img,$img,1050,$height,350);
if(!$res){
throw new Exception('签名失败', API_CODE_INVILD_PARAM);
}
$imgs[] = http_host_com('api').'/'.$res;
$imgs = $this->up_qiniu($imgs,$res);
$update = [
'imgs' => json_encode($imgs,JSON_UNESCAPED_UNICODE),
'sign_time' => date('Y-m-d H:i:s'),
'status' => 1
];
$result = $this->contracts_model->update($update,['id'=>$row['id']]);
if($result){
$redis->delete($cache_key);
$opt_row = $this->receiver_order_oplogs_model->select(['order_id'=>$id,'type'=>3],'id desc',1,1);//最后修改日志
$opt_row = json_decode($opt_row[0]['log_json'],true);
if($opt_row['pack_id']==$order['pack_id']){
//不需要签服务补充协议 完成重新补充协议签约
$this->orders_entity->finish_sp($id);
}
throw new Exception('签名成功', API_CODE_SUCCESS);
}else{
throw new Exception('签名失败', API_CODE_INVILD_PARAM);
}
break;
case 5://委托服务补充协议
$sign_img = array_pop($imgs);
$height = $order['ifentrust'] ? 1270 : 1150;
$res = $sign_entity->merge($sign_img,$img,1050,$height,340);
if(!$res){
throw new Exception('签名失败', API_CODE_INVILD_PARAM);
}
$imgs[] = http_host_com('api').'/'.$res;
$imgs = $this->up_qiniu($imgs,$res);
$update = [
'imgs' => json_encode($imgs,JSON_UNESCAPED_UNICODE),
'sign_time' => date('Y-m-d H:i:s'),
'status' => 1
];
$result = $this->contracts_model->update($update,['id'=>$row['id']]);
if($result){
$redis->delete($cache_key);
$this->orders_entity->finish_sp($id);
throw new Exception('签名成功', API_CODE_SUCCESS);
}else{
throw new Exception('签名失败', API_CODE_INVILD_PARAM);
}
break;
default:
$redis->delete($cache_key);
throw new Exception('未知签名类型', API_CODE_INVILD_PARAM);
}
}
//图片上传七牛
private function up_qiniu($imgs,$file){
$this->load->library('qiniu');
//上传七牛并保存
foreach($imgs as $key=>$val){
$file_name = md5($val).'.jpg';
$result = $this->qiniu->fetch($val,$file_name);
if($result){
$imgs[$key] = $result['file'];
if (strpos($val, $file) !== false) {
@unlink(FCPATH.$file);
}
}
}
return $imgs;
}
}