MiniQrCode.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * @link:http://www.gdqijianshi.com/
  4. * @copyright: Copyright (c) 2020 广东七件事集团
  5. * Created by PhpStorm
  6. * Author: ganxiaohao
  7. * Date: 2020-04-02
  8. * Time: 11:22
  9. */
  10. namespace common\helpers;
  11. use common\components\export\CsvTool;
  12. use common\enums\RabbitMqEnum;
  13. use common\models\common\Attachment;
  14. use common\models\common\PosterCode;
  15. class MiniQrCode
  16. {
  17. public function getAppletQrCode($mall_id,$page, $scene)
  18. {
  19. $page = ltrim($page, '/');
  20. $url = '';
  21. /** @var Wechat $wechat */
  22. \Yii::$app->params['wechatMiniProgramConfig'] = \Yii::$app->services->setting->mall->get($mall_id, 'mini_program');
  23. $miniProgram = \Yii::$app->wechat->miniProgram;
  24. $response = $miniProgram->app_code->getUnlimit($scene, ['width' => 200, 'page' => $page]);
  25. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  26. if (in_array('image/jpeg', $response->getHeader('Content-Type'))) {
  27. //返回base64图片
  28. $content = 'data:image/jpg;base64,'. base64_encode($response->getBody()->getContents());
  29. $path = \Yii::getAlias('@attachment') . '/download/qrcode';
  30. if (!file_exists($path)) {// 异步生成小程序图片文件夹
  31. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, ['path' => $path], PosterCode::class, 'mkdirQrcode');
  32. }
  33. $fileName = $this->base64_image_content($content, $path);
  34. if ($fileName == false){
  35. \Yii::error('getAppletQrCode:文件名称不存在');
  36. return false;
  37. }
  38. // 图片上传到云存储
  39. $csv = new CsvTool();
  40. $url = $csv->upload($mall_id, 'download/qrcode/' . $fileName);
  41. $config = \Yii::$app->services->attachment->getStorageConfig($mall_id);
  42. if ($config['drive'] != Attachment::DRIVE_LOCAL) unlink($path .'/'. $fileName);// 删除临时存储的文件
  43. } else {
  44. //返回文字
  45. \Yii::error('getAppletQrCode:获取失败:' . $response->getBody()->getContents());
  46. return false;
  47. }
  48. }else{
  49. \Yii::error('getAppletQrCode:'.$response['errmsg']);
  50. return false;
  51. }
  52. return $url;
  53. }
  54. function base64_image_content($base64_image_content,$path, $fileName = ''){
  55. try {
  56. //匹配出图片的格式
  57. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
  58. $type = $result[2];
  59. if(!file_exists($path)){
  60. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  61. mkdir($path, 0777);
  62. }
  63. $fileName = !empty($fileName) ? $fileName : md5(microtime(true));
  64. if (file_put_contents($path . '/' . $fileName. ".{$type}", base64_decode(str_replace($result[1],'', $base64_image_content)))){
  65. return $fileName . ".{$type}";
  66. }else{
  67. return false;
  68. }
  69. }else{
  70. return false;
  71. }
  72. } catch (\Exception $e) {
  73. return false;
  74. }
  75. }
  76. }