📖 File Reader
<?php
namespace app\common;
use app\api\common\Common_config;
use app\BaseController;
/*微信卡券*/
class Common_wechat_card extends BaseController
{
protected $app_id = '';//app_id
protected $app_secret = '';//secret
public function initialize()
{
$config = Common_config::config("'app_id','app_secret'");
$this->app_id = $config["app_id"];
$this->app_secret = $config["app_secret"];
}
/*获取access_token*/
public function get_token()
{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->app_id."&secret=".$this->app_secret;
if (empty(session("access_token")) || session("expires_in") <= time()){
$data = $this->httpRequest($url);
$data = json_decode($data, true);
session("access_token",$data["access_token"]);
session("expires_in",time()+7000);
$access_token = $data["access_token"];
}else{
$access_token = session("access_token");
}
return $access_token;
}
/*创建卡卷*/
public function creation_card()
{
$access_token = $this->get_token();
/*上传卡券图片素材*/
$url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=".$access_token;
$logo ='/www/wwwroot/yishouhg.jiboshimr.cn/public/uploads/卡卷logo.jpg'; // 一定要图片的绝对路径
$file = new \CURLFile(realpath($logo)); //文件流数据
$data = [
'buffer'=>$file,
'access_token'=>$access_token,
];
$output = $this->httpRequest($url,$data,'POST');
$output_arr = json_decode($output,true);
$img_url = $output_arr['url'];
/*创建卡券*/
$url = "https://api.weixin.qq.com/card/create?access_token=".$access_token;
$data = [
'card'=>[
'card_type'=>'CASH',//代金券
'cash'=>[
'base_info'=>[
'logo_url'=>$img_url,
'code_type'=>'CODE_TYPE_QRCODE',
'brand_name'=>'刘一手火锅',
'title'=>'卡卷名',
'color'=>'Color010',//卷颜色
'notice'=>'卡券使用提醒(请出示二维码)',
'description'=>'卡券使用说明...',
'sku'=>[
'quantity'=>10000,
], //产品信息
"date_info"=> [
'type'=>'DATE_TYPE_FIX_TIME_RANGE',
'begin_timestamp'=>1736580177, //启用日期
'end_timestamp'=>1737012177, //结束日期
], //有效日期
],
'least_cost'=>(1000*100),
'reduce_cost'=>(100*100)
],
]
];
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
$output = $this->httpRequest($url,$data,'POST');
$output_arr = json_decode($output,true);
}
public function httpRequest($url, $data = '', $method = 'GET')
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
if ($method == 'POST') {
curl_setopt($curl, CURLOPT_POST, 1);
if ($data != '') {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
}
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
}