laravel框架 封装的卡卷管理代码

 '',
        'DISCOUNT' => '',
        'GIFT' => '',
        'CASH' => '',
        'GENERAL_COUPON' => '',
        'MEMBER_CARD' => '',
        'SCENIC_TICKET' => '',
        'MOVIE_TICKET' => '',
        'BOARDING_PASS' => '',
        'MEETING_TICKET' => '',
        'BUS_TICKET' => '',
        'EXPERIENCE' => ''
    ];
    protected $status_text = [
        'CARD_STATUS_NOT_VERIFY' => '待审核',
        'CARD_STATUS_VERIFY_FAIL' => '审核失败',
        'CARD_STATUS_VERIFY_OK' => '审核通过',
        'CARD_STATUS_DELETE' => '商户删除',
        'CARD_STATUS_DISPATCH' => '公众平台投放的卡券'
    ];
    protected $user_card_status_text = [
        'NORMAL' => '正常',
        'CONSUMED' => '已核销',
        'EXPIRE' => '已过期',
        'GIFTING' => '转赠中',
        'GIFT_TIMEOUT' => '转赠超时',
        'DELETE' => '已删除',
        'UNAVAILABLE' => '已失效'
    ];
    public function index(Request $data)
    {
        print_r($this->getApiTicket());
        return view('index/index');
    }
    public function createCoupon($data)
    {
        $this->getAccessToken();

        $url = "https://api.weixin.qq.com/card/create?access_token=".session('token.access_token');
        $card = json_decode($this->http_post($url,json_encode($data,JSON_UNESCAPED_UNICODE)),1);
        //判断是否成功获取
        if($card['errcode'] == 'ok')
        {
            return [
                'card_id' => $card['card_id'],
                'status' => 200
            ];
        }else{
            return [
                'status' => 400,
                'errmsg' => '创建卡卷失败,原因:'.$card['errmsg']
            ];
        }
    }
    //获取素材列表
    public function getMaterial($type='image',$offset=0,$count=20)
    {
        $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token='.session('token.access_token');
        $data = json_encode([
            'type' => $type,
            'offset' => $offset,
            'count' => $count
        ]);
        $res = json_decode($this->http_post($url,$data),1);
        if(empty($res['errmsg']))
        {
            return [
                'status' => 200,
                'material' => $res
            ];
        }else{
            return [
                'status' => 400,
                'errmsg' => '获取素材失败,原因:'.$res['errmsg']
            ];
        }
    }
    //获取卡卷领取二维码

    /**
     * @param $card_id 卡卷id值
     * @param bool $code 自定义code码
     * @param bool $openid 限制用户openid
     * @param bool $unique 二维码一次性
     * @param unsigned int $expire_seconds 二维码有效时间 范围:60 ~ 1800 默认365天
     * @return array
     */
    public function getCardQrcode($card_id,$code=false,$openid=false,$unique=false,$expire_seconds='')
    {
        $this->getAccessToken();
        $data = [
            'action_name' => 'QR_CARD',
            'expire_seconds' => $expire_seconds,//二维码有效时间
            'action_info' =>[
                'card' =>[
                    'card_id' => $card_id ,
                    'code' => $code,//自定义code
                    'openid' => $openid,//用户制定openid
                    'is_unique_code' => $unique,//扫描一次性是否开启
                    'outer_str' => '',//场景值字符串
                    'outer_id' => '',//场景值id
                ],
            ],

        ];
        $url = 'https://api.weixin.qq.com/card/qrcode/create?access_token='.session('token.access_token');
        $res = json_decode($this->http_post($url,json_encode($data)),1);
        if($res['errmsg'] == 'ok')
        {
            return [
                'status' => 200,
                'ticket' => $res['ticket'],
                'expire_seconds' => $res['expire_seconds'],
                'url' => $res['url'],
                'show_qrcode_url' => $res['show_qrcode_url']
            ];
        }else{
            return [
                'status' => 400,
                'errmsg' => $res['errmsg']
            ];
        }
    }
    //上传图片素材 传入name为image的file文件

    /**
     * @param Request $data
     * @return bool
     */
    public function uploadImg($file)
    {
        $this->getAccessToken();
        $api = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=".session('token.access_token')."&type=image";
        $res = json_decode($this->http_post_media($api,$file),1);
        unlink($file);
        if(empty($res['errcode']))
        {
            return [
                'status' => 200,
                'url' => $res['url']
            ];
        }else{
            return [
                'status' => 400,
                'errmsg' => $res['errmsg']
            ];
        }
    }
    //核销卡卷
    /**
     * @param $code //卡卷码
     * @param $card_id //卡卷id
     * @return array
     */
    public function chargeOffStatus($code,$card_id)
    {
        $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/card/code/get?access_token='.session('token.access_token');
        $data = json_encode([
            'code' => $code,
            'card_id' => $card_id,
            'check_consume' => false
        ]);
        $res = json_decode($this->http_post($url,$data),1);
        //检查卡卷是否可核销
        if($res['errcode']==0)
        {
            if($res['can_consume'] == false)
            {
                return [
                    'status' => 400,
                    'msg' => '无法使用卡卷,原因:'.$this->user_card_status_text[$res['user_card_status']]
                ];
            }else{
                $url = 'https://api.weixin.qq.com/card/code/consume?access_token='.session('token.access_token');
                $res = json_decode($this->http_post($url,json_encode([
                    'code'=>$code
                ])),1);
                if($res['errmsg'] == 'ok')
                {
                    return [
                        'status' =>200
                    ];
                }
            }
        }else{
            return [
                'status' => 401,
                'errmsg' => '无法使用的卡卷,卡卷编码错误,请检查卡卷所属商户'
            ];
        }

    }
    //获取卡卷货架

    /**
     * @param $banner 页面banner
     * @param $title 页面标题
     * @param array $card_list 卡卷数据,需要传入卡卷id和缩略图 ['card_id'=>'','thumb_url'=>'']
     * @param bool $can_share
     * @param string $scene
     * @return array
     */
    public function ticketShelf($banner,$title,$card_list,$can_share=true,$scene='SCENE_NEAR_BY')
    {
        $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/card/landingpage/create?access_token='.session('token.access_token');
        $data = json_encode([
            'banner' => $banner,
            'page_title' => $title,
            'can_share' => $can_share ,
            'scene' => $scene,
            'card_list' =>$card_list
        ]);
        $res = json_decode($this->http_post($url,$data),1);
        if($res['errmsg'] == 'ok')
        {
            return [
                'status' => 200,
                'url' => $res['url'],
                'page_id' => $res['page_id']
            ];
        }else{
            return [
                'status' => 400,
                'errmsg' => '获取货架接口出错,原因:',$res['errmsg']
            ];
        }
    }
    //获取卡卷列表

    /**
     * @param int $offset 偏移量
     * @param int $count 列表总数,最多50
     * @param string $status CARD_STATUS_NOT_VERIFY  待审核  CARD_STATUS_VERIFY_FAIL 审核失败 CARD_STATUS_VERIFY_OK 通过审核 CARD_STATUS_DELETE 卡券被商户删除 CARD_STATUS_DISPATCH 在公众平台投放过的卡券
     * @return array
     */
    public function getTicketList($offset=0,$count=50,$status=[])
    {
        $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/card/batchget?access_token='.session('token.access_token');
        $data = json_encode([
            'offset' => $offset,
            'count' => $count,
            'status_list' => $status
            //CARD_STATUS_NOT_VERIFY”,待审核;“CARD_STATUS_VERIFY_FAIL”,审核失败;“CARD_STATUS_VERIFY_OK”,通过审核;“CARD_STATUS_DELETE”,卡券被商户删除;“CARD_STATUS_DISPATCH”,在公众平台投放过的卡券;
        ]);
        $res = json_decode($this->http_post($url,$data),1);
        if($res['errmsg'] == 'ok')
        {
            return [
                'status' => 200,
                'card_id_list' => $res['card_id_list'],
                'total' => $res['total_num']
            ];
        }else{
            return [
                'status' => 400,
                'errmsg' => '获取列表失败,原因:'.$res['errmsg']
            ];
        }
    }
    //获取卡卷详情
    public function getCardInfo($card_id)
    {
        $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/card/get?access_token='.session('token.access_token');
        $data = json_encode([
            'card_id' => $card_id
        ]);
        $res = json_decode($this->http_post($url,$data),1);
        if($res['errmsg'] == 'ok')
        {
            return [
                'status' => 200,
                'info' => $res['card']
            ];
        }else{
            return [
                'status' => 400,
                'errmsg' => '获取详情失败,原因:'.$res['errmsg']
            ];
        }
    }
    //更改卡卷信息 (未完善)
    /**
     * @return array
     */
    public function updateTicket()
    {
        $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/card/update?access_token='.session('token.access_token');
        $data = json_encode([

        ]);
        $res = json_decode($this->http_post($url,$data),1);
        if($res['errmsg'] == 'ok')
        {
            return [
                'status' => 200,
                //'card_id_list' => $res['card_id_list'],
                //'total' => $res['total_num']
            ];
        }else{
            return [
                'status' => 400,
                //'errmsg' => '获取列表失败,原因:'.$res['errmsg']
            ];
        }
    }
    //更改库存接口
    /**
     * @param $card_id 卡卷id
     * @param int $increase_stock_value 增加库存数量
     * @param int $reduce_stock_value 减少库存数量
     * @return array
     */
    public function updateTicketStock($card_id,$increase_stock_value,$reduce_stock_value)
    {
        $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/card/modifystock?access_token='.session('token.access_token');
        $data = json_encode([
            'card_id' => $card_id,
            'increase_stock_value' => $increase_stock_value,
            'reduce_stock_value' => $reduce_stock_value
        ]);
        $res = json_decode($this->http_post($url,$data),1);
        if($res['errmsg'] == 'ok')
        {
            return [
                'status' => 200
            ];
        }else{
            return [
                'status' => 400,
                'errmsg' => '修改库存失败,原因:'.$res['errmsg']
            ];
        }
    }
    //删除卡卷

    /**
     * @param $card_id  卡卷id
     * @return array
     */
    public function deleteTicket($card_id)
    {
        $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/card/delete?access_token='.session('token.access_token');
        $data = json_encode([
            'card_id' => $card_id
        ]);
        $res = json_decode($this->http_post($url,$data),1);
        if($res['errmsg'] == 'ok')
        {
            return [
                'status' => 200
            ];
        }else{
            return [
                'status' => 400,
                'errmsg' => '删除卡卷失败,原因:'.$res['errmsg']
            ];
        }
    }
    //设置卡卷失效

    /**
     * @param string(20) $code 卡卷编码
     * @param string(64) $reason 失效原因
     * @param null $card_id 自定义code需要填写
     * @return array
     */
    public function unavailableTicket($code,$reason,$card_id=null)
    {
        $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/card/code/unavailable?access_token='.session('token.access_token');
        $data = json_encode([
            'code' => $code ,
            'reason' => $reason,
            'card_id' => $card_id,
        ]);
        $res = json_decode($this->http_post($url,$data),1);
        if($res['errmsg'] == 'ok')
        {
            return [
                'status' => 200
            ];
        }else{
            return [
                'status' => 400,
                'errmsg' => '设置失效失败,原因:'.$res['errmsg']
            ];
        }
    }
    //拉取卡券概况数据

    /**
     * 日期格式需要使用 2015-10-10 不可使用时间戳 查询区间需要小于62,同时不可以拉取当天的信息
     * @param string(16) $begin_date 开始时间
     * @param string(16) $end_date 结束时间
     * @param int $cond_source 卡卷来源 0为公众平台创建的卡券数据 1是API创建的卡券数据
     * @return array
     */
    public function getCardBizuinInfo($begin_date,$end_date,$cond_source)
    {
        $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/datacube/getcardbizuininfo?access_token='.session('token.access_token');
        $data = json_encode([
            'begin_date' => $begin_date ,
            'end_date' => $end_date,
            'cond_source' => $cond_source,
        ]);
        $res = json_decode($this->http_post($url,$data),1);
        if(empty($res['errmsg']))
        {
            return [
                'list' => $res['list']
            ];
        }else{
            return [
                'status' => 400,
                'errmsg' => '设置失效失败,原因:'.$res['errmsg']
            ];
        }
    }
    //拉取免费卡卷概况数据

    /**
     * 日期格式需要使用 2015-10-10 不可使用时间戳 查询区间需要小于62,同时不可以拉取当天的信息
     * @param $card_id 卡卷id
     * @param string(16) $begin_date 开始时间
     * @param string(16) $end_date 结束时间
     * @param $cond_source 卡卷来源 0为公众平台创建的卡券数据 1是API创建的卡券数据
     * @return array
     */
    public function getCardCardInfo($card_id,$begin_date,$end_date,$cond_source)
    {
        $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/datacube/getcardcardinfo?access_token='.session('token.access_token');
        $data = json_encode([
            'card_id' => $card_id,
            'begin_date' => $begin_date ,
            'end_date' => $end_date,
            'cond_source' => $cond_source
        ]);
        $res = json_decode($this->http_post($url,$data),1);
        if(empty($res['errmsg']))
        {
            return [
                'status' => 200,
                'list' => $res['list']
            ];
        }else{
            return [
                'status' => 400,
                'errmsg' => '设置失效失败,原因:'.$res['errmsg']
            ];
        }
    }
    //拉取会员卡数据
    public function getCardMemberCardInfo($begin_date,$end_date,$cond_source)
    {
        $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/datacube/getcardmembercardinfo?access_token='.session('token.access_token');
        $data = json_encode([
            'begin_date' => $begin_date ,
            'end_date' => $end_date,
            'cond_source' => $cond_source
        ]);
        $res = json_decode($this->http_post($url,$data),1);
        if(empty($res['errmsg']))
        {
            return [
                'list' => $res['list']
            ];
        }else{
            return [
                'status' => 400,
                'errmsg' => '设置失效失败,原因:'.$res['errmsg']
            ];
        }
    }
    //拉去单张会员卡数据
    public function getCardMemberCardDetail($card_id,$begin_date,$end_date)
    {
        $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/datacube/getcardmembercarddetail?access_token='.session('token.access_token');
        $data = json_encode([
            'begin_date' => $begin_date ,
            'end_date' => $end_date,
            'card_id' => $card_id
        ]);
        $res = json_decode($this->http_post($url,$data),1);
        if(empty($res['errmsg']))
        {
            return [
                'list' => $res['list']
            ];
        }else{
            return [
                'status' => 400,
                'errmsg' => '设置失效失败,原因:'.$res['errmsg']
            ];
        }
    }

}


