| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- /**
- * @link:http://www.gdqijianshi.com/
- * @copyright: Copyright (c) 2020 广东七件事集团
- * Created by PhpStorm
- * Author: ganxiaohao
- * Date: 2020-04-02
- * Time: 11:22
- */
- namespace common\helpers;
- use common\components\export\CsvTool;
- use common\enums\RabbitMqEnum;
- use common\models\common\Attachment;
- use common\models\common\PosterCode;
- class MiniQrCode
- {
- public function getAppletQrCode($mall_id,$page, $scene)
- {
- $page = ltrim($page, '/');
- $url = '';
- /** @var Wechat $wechat */
- \Yii::$app->params['wechatMiniProgramConfig'] = \Yii::$app->services->setting->mall->get($mall_id, 'mini_program');
- $miniProgram = \Yii::$app->wechat->miniProgram;
- $response = $miniProgram->app_code->getUnlimit($scene, ['width' => 200, 'page' => $page]);
- if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
- if (in_array('image/jpeg', $response->getHeader('Content-Type'))) {
- //返回base64图片
- $content = 'data:image/jpg;base64,'. base64_encode($response->getBody()->getContents());
- $path = \Yii::getAlias('@attachment') . '/download/qrcode';
- if (!file_exists($path)) {// 异步生成小程序图片文件夹
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, ['path' => $path], PosterCode::class, 'mkdirQrcode');
- }
- $fileName = $this->base64_image_content($content, $path);
- if ($fileName == false){
- \Yii::error('getAppletQrCode:文件名称不存在');
- return false;
- }
- // 图片上传到云存储
- $csv = new CsvTool();
- $url = $csv->upload($mall_id, 'download/qrcode/' . $fileName);
- $config = \Yii::$app->services->attachment->getStorageConfig($mall_id);
- if ($config['drive'] != Attachment::DRIVE_LOCAL) unlink($path .'/'. $fileName);// 删除临时存储的文件
- } else {
- //返回文字
- \Yii::error('getAppletQrCode:获取失败:' . $response->getBody()->getContents());
- return false;
- }
- }else{
- \Yii::error('getAppletQrCode:'.$response['errmsg']);
- return false;
- }
- return $url;
- }
- function base64_image_content($base64_image_content,$path, $fileName = ''){
- try {
- //匹配出图片的格式
- if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
- $type = $result[2];
- if(!file_exists($path)){
- //检查是否有该文件夹,如果没有就创建,并给予最高权限
- mkdir($path, 0777);
- }
- $fileName = !empty($fileName) ? $fileName : md5(microtime(true));
- if (file_put_contents($path . '/' . $fileName. ".{$type}", base64_decode(str_replace($result[1],'', $base64_image_content)))){
- return $fileName . ".{$type}";
- }else{
- return false;
- }
- }else{
- return false;
- }
- } catch (\Exception $e) {
- return false;
- }
- }
- }
|