From 6748e392f0dae070b710461917963e978a51e46d Mon Sep 17 00:00:00 2001 From: lccsw <805383944@qq.com> Date: Fri, 1 Apr 2022 16:35:14 +0800 Subject: [PATCH] add-api-pay_img --- api/controllers/Welcome.php | 4 - api/controllers/wxapp/licheb/CusorderV2.php | 6 +- common/libraries/Hd_wechat.php | 88 +++++++++++++++++++ .../libraries/receiver/Orders_v2_entity.php | 20 ++++- 4 files changed, 110 insertions(+), 8 deletions(-) diff --git a/api/controllers/Welcome.php b/api/controllers/Welcome.php index e001f174..b80b45fe 100755 --- a/api/controllers/Welcome.php +++ b/api/controllers/Welcome.php @@ -11,9 +11,5 @@ class Welcome extends CI_Controller { } public function test(){ - $this->load->library('receiver/orders_v2_entity'); - $biz_id = 134; - $row = $this->orders_v2_entity->get_biz_mchid($biz_id); - print_r($row); } } diff --git a/api/controllers/wxapp/licheb/CusorderV2.php b/api/controllers/wxapp/licheb/CusorderV2.php index 64b26c39..4d35d53e 100644 --- a/api/controllers/wxapp/licheb/CusorderV2.php +++ b/api/controllers/wxapp/licheb/CusorderV2.php @@ -211,7 +211,7 @@ class CusorderV2 extends Wxapp{ $this->customers_model->update(array('order_time' => date('Y-m-d H:i:s')),['id'=>$cus_id]); $this->customers_entity->add_log($cus_id, $this->session['uid'], $this->session['uname'], "新增订单"); - return ['id'=>$o_id,'pay_img'=>$orders_entity::API_PAY_IMG]; + return ['id'=>$o_id,'pay_img'=>$orders_entity->pay_img($o_id)]; }else{ throw new Exception('创建失败', ERR_PARAMS_ERROR); } @@ -289,7 +289,7 @@ class CusorderV2 extends Wxapp{ $userinfo = $this->app_liche_users_model->get(['mobile'=>$data['mobile']],'id'); $orders_entity->c_intention($o_id,$userinfo['id'],$inten_money); } - return ['id'=>$o_id,'pay_img'=>$orders_entity::API_PAY_IMG]; + return ['id'=>$o_id,'pay_img'=>$orders_entity->pay_img($o_id)]; } //修改订单信息 @@ -470,7 +470,7 @@ class CusorderV2 extends Wxapp{ 'payway' => $row['brand_id'] ? $row['payway'] : '', 'car_data' => $car_data, 'pay_status' => $pay_status, - 'pay_img' => $orders_entity::API_PAY_IMG, + 'pay_img' => $orders_entity->pay_img($row['id']), 'pack_id' => $row['pack_id'], 'brand_id' => $row['brand_id'], 'car_id' => $row['s_id'], diff --git a/common/libraries/Hd_wechat.php b/common/libraries/Hd_wechat.php index 9953e792..0f83b755 100755 --- a/common/libraries/Hd_wechat.php +++ b/common/libraries/Hd_wechat.php @@ -169,6 +169,66 @@ class Hd_wechat return $api_ticket; } + /** + * 生成二维码 + * @param $filename (文件名称) + * @param $scene (最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式) + * @param $page (必须是已经发布的小程序存在的页面(否则报错),例如 pages/index/index, 根路径前不要填加 /,不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面) + * @param $width (二维码的宽度,单位 px,最小 280px,最大 1280px,默认430px) + * @return array {'file':'文件路径', 'url':'访问相对路径'} + */ + function qrcode($filename, $scene, $page, $width){ + $file = APPPATH . '../www/api/wx/' . $filename . '.png'; + $dir = substr($file, 0, strrpos($file, '/')); + if(!is_dir($dir)){ + $ret = mkdir($dir, 0777, true);// 如果文件夹不存在,将以递归方式创建该文件夹 + if(!$ret){ + debug_log("[error] ". __FUNCTION__ . ": mkdir {$ret}, filename:{$filename}, scene:{$scene}, page:{$page}, width:{$width}", $this->log_dir); + return array(); + } + } + + if(file_exists($file)){ + return array('file' => $file, 'url' => 'wx/' . $filename . '.png'); + } + + $access_token = $this->access_token(); + if(!$access_token){ + debug_log("[error] ". __FUNCTION__ . ": not access_token", $this->log_dir); + return array(); + } + + $pre_url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='; + $url = $pre_url . $access_token; + $data = array( + 'scene' => $scene, + 'page' => $page, + 'width' => $width ? $width : 430, + ); + + list($code, $res) = $this->http_post($url, $data); + $ret = json_decode($res, true); + + if(isset($ret['errcode']) && 40001 == $ret['errcode']){//token过期,重置后请求 + $url = $pre_url . $this->access_token(true); + list($code, $res) = $this->http_post($url, $data); + $ret = json_decode($res, true); + } + + if(isset($ret['errcode'])){ + debug_log("[error] ". __FUNCTION__ . ": httpcode:{$code}, response:{$res}, filename:{$filename}, scene:{$scene}, page:{$page}, width:{$width}", $this->log_dir); + return array(); + } + + $ret = file_put_contents($file, $res); + if(false === $ret){ + debug_log("[error] ". __FUNCTION__ . ": file_put_contents {$ret}, filename:{$filename}, scene:{$scene}, page:{$page}, width:{$width}", $this->log_dir); + return array(); + } + + return array('file' => $file, 'url' => 'wx/' . $filename . '.png'); + } + /** * @param $url * @return mixed @@ -198,4 +258,32 @@ class Hd_wechat return $res; } + + /** + * @param $url + * @param $data + * @return array + */ + private function http_post($url, $data){ + debug_log("[info] ". __FUNCTION__ . ":url={$url}, data=".json_encode($data), $this->log_dir); + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); + $res = curl_exec($ch); + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + $size = strlen($res); + if($size > 5000){ + debug_log("[info] ". __FUNCTION__ . ":httpcode={$code}, response is big only show size={$size}", $this->log_dir); + } else { + debug_log("[info] ". __FUNCTION__ . ":httpcode={$code}, response={$res}", $this->log_dir); + } + + return array($code, $res); + } } diff --git a/common/libraries/receiver/Orders_v2_entity.php b/common/libraries/receiver/Orders_v2_entity.php index 3841c129..0ede6624 100644 --- a/common/libraries/receiver/Orders_v2_entity.php +++ b/common/libraries/receiver/Orders_v2_entity.php @@ -11,7 +11,6 @@ class Orders_v2_entity{ const PRICE_FINANCE = 1000; //金融服务费写死 const PRICE_FINANCE_NZ = 2000; //哪吒金融服务费 const API_PAY_IMG = 'https://img.liche.cn/liche/2022/04/22ed0636993504bc/18ad8baab11b2891.png'; //api 跳转支付二维码 - //const API_CREATE_PAY_IMG = 'https://img.liche.cn/liche/2021/11/65b34b962f5f06b3/62e7d28d77bfe404.png'; //api 狸车我的页面 const V2_START_ID = 10000; //升级后订单开始 private $ci; @@ -639,4 +638,23 @@ class Orders_v2_entity{ return $data; } + //小程序支付图片 + public function pay_img($oid){ + $this->ci->load->model('app/app_model', 'mdApp'); + $wxconfig = $this->ci->mdApp->appConfig()[1]['wx']; + $img = self::API_PAY_IMG; + if ($wxconfig) { + $this->ci->load->library("hd_wechat", $wxconfig); + $width = '430px'; + $path = "pages/mine/carOrder/index"; + $scene = $oid; + $filename = "{$this->ci->mdApp->appConfig()[1]['app_key']}/" . substr(md5($path.'?id='.$oid), 8, 16); + $result = $this->ci->hd_wechat->qrcode($filename,$scene,$path,$width); + if($result['url']){ + $img = http_host_com('api').'/'.$result['url']; + } + } + return $img; + } + }