快手本地生活-开放平台:https://open.kwailocallife.com/docs/dev
快手本地生活-商家中心:https://lbs.kuaishou.com/ll/merchant/login
实现功能:对接快手券码,实现在快手上购买券码,然后在自己开发的app上扫码核销,抵扣优惠券
<?php
namespace app\api\controller;
use app\api\controller\Base;
use service\ApiReturn;
use think\Db;
header('Content-Type: application/x-www-form-urlencoded');
/**
* 第三方接口【快手接口】
* @package app\api\controller\v1
*/
class Kwailocallife extends Base
{
public $app_id = '' ;//开发者的appKey
public $grant_type = 'code' ;//授权的类型 写死 "code"
public $code = '' ;//临时授权票据
public $app_secret = '' ;//开发者的appSecret
public $access_token = '' ;
public function __construct()
{
//获取快手配置
$member_config = Db::name('member_config')->where('name','kwailocallife')->value('value');
$member_config = json_decode($member_config,true) ;
$this->app_id = $member_config['app_id'] ;
$this->app_secret = $member_config['app_secret'] ;
}
/**
* Notes:根据code获取access_token---api调用使用access_token
* User: 任性不起来了
* Date: 2024/5/14 15:20
* @param $data
* @param $user
*/
public function get_access_token($data,$user){
$data = input() ;
$code = $data['code'] ;
error_log(date('Y-m-d H:i:s').' 授权'.json_encode($data,320).' '.PHP_EOL,3,'../runtime/Kwailocallife/get_access_token.log');
$url = 'https://lbs-open.kuaishou.com/oauth2/access_token';
$param = [
'app_id' => $this->app_id,
'grant_type' => $this->grant_type,
'code' => $code,
'app_secret' => $this->app_secret,
];
$res = $this->http($url,$param,'GET');
$res = json_decode($res,true);
error_log(date('Y-m-d H:i:s').' 授权【$res】'.json_encode($res,320).' '.PHP_EOL,3,'../runtime/Kwailocallife/get_access_token.log');
if($res && $res['extra']['error_code'] == 0 ){
//updateTime:2024-05-18 授权成功保存token信息
$time = time();
$expires_time = $time + $res['expires_in'] ;
$refresh_expires_time = $time + $res['refresh_token_expires_in'] ;
$update = [
'access_token' => $res['access_token'] ,
'open_id' => $res['open_id'] ,
'expires_in' => $res['expires_in'] ,
'token_type' => $res['token_type'] ,
'refresh_token' => $res['refresh_token'] ,
'refresh_token_expires_in' => $res['refresh_token_expires_in'] ,
'expires_time' => $expires_time ,
'refresh_expires_time' => $refresh_expires_time ,
'update_time' => $time ,
] ;
Db::name('kuaishou_token')->where('aid',1)->update($update) ;
return ApiReturn::r(1, ['access_token'=>$res['access_token']], '获取access_token成功');
}else{
return ApiReturn::r(0, [], '获取access_token失败');
}
}
/**
* Notes:获取保存的快手token
* User: 任性不起来了
* Date: 2024/5/18 10:23
* @param $data
* @param $user
*/
public function get_kuaishou_token($data,$user){
//获取快手授权token
$token = Db::name('kuaishou_token')->where('aid',1)->field('aid,access_token,refresh_token,open_id,expires_time,refresh_expires_time,update_time,poi_id')->find();
//todo 授权到期前---再重新获取新的token
return ApiReturn::r(1, $token, 'token');
}
/**
* Notes:新版-门店信息查询接口
* User: 任性不起来了
* Date: 2024/5/16 9:40
*/
public function shop_query(){
$data = input() ;
error_log(date('Y-m-d H:i:s').' $data'.json_encode($data,320).' '.PHP_EOL,3,'../runtime/Kwailocallife/shop_query.log');
$url = 'https://lbs-open.kuaishou.com/goodlife/v1/shop/poi/query';
$param = [
'size' => 15,
'page' => 1,
// 'third_id' => '11',//三方ID
'account_id' => '210000510000******',//本地生活账户ID【需替换为自己的账号ID】
// 'poi_id' => '1',//快手门店POI_ID
];
$header = [
'Content-Type: application/json',
'access-token: '.$data['access_token'],
];
$res = $this->http($url,$param,'GET',$header);
$res = json_decode($res,true);
error_log(date('Y-m-d H:i:s').' 授权【$res】'.json_encode($res,320).' '.PHP_EOL,3,'../runtime/Kwailocallife/shop_query.log');
if($res && $res['extra']['error_code'] == 0 ){
$pois = $res['data'] ;
return ApiReturn::r(1, $pois, '门店信息');
}else{
return ApiReturn::r(0, [], $res['extra']['sub_description'] );
}
}
/**
* Notes:验券准备
* User: 任性不起来了
* Date: 2024/5/16 14:43
*/
public function certificate_prepare(){
$data = input() ;
error_log(date('Y-m-d H:i:s').' $data'.json_encode($data,320).' '.PHP_EOL,3,'../runtime/Kwailocallife/certificate_prepare.log');
$url = 'https://lbs-open.kuaishou.com/goodlife/v1/fulfilment/certificate/prepare';
// $encrypted_data = $data['encrypted_data'] ? urlencode($data['encrypted_data']) : '' ;
$encrypted_data = $data['encrypted_data'] ? $data['encrypted_data'] : '' ;
$param = [
'encrypted_data' => $encrypted_data ,//从二维码解析出来的标识(传参前需要先进行URL编码) (encrypted_data/code必须二选一)
// 'code' => $data['code'],//原始的快手团购券码 (encrypted_data/code必须二选一)
];
error_log(date('Y-m-d H:i:s').' encrypted_data==='.$encrypted_data.' '.PHP_EOL,3,'../runtime/Kwailocallife/certificate_prepare.log');
$header = [
'Content-Type: application/json',
'access-token: '.$data['access_token'],
];
$res = $this->http($url,$param,'GET',$header);
$res = json_decode($res,true);
error_log(date('Y-m-d H:i:s').' 【$res】'.json_encode($res,320).' '.PHP_EOL,3,'../runtime/Kwailocallife/certificate_prepare.log');
if($res && $res['extra']['error_code'] == 0 ){
return ApiReturn::r(1, $res['data'], '验券准备');
}else{
return ApiReturn::r(0, [], $res['extra']['sub_description'] );
}
}
/**
* Notes:验券核销接口
* User: 任性不起来了
* Date: 2024/5/16 14:43
*/
public function certificate_verify(){
$data = input() ;
error_log(date('Y-m-d H:i:s').' $data'.json_encode($data,320).' '.PHP_EOL,3,'../runtime/Kwailocallife/certificate_verify.log');
//校验快手券是否已使用
$is_find = Db::name('order')->where('ks_order_id',$data['order_id'])->find();
if($is_find){
return ApiReturn::r(0, [], '该券码已使用,请勿重复核销' );
}
$url = 'https://lbs-open.kuaishou.com/goodlife/v1/fulfilment/certificate/verify';
$param = [
'verify_token' => $data['verify_token'],//一次验券的标识 (用于短时间内的幂等); 平台券的verify_token由验券准备接口返回; 三方券的verify_token由开发者自生成,多次验券verify_token的值要有变化
'poi_id' => $data['poi_id'],//核销的快手门店id
'order_id' => $data['order_id'],//快手订单号
'encrypted_codes' => [$data['encrypted_code']],//验券准备接口返回的加密快手券码
// 'codes' => [$data['codes']],//快手核销券码
];
$param = json_encode($param);
error_log(date('Y-m-d H:i:s').' 【$param】'.json_encode($param,320).' '.PHP_EOL,3,'../runtime/Kwailocallife/certificate_verify.log');
$header = [
'Content-Type: application/json',
'access-token: '.$data['access_token'],
];
//调用curl请求,原来的post请求不好使---改为调用滴滴的post请求
$res = $this->postCurl($url,$param,'POST',$header);
error_log(date('Y-m-d H:i:s').' 【$res】'.json_encode($res,320).' '.PHP_EOL,3,'../runtime/Kwailocallife/certificate_verify.log');
if($res && $res['extra']['error_code'] == 0 ){
//获取订单数据
$order = $this->order_detail($data);
error_log(date('Y-m-d H:i:s').' 【$order】'.json_encode($order,320).' '.PHP_EOL,3,'../runtime/Kwailocallife/certificate_verify.log');
$ks_coupon_money = $order['item_info']['market_price'] ;
//金额单位为分
$ks_coupon_money = bcdiv($ks_coupon_money,100,2) ;
//修改订单数据
$update = [
'ks_order_id' => $data['order_id'] ,
'ks_coupon_money' => $ks_coupon_money ,
'update_time' => time() ,
] ;
Db::name('order')->where('order_sn',$data['order_sn'])->update($update);
return ApiReturn::r(1, $res['data'], '核销成功');
}else{
return ApiReturn::r(0, [], $res['extra']['sub_description'] );
}
}
/**
* Notes:撤销核销
* User: 任性不起来了
* Date: 2024/5/16 14:43
*/
public function certificate_cancel(){
$data = input() ;
error_log(date('Y-m-d H:i:s').' $data'.json_encode($data,320).' '.PHP_EOL,3,'../runtime/Kwailocallife/certificate_cancel.log');
//校验快手券是否已使用
$is_find = Db::name('order')->where('ks_order_id',$data['order_id'])->find();
if($is_find){
return ApiReturn::r(0, [], '该券码已使用,请勿重复核销' );
}
$url = 'https://lbs-open.kuaishou.com/goodlife/v1/fulfilment/certificate/cancel';
$param = [
'verify_id' => $data['verify_id'],//代表券码一次核销的唯一标识(验券时返回)
'certificate_id' => $data['certificate_id'],//代表一张券码的标识(验券时返回)
// 'order_id' => $data['order_id'],//订单号 用code_with_biz_cancel_info撤销核销时,必传
];
$param = json_encode($param);
error_log(date('Y-m-d H:i:s').' 【$param】'.json_encode($param,320).' '.PHP_EOL,3,'../runtime/Kwailocallife/certificate_cancel.log');
$header = [
'Content-Type: application/json',
'access-token: '.$data['access_token'],
];
//调用curl请求,原来的post请求不好使---改为调用滴滴的post请求
$res = $this->postCurl($url,$param,'POST',$header);
error_log(date('Y-m-d H:i:s').' 【$res】'.json_encode($res,320).' '.PHP_EOL,3,'../runtime/Kwailocallife/certificate_cancel.log');
if($res && $res['extra']['error_code'] == 0 ){
//获取订单数据
$order = $this->order_detail($data);
error_log(date('Y-m-d H:i:s').' 【$order】'.json_encode($order,320).' '.PHP_EOL,3,'../runtime/Kwailocallife/certificate_cancel.log');
//修改订单数据
$update = [
'ks_order_id' => 0 ,
'ks_coupon_money' => 0 ,
'update_time' => time() ,
] ;
Db::name('order')->where('order_sn',$data['order_sn'])->update($update);
return ApiReturn::r(1, $res['data'], '撤销成功');
}else{
return ApiReturn::r(0, [], $res['extra']['sub_description'] );
}
}
/**
* Notes:前端使用扫码插件,通过短链获取长链
* User: 任性不起来了
* Date: 2024/5/21 14:06
*/
public function get_result(){
$data = input() ;
$url = $data['result'];//https://ksurl.cn/z5H2hjQ0
$response = $this->restoreUrl($url);
error_log(date('Y-m-d H:i:s').' 【$url】'.$url.' '.PHP_EOL,3,'../runtime/Kwailocallife/get_result.log');
error_log(date('Y-m-d H:i:s').' 【$response===】'.json_encode($response,320).' '.PHP_EOL,3,'../runtime/Kwailocallife/get_result.log');
$res = explode('object_id=',$response);
if($res){
return ApiReturn::r(1, ['object_id'=>$res[1]], '长链');
}else{
return ApiReturn::r(0, [], 'url解析失败' );
}
}
/**
* 短链还原成长链
* @param $shortUrl
* @return mixed|string
*/
public function restoreUrl($shortUrl)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $shortUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla / 5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko / 20100101 Firefox / 70.0');
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_NOBODY, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
$data = curl_exec($curl);
$curlInfo = curl_getinfo($curl);
curl_close($curl);
if ($curlInfo['http_code'] == 301 || $curlInfo['http_code'] == 302) {
return $curlInfo['redirect_url'];
}
return '';
}
/**
* Notes:查询快手订单详情
* User: 任性不起来了
* Date: 2024/5/18 11:25
* @param $data
* @param $user
*/
public function order_detail($data,$user){
error_log(date('Y-m-d H:i:s').' $data'.json_encode($data,320).' '.PHP_EOL,3,'../runtime/Kwailocallife/order_detail.log');
$url = 'https://lbs-open.kuaishou.com/goodlife/v2/trade/order/detail';
$param = [
'order_id' => $data['order_id'],//快手订单号
];
error_log(date('Y-m-d H:i:s').' 【$param】'.json_encode($param,320).' '.PHP_EOL,3,'../runtime/Kwailocallife/order_detail.log');
$header = [
'Content-Type: application/json',
'access-token: '.$data['access_token'],
];
$res = $this->http($url,$param,'GET',$header);
$res = json_decode($res,true);
error_log(date('Y-m-d H:i:s').' 【$res】'.json_encode($res,320).' '.PHP_EOL,3,'../runtime/Kwailocallife/order_detail.log');
if($res && $res['extra']['error_code'] == 0 ){
return $res['data'] ;
return ApiReturn::r(1, $res['data'], '订单详情');
}else{
return ApiReturn::r(0, [], $res['extra']['sub_description'] );
}
}
/**
* Notes: curl——》 get请求
* User: 任性不起来了
* Date: 2024/5/18 14:48
* @param $url
* @param $params
*/
public function http($url, $params, $method = 'GET', $header = array(), $multi = false)
{
error_log(date('Y-m-d H:i:s').' 【$params】'.json_encode($params,320).' '.PHP_EOL,3,'../runtime/Kwailocallife/http.log');
if(empty($header)){
$header = [
'Accept: application/json',
'User-Agent: */*',
'Content-Type: application/json; charset=utf-8',
];
}
$opts = array(
CURLOPT_TIMEOUT => 30,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => $header
);
/* 根据请求类型设置特定参数 */
switch (strtoupper($method)) {
case 'GET':
$opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
break;
case 'POST':
//判断是否传输文件
$params = $multi ? $params : http_build_query($params);
$opts[CURLOPT_URL] = $url;
$opts[CURLOPT_POST] = 1;
$opts[CURLOPT_POSTFIELDS] = $params;
break;
default:
throw new Exception('不支持的请求方式!');
}
error_log(date('Y-m-d H:i:s').' 【$opts】'.json_encode($opts,320).' '.PHP_EOL,3,'../runtime/Kwailocallife/http.log');
/* 初始化并执行curl请求 */
$ch = curl_init();
curl_setopt_array($ch, $opts);
$data = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error) throw new Exception('请求发生错误:' . $error);
error_log(date('Y-m-d H:i:s').' 【$data】'.json_encode($data,320).' '.PHP_EOL,3,'../runtime/Kwailocallife/http.log');
return $data;
}
/**
*$this->postCurl方法
*/
public function postCurl($url, $body,$type='POST',$header=[])
{
$headers = array(
"Content-type:application/x-www-form-urlencoded;charset=UTF-8"
);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//1.创建一个curl资源
$ch = curl_init();
//2.设置URL和相应的选项
curl_setopt($ch, CURLOPT_URL, $url);//设置url
//设置为false,只会获得响应的正文(true的话会连响应头一并获取到)
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 设置超时限制防止死循环
//设置发起连接前的等待时间,如果设置为0,则无限等待。
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
//将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//2)设备请求体
if (@count($body) > 0) {
// $b=json_encode($body,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);//全部数据使用HTTP协议中的"POST"操作来发送。
}
//设置请求头
if (count($header) > 0) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
//上传文件相关设置
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 对认证证书来源的检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);// 从证书中检查SSL加密算
//3)设置提交方式
switch ($type) {
case "GET":
curl_setopt($ch, CURLOPT_HTTPGET, true);
break;
case "POST":
curl_setopt($ch, CURLOPT_POST, true);
break;
case "PUT"://使用一个自定义的请求信息来代替"GET"或"HEAD"作为HTTP请 求。这对于执行"DELETE" 或者其他更隐蔽的HTT
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
break;
case "DELETE":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
}
//4)在HTTP请求中包含一个"User-Agent: "头的字符串。-----必设
curl_setopt($ch, CURLOPT_USERAGENT, 'SSTS Browser / 1.0');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla / 4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident / 4.0)'); // 模拟用户使用的浏览器
//5)
//3.抓取URL并把它传递给浏览器
$res = curl_exec($ch);
$result = json_decode($res, true);
//4.关闭curl资源,并且释放系统资源
curl_close($ch);
if (empty($result))
return $res;
else
return $result;
}
}