swoft 获取微信零时二维码 并上传阿里云oss aliyun-oss

1、获取access_token、两个小时门票过期重新获取

    public function AccessToken(){
        $time = time();

        $key = "wx68065208096access_token";

        $accessData = DB::table('db_wx_token')->where('key',$key)->value('token');

         $data = json_decode($accessData,true);

        if ($time < ($data['expire_time']+7000)) {

            $accessToken = $data['access_token'];

        }else{
            $URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$secret";

            $res = HttpCurlGet($URL);

            $Arr = ['expire_time'=>$time,'access_token'=>$res['access_token']];

            DB::table('db_wx_token')->where('key','wx68065208096access_token')->update(['token'=>json_encode($Arr,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT)]);

            $accessToken = $res['access_token'];
        }

        return $accessToken;
    }

2、调用接口获取零时二维码、零时二维码有效时长7天

/**
 * 获取商品零时二维码--带用户参数
*/ 
    public function getZeroTimeQRcode($user_id, $goods_id){
        $admin_id = DB::table('db_user_salses')->where('uid', $user_id)->value('adminid');
$WeChat = new WeChat();

        $AccessToken = $WeChat->AccessToken();

        $url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$AccessToken;

     $post_data = ['path'=>'pages/good/goodDetail?user_id='.$user_id.'&goods_id='.$goods_id.'&admin_id='.$admin_id,'width'=>140]; //二维码带上参数和二维码的尺寸要求
$data = json_encode($post_data); $result = HttpCurlPost($url, $data, 30); $Upload = new Upload(); $qrcodeUrl = $Upload->ZeroTimeQRcodeImageUpload($result); //上传到阿里云OSS并返回结果 return $qrcodeUrl; }

3、阿里云OSS

/**
 *  图片上传阿里云OSS
 *  图片文件直接上传
 */
    public function ZeroTimeQRcodeImageUpload($image){
        $ossClient = new OssClient($config['KeyId'], $config['KeySecret'], $config['Endpoint']);
            
        $fileName = "ZeroTimeQRcode/".date('Ymd')."/".sha1(date('YmdHis', time()) . uniqid()) .'.png';//零时二维码到阿里云OSS空间下指定的ZeroTimeQRcode/文件下并创建日期文件夹下保存文件

        $result = $ossClient->putObject($config['Bucket'], $fileName, $image); //微信零时二维码接口返回的是对象,所以不能用$ossClient->uploadFile

        if ($result['info']['http_code'] == 200) {
            $msg = "上传成功!";
        }else{
            $msg = "未知错误!";
        }

        $res = Result($result['info']['http_code'], $msg, $fileName);
        
        return $res;
    }

 

 完成~!!!

posted @ 2020-03-12 17:41  黑锦鲤  阅读(375)  评论(0编辑  收藏  举报