From 4fbb5f9787157e9be3cc9f24ccc4679b2f11c85c Mon Sep 17 00:00:00 2001
From: lccsw <1127794702@qq.com>
Date: Sat, 9 Oct 2021 14:43:05 +0800
Subject: [PATCH] add-api-qywechat-robot
---
api/controllers/wxapp/liche/Clues.php | 252 ++++++++++++++++++++
api/controllers/wxapp/liche/Wxnotify_v3.php | 5 +
common/libraries/Qyrobot.php | 95 ++++++++
3 files changed, 352 insertions(+)
create mode 100644 api/controllers/wxapp/liche/Clues.php
create mode 100644 common/libraries/Qyrobot.php
diff --git a/api/controllers/wxapp/liche/Clues.php b/api/controllers/wxapp/liche/Clues.php
new file mode 100644
index 00000000..08cad53d
--- /dev/null
+++ b/api/controllers/wxapp/liche/Clues.php
@@ -0,0 +1,252 @@
+login_white = array();//登录白名单
+ $this->check_status = array();//用户状态校验
+ $this->check_mobile = array();//需要手机号
+ $this->check_headimg =array();//授权微信信息
+ }
+
+ 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('receiver/orders_entity');
+ $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']]);
+ }
+ }
+
+ $data = [
+ 'title' => $title,
+ 'url' => $h5_url,
+ 'img' => 'https://qs.haodian.cn/wechat_app/liche/mine/he-demo-2.jpg',
+ ];
+ 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],'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){
+ $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'] ? 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->load->library('receiver/orders_entity');
+ $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->load->library('receiver/orders_entity');
+ $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;
+ 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;
+ }
+}
diff --git a/api/controllers/wxapp/liche/Wxnotify_v3.php b/api/controllers/wxapp/liche/Wxnotify_v3.php
index 88e1244c..dc8ca730 100644
--- a/api/controllers/wxapp/liche/Wxnotify_v3.php
+++ b/api/controllers/wxapp/liche/Wxnotify_v3.php
@@ -110,6 +110,11 @@ class Wxnotify_v3 extends CI_Controller{
$this->load->service('apporder/payment_service', array('app_id' => $this->app_id));
$result = $this->payment_service->after_pay_liche($sid,$result['data']['amount']['payer_total']/100);
if($result['code']){
+ if($order['type']==1){ //支付定金推送企业微信群
+ $this->load->library('qyrobot');
+ $robot_res = $this->qyrobot->deposit_notify($order['o_id']);
+ debug_log("[info] ". __FUNCTION__ . ":推送企业微信群 ".json_encode($robot_res,JSON_UNESCAPED_UNICODE), $this->log_file,$this->log_dir);
+ }
debug_log("[success] ". __FUNCTION__ . ":操作成功", $this->log_file,$this->log_dir);
}else{
debug_log("[error] ". __FUNCTION__ . ":".$result['msg'], $this->log_file,$this->log_dir);
diff --git a/common/libraries/Qyrobot.php b/common/libraries/Qyrobot.php
new file mode 100644
index 00000000..4e31734e
--- /dev/null
+++ b/common/libraries/Qyrobot.php
@@ -0,0 +1,95 @@
+ci = & get_instance();
+ if($_SERVER['CI_ENV'] == 'development'){ //测试环境
+ $this->key = '0b644923-4e5c-46be-9a87-6dfcb023d09c';
+ }
+ }
+ /**
+ * 支付定金推送消息
+ * @param $oid (lc_receiver_orders表)订单id
+ */
+ public function deposit_notify($oid){
+ $this->ci->load->model('receiver/order/receiver_orders_model');
+ $this->ci->load->model("biz/biz_model");
+ $this->ci->load->model('auto/auto_brand_model');
+ $this->ci->load->model('auto/auto_series_model');
+
+ $this->ci->load->model('app/liche/app_liche_orders_model');
+ $this->ci->load->model('app/liche/app_liche_orders_model');
+ $this->ci->load->model('app/licheb/app_licheb_users_model');
+
+ $order_row = $this->ci->receiver_orders_model->get(['id'=>$oid]);
+ if(!$order_row){
+ return false;
+ }
+ $biz_row = $this->ci->biz_model->get(['id'=>$order_row['biz_id']]);
+ if($biz_row['type']==3){
+ $where = [
+ 'group_id' => 4,
+ 'status' => 1,
+ "biz_id like '%{$biz_row['id']}%'" => null
+ ];
+ $users = $this->ci->app_licheb_users_model->select($where,'id desc',1,10,'uname,biz_id'); //渠道经理
+ $user_info = '';
+ if($users){
+ foreach($users as $key=>$val){
+ $biz_id_arr = explode(',',$val['biz_id']);
+ if(!in_array($biz_row['id'],$biz_id_arr)){
+ unset($users[$key]);
+ }
+ }
+ $user_info = implode(' ',array_column($users,'uname'));
+ }
+ $biz_info = "{$user_info}渠道 {$biz_row['biz_name']}";
+ }else{
+ $user = $this->ci->app_licheb_users_model->get(['id'=>$order_row['admin_id']],'uname'); //获取销售员
+ $biz_info = "{$biz_row['biz_name']} {$user['uname']}";
+ }
+ $brand_row = $this->ci->auto_brand_model->get(['id'=>$order_row['brand_id']],'name');
+ $series_row = $this->ci->auto_series_model->get(['id'=>$order_row['s_id']],'name');
+ $car_info = "{$brand_row['name']} {$series_row['name']}";
+
+ $where = [
+ 'o_id' => $order_row['id'],
+ 'type' => 1,
+ 'status' => 1,
+ 'pay_price>=' => 100
+ ];
+ $where['c_time>='] = strtotime(date('Y-m-d 00:00:00'));
+ $where['c_time<='] = time();
+ $today_count = $this->ci->app_liche_orders_model->count($where);//今日成交订单数
+ $where['c_time>='] = strtotime(date('Y-m-01 00:00:00'));
+ $month_count = $this->ci->app_liche_orders_model->count($where);//本月成交订单数
+
+ $url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key='.$this->key;
+ $this->ci->load->library('mycurl');
+ $data = [
+ 'msgtype' => 'markdown',
+ 'markdown' => [
+ 'content' => "🎉🎉🎉 **喜讯** 🎉🎉🎉 \n
+ 恭喜 {$biz_info} 成交 {$car_info} 一台。\n
+ >本日累计成交: {$today_count} 单
+ >本月累计成交: {$month_count} 单\n
+ ### 狸车加油,冲冲冲!💪🏻💪🏻💪🏻"
+ ]
+ ];
+ $res = $this->ci->mycurl->httpPost($url,$data,'is_json');
+ $result = json_decode($res,true);
+ return $result;
+ }
+}