add-api-qywechat-robot

This commit is contained in:
lccsw
2021-10-09 14:43:05 +08:00
parent 1c83ac1591
commit 4fbb5f9787
3 changed files with 352 additions and 0 deletions
+252
View File
@@ -0,0 +1,252 @@
<?php
defined('WXAPP_APP') OR exit('No direct script access allowed');
/**
* Created by Vim
* User: lcc
* Date: 2021/10/09
* Time: 10:08
*/
require_once APPPATH.'controllers/wxapp/Wxapp.php';
class Clues 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();//授权微信信息
}
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;
}
}
@@ -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);
+95
View File
@@ -0,0 +1,95 @@
<?php
/**
* Created by Vim
* User: lcc
* Desc: 企业微信机器人
* Date: 2021/10/09
* Time: 11:01
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class Qyrobot{
private $key = 'dfed1a38-c4e0-4904-94eb-306e9755be04'; //正式
private $ci;
public function __construct(){
$this->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
恭喜 <font color=\"warning\">{$biz_info}</font> 成交 <font color=\"warning\">{$car_info}</font> 一台。\n
>本日累计成交: <font color=\"warning\">{$today_count}</font> 单
>本月累计成交: <font color=\"warning\">{$month_count}</font> 单\n
### 狸车加油,冲冲冲!💪🏻💪🏻💪🏻"
]
];
$res = $this->ci->mycurl->httpPost($url,$data,'is_json');
$result = json_decode($res,true);
return $result;
}
}