• 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;
        }

     完成~!!!

  • 相关阅读:
    JS 原型模式 工厂模式 构造函数的区别
    JS 深入1
    理解DOM的一个例子
    Fuzzing参数
    神经网络相关知识和概念整理
    [转载] 系统、模型和仿真
    frp内网穿透,从外网访问内网资源
    常用软件配置
    141. 环形链表
    501. 二叉搜索树中的众数
  • 原文地址:https://www.cnblogs.com/heijinli/p/12470721.html
Copyright © 2020-2023  润新知