zoukankan      html  css  js  c++  java
  • 微信小程序自定义券票二维码文件流上传到OSS解决方法

    前言
            目前项目中有这样一下需求,通过传入的不同的参数生成不同渠道的微信小程序二维码,从而统计各大平台从小程序引流的数据。而旧系统是先通过接口生成二维码后先是保存至当前服务器上后,拿到图片路径和文件信息然后再使用OSS的SDK上传到存储桶。可能是因为生成的二维码是文件流,所以以前的人是通过file_put_contents函数做的保存再使用SDK的文件上传接口。

    思路
          为了去掉保存至服务器这一步,所以就不能使用文件上传。而目前大平台的存储服务一般都不止提供一种方式的上传,所以找到了文档发现了一个“字符串上传”。也就是当调用微信接口返回的二维码文件流(一堆字符串),然后通过该SDK配置路径和文件就可以实现小程序码的上传了。

    流程
    1. 下载OSS的SDK,可以去其官网也可以composer require aliyuncs/oss-sdk-php

    2.  配置参数封装其字符串上传方法等

    3.  封装二维码生成方法

    4.  实现上传后执行业务操作

    编码

    1. 文件流上传部分

    /*
     *@title 单文件流上传
     *@param string $content 文件流字符串
     *@param string $file_name 存储位置/文件名(包括文件后缀)
     *@return object
    */
    public function objectUpload($content, $file_name='')
    {
    	$config = $this->config;
    	$res['code'] = 1;
    	$res['message'] = '';
    	try{
    		$ossClient = new OssClient($config['AccessKeyID'], $config['AccessKeySecret'], $config['EndPoint']);
    
    		$result = $ossClient->putObject($config['Bucket'], $file_name, $content);
    
    		$res['data']['url'] = $result['info']['url'];
    		$res['data']['path'] = explode("/",$file_name)[1];
    
    	} catch(OssException $e) {
    		$res['code'] = 0;
    		$res['message'] = $e->getMessage();
    	}
    	return $res;
    }

    2. 公用文件流上传函数部分

    error_reporting(E_ALL ^ E_NOTICE);
    
    if (!function_exists('oss_object_upload')) {
        function oss_object_upload($content,$file_name)
        {
    
            $oss = ossOssFactory::factory("AliOss");
    
            $result = $oss->objectUpload($content,$file_name);
    
            return $result;
        }
    }

    3. 二维码生成部分

    <?php
    
    namespace appcommonlibrary;
    
    use FirebaseJWTJWT as FirebaseJWT;
    use ossOssFactory;
    use thinkDb;
    use thinkSession;
    use thinkRequest;
    use thinkLoader;
    use thinkfacadeConfig;
    use GuzzleHttpClient;
    
    
    class Wechat{
    
    	protected static $instance = null;
    	protected $_error = "";
    	protected $_errorMsg = "";
    	protected $config;
    
    	public function __construct($type){
            $this->config = Config::pull($type);	// 获取配置参数
        }
    
        /*
         * @title 获取对象
         * @author beiqiaosu
         * @param string $type (mini:小程序配置)
         * @return null| object
         * */
    	public static function getInstance($type='mini')
        {
    		if (!self::$instance) {
    		    self::$instance = new self($type);
    		}
    
    		return self::$instance;
    	}
    
    	// 请求微信接口凭证access_token
    	public function getAccessToken()
        {
            $res = false;
            if(!$this->config) {
                $this->_error = '请求配置不存在';
                $this->_errorMsg = '接口错误';
            }
    
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->config['appId']}&secret={$this->config['appSecret']}";
    
            $client = new Client();
            $response = $client->request('GET', $url, ['verify' => false]);
            $res = $response->getBody()->getContents();
            $result = json_decode(stripcslashes($res), true);
    
            if (!isset($result['errcode']) || $result['errcode'] == 0) {
                // 获取成功
                $res = $result['access_token'];
            } else {
                $this->_error = $result['errmsg'];
                $this->_errorMsg = '接口错误';
            }
            return $res;
        }
    
        // 生成小程序自定义二维码
        public function getQrcode($scene)
        {
            $res = false;
    
            if(empty($scene)) {
                $this->_error = 'scene参数不存在';
                $this->_errorMsg = '接口出错';
            }
    
            $accrssToken = $this->getAccessToken();
    //        if(!$accrssToken) return $res;
    
            $data = array(
                "scene" => "{$scene}"
            );
    
            $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accrssToken}";
    
    //        $client = new Client(array(
    //            'content-type' => 'application/json'
    //        ));
    //        $paramArr = json_encode($data);
    //        $response = $client->post($url, [
    //            GuzzleHttpRequestOptions::JSON => $data
    //        ]);
    //        $response = $client->request('POST', $url, ['json'=>$paramArr]);
    //        $res = $response->getBody()->getContents();
    //        $result = json_decode(stripcslashes($res), true);
    
            $returnData = self::httpsRequest($url, $data, 'json');
    
            if (!isset($returnData['errcode']) || $returnData['errcode'] == 0) {
                $res = $returnData;
            }else {
                $this->_error = $returnData['errmsg'];
                $this->_errorMsg = '接口出错';
            }
    
            return $res;
        }
    
        // cURL POST请求方法(暂放)
        public static function httpsRequest($url, $data, $type)
        {
            if ($type == 'json') {
                $headers = array("Content-type: application/json;charset=UTF-8", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache");
                $data = json_encode($data);
            }
    
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
            if (!empty($data)) {
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            $output = curl_exec($curl);
    
            curl_close($curl);
            return $output;
        }
    
    	public function getError()
        {
    		return $this->_error;
    	}
    
    	public function getErrorMsg()
        {
    		return $this->_errorMsg;
    	}
    
        final protected function __clone()
        {
    
        }
    
        public function __call($name, $arguments)
        {
            // TODO: Implement __call() method.
        }
    
    }

    4. 开始上传的业务实现部分。

    // 获取二维码文件流
    $ret = appcommonlibraryWechat::getInstance()->getQrcode("activty".$activtyId);
    if(!$ret) {
    	$res['code'] = 400;
    	$res['msg'] = appcommonlibraryWechat::getInstance()->getError();
    	return $res;
    }
    
    $path = "qrcode/";     // 二维码文件流存放位置
    $fileName = time() . rand(1000,9999) . ".png";
    $result = oss_object_upload($ret,$path.$fileName);
    
    // 文件流上传失败
    if(!$result['code']) {
    	$res['code'] = 400;
    	$res['msg'] = $result['message'];
    	return $res;
    }
    
    // 将二维码图片放入到引流配置中
    $data = [
    	"id" => $activtyId,
    	"img" => $result['data']['url']
    ];
    
    $upRes = Db::name("activity")->update($data);

    说明

        以上代码是部分,所以需要自己根据业务整理修改合理的采取,需要封装的类也可以在公众号回复 "文件流OSS" 获取。

  • 相关阅读:
    如何在Oracle官网下载java的JDK最新版本和历史版本
    屏幕录制专家【Bandicam】
    Bandicam下载 + 破解
    华为荣耀7i手动更改DNS,提高网页加载速度
    SQL中使用GROUP BY注意事项
    JavaScript数据类型判断
    React.lazy和Suspense组合实现组件懒加载
    使用React+TypeScript构建自己的组件库
    leetcode-0101 对称二叉树
    leetcode-0543 二叉树的直径
  • 原文地址:https://www.cnblogs.com/zerofc/p/12783283.html
Copyright © 2011-2022 走看看