php对接快手券码,扫码核销

快手本地生活-开放平台: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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值