对接有赞商城(有赞商家自用型)

479 阅读1分钟

官网:https://www.youzan.com/

注册有赞的商户,进行企业认证,成为商户之后,登录后台获取店铺Id

创建应用时选择有赞商家(自用型),选择部署在本地,需等待审核(1-3天)



审核通过后会获取client_id,client_secret,也可创建测试店铺,进行开发



进入控制台对注册的商户进行授权


然后进入商城后台同意授权


开发时先 下载有赞的PHPsdk,推荐使用composer

composer require youzanyun/open-sdk


代码;

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/4
 * Time: 14:54
 */
namespace app\common\logic;

use app\common\model\Config;

class Youzan extends Base{

    private $client_id = '********';
    private $client_secret = '*****************';
    private  $store_id = '**********';

    /**
     * @return mixed
     * 获取token
     */
    public function getAccessToken(){
        $config_model = new  Config();
        $config = $config_model->findOne(['id'=>1],'youzan_token,youzan_expire_time');
        $time = microsecond();
        if (($config['youzan_token'] == null) || ($config['youzan_expire_time'] < $time)) {
            //获取access_token
            $AccessToken = new \Youzan\Open\Token($this->client_id, $this->client_secret);
            $result = $AccessToken->getSelfAppToken($this->store_id);
            $config_model->where(['id'=>1])->update(['youzan_token'=>$result['access_token'],'youzan_expire_time'=>$result['expires']]);
            $access_token = $result['access_token'];
            //dump($result);die;
        } else {
            $access_token = $config['youzan_token'];
        }
        return $access_token;
    }

    /**
     * 查询是否有有赞账号
     */
    public function isYouzanUser($phone){
        $accessToken = $this->getAccessToken();
        $client = new \Youzan\Open\Client($accessToken);
        $method = 'youzan.users.account.check';
        $apiVersion = '1.0.0';

        $data = [
            'account_id' => $phone,
            'account_type' => 'Mobile',
        ];

        //设置参数
        $params =$data;
        $response = $client->post($method, $apiVersion, $params);
        $is_have = 1;
        if($response['data'] === true){
            $is_have = 2;
        }
        return $is_have;
    }


    /**
     * 获取用户积分
     */
    public function getUserIntegral($phone){
        $accessToken = $this->getAccessToken();
        $client = new \Youzan\Open\Client($accessToken);
        $method = 'youzan.crm.customer.points.get';
        $apiVersion = '1.0.0';
        $data = [
            'account_id' => $phone,
            'account_type' => 2,
        ];
        //设置参数
        $params =[
            "user"=> $data,
        ];
        $response = $client->post($method, $apiVersion, $params);
        $integral = 0;
        if($response['code'] == 200){
            $integral = $response['data']['point'];
        }
       return $integral;
    }

    /**
     * 给用户加积分
     */
    public function sendIntegral($phone,$integral,$reason,$log_id){
        $accessToken = $this->getAccessToken();
        $client = new \Youzan\Open\Client($accessToken);
        $method = 'youzan.crm.customer.points.increase';
        $apiVersion = '4.0.0';

        $data = [
            'reason' => $reason,
            'kdt_id' =>$this->store_id,
            'biz_value' => $log_id,
            'client_id' => $this->client_id,
            'points' => $integral,
            'user'=>[
                'account_id' => $phone,
                'account_type' => 2,
            ],
        ];
        //设置参数
        $params =[
            "params"=> $data,
        ];
        $response = $client->post($method, $apiVersion, $params);
        return $response;
    }

    /**
     * 给用户减积分
     */
    public function outIntegral($phone,$integral,$reason,$log_id){
        $accessToken = $this->getAccessToken();
        $client = new \Youzan\Open\Client($accessToken);
        $method = 'youzan.crm.customer.points.decrease';
        $apiVersion = '4.0.0';

        $data = [
            'reason' => $reason,
            'kdt_id' =>$this->store_id,
            'biz_value' => $log_id,
            'client_id' => $this->client_id,
            'points' => $integral,
            'user'=>[
                'account_id' => $phone,
                'account_type' => 2,
            ],
        ];
        //设置参数
        $params =[
            "params"=> $data,
        ];
        $response = $client->post($method, $apiVersion, $params);
        return $response;
    }

    /**
     * 同步积分
     */
    public function synchronizationIntegral($phone,$integral){
        $accessToken = $this->getAccessToken();
        $client = new \Youzan\Open\Client($accessToken);
        $method = 'youzan.crm.customer.points.sync';
        $apiVersion = '4.0.0';

        $data = [
            'account_id' => $phone,
            'account_type' => 2,
        ];
        //设置参数
        $params =[
            'reason' => '用户同步积分',
            'biz_value' => '',
            'points' => $integral,
            "user"=> $data,
        ];
        $response = $client->post($method, $apiVersion, $params);
        return $response;
    }

    /**
     * 获取积分日志
     */
    public function integralLog($phone){
        $accessToken = $this->getAccessToken();
        $client = new \Youzan\Open\Client($accessToken);
        $method = 'youzan.crm.customer.points.changelog.search';
        $apiVersion = '4.0.0';

        $data = [
            'account_id' => $phone,
            'account_type' => 2,
        ];
        //设置参数
        $params = [
            "end_time"=> '2020-06-10 15:29:59',
            "begin_time"=> '2020-06-10 00:00:00',
            "page"=> 1,
            "user"=> $data,
            "page_size"=> 20
        ];
        $response = $client->post($method, $apiVersion, $params);
        dump($response);
    }
}

主要实现的是对接有赞的积分api,实现了是否有有赞账号,同步用户积分,获取用户当前积分,获取积分日志,给用户加积分,给用户减积分等