public function http_post_media($url,$filename){
        $ch = curl_init($url);
        $file = curl_file_create($filename);
        $data = array('media' => $file);
        if(stripos($url,"https://")!==FALSE){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $content = curl_exec($ch);
        $aStatus = curl_getinfo($ch);
        curl_close($ch);
        if(intval($aStatus["http_code"])==200){
            return $content;
        }else{
            return false;
        }
    }
    public function http_post($url,$param,$post_file=false){
        $oCurl = curl_init();
        $this_header = array("content-type: application/x-www-form-urlencoded;charset=UTF-8");
        if(stripos($url,"https://")!==FALSE){
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (is_string($param) || $post_file) {
            $strPOST = $param;
        } else {
            $aPOST = array();
            foreach($param as $key=>$val){
                $aPOST[] = $key."=".urlencode($val);
            }
            $strPOST = join("&", $aPOST);
        }
        curl_setopt($oCurl, CURLOPT_HEADER, 0);
        curl_setopt($oCurl,CURLOPT_HTTPHEADER,$this_header);
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($oCurl, CURLOPT_POST,true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS,$param);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if(intval($aStatus["http_code"])==200){
            return $sContent;
        }else{
            return false;
        }
    }
    function getApiTicket()
    {
        $this->getAccessToken();
        $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.session('token.access_token').'&type=wx_card';
        $res = json_decode($this->http_post($url,''),1);
        if($res['errmsg'] == 'ok')
        {
            return [
                'status' => 200,
                'ticket' => $res['ticket']
            ];
        }else{
            return [
                'status' => 400,
                'errmsg' => $res['errmsg']
            ];
        }
    }
    function getAccessToken()
    {
        if(session()->get('token.access_token')==null || session()->get('token.time')+7100where('id','=',1)->first();
            if($token)
            {
                if( $token->created+7100

你可能感兴趣的:(laravel框架 封装的卡卷管理代码)