Files
liche/api/controllers/wxapp/liche/Contract.php
T
2022-09-01 09:26:16 +08:00

372 lines
16 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->model('app/liche/app_liche_orders_model');
$this->load->library('receiver/orders_entity');
$this->load->library('receiver/order_datas_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]);
$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]);
}else{
$con_id = $contract['id'];
$cid = $contract['cid'];
}
//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,
'c_time' => time()
];
$this->contracts_model->update($update,['id'=>$con_id]);
$next_path = ''; //下一步合同
switch($type){
case 0: //整车
$row['srv_ids'] && $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['srv_ids']!=$row['srv_ids']){
//需要签服务补充协议
$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],'id,brand_id,mobile,biz_id,ifentrust,srv_ids,info_json,payway');
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');
$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'] ? 640 : 520;
$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){
//增加分销记录
$this->load->library('entity/deal_entity',['app_id'=>1]);
$deal_res = $this->deal_entity->deal_log($row['o_id']);
if(!$order['srv_ids']){
$userinfo = $this->app_user_model->get(['mobile'=>$order['mobile']]);
$this->orders_entity->sign_after_v2($id,$userinfo);
$order['payway'] && $this->orders_entity->check_finish_v2($id,$userinfo);
}
$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);
$srv_count = $order['srv_ids'] ? count(explode(',',$order['srv_ids'])) : 0;
$height = $order['ifentrust'] ? 1420 : 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){
$userinfo = $this->app_user_model->get(['mobile'=>$order['mobile']]);
$this->orders_entity->sign_after_v2($id,$userinfo);
$order['payway'] && $this->orders_entity->check_finish_v2($id,$userinfo);
$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'] ? 680 : 490;
$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){
//判断尾款是否支付
if($this->app_liche_orders_model->count(['o_id'=>$id,'status'=>1,'type'=>3])){ //尾款已支付
$this->ckcars_model->update(['status'=>3],['o_id'=>$id]);
$this->orders_model->update(['status'=>3],['id'=>$id]);
$bill_row = $this->bills_model->get(['o_id'=>$id]);
if(!$bill_row){
$this->bills_model->add(['o_id'=>$id,'status'=>1,'c_time'=>time()]);
}else{
$this->bills_model->update(['status'=>1],['id'=>$bill_row['id']]);
}
}else{
$this->ckcars_model->update(['status'=>2],['o_id'=>$id]);
}
$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'] ? 1510 : 1370;
$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->orders_model->update(['status'=>6],['id'=>$row['o_id']]);
$this->deliverys_model->update(['status'=>2],['o_id'=>$row['o_id']]);
//完成分销
$this->load->library('entity/deal_entity',['app_id'=>1]);
$this->deal_entity->deal_log_finish($row['o_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'] ? 1480 : 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['srv_ids']==$order['srv_ids']){
//不需要签服务补充协议 完成重新补充协议签约
$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'] ? 1280 : 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;
}
//车辆确认
protected function get_finsh(){
$id = $this->input_param('id');
//生成交车确认pdf
$res = $this->order_datas_entity->fill_finsh_pdf($id);
if(!$res['code']){
throw new Exception($res['msg'], API_CODE_INVILD_PARAM);
}
$contract = $this->contracts_model->get(['type'=>6,'o_id'=>$id]);
if(!$contract){
throw new Exception('参数错误', API_CODE_INVILD_PARAM);
}
$imgs_arr = json_decode($contract['imgs'],true);
$imgs = [];
if($imgs_arr){
foreach($imgs_arr as $key => $val){
$imgs[] = build_qiniu_image_url($val);
}
}
$data = [
'imgs' => $imgs,
];
return $data;
}
}