QrcodeService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services;
  12. use app\services\system\attachment\SystemAttachmentServices;
  13. use crmeb\services\wechat\MiniProgram;
  14. use GuzzleHttp\Psr7\Utils;
  15. /**
  16. * Class QrcodeService
  17. * @package crmeb\services
  18. */
  19. class QrcodeService
  20. {
  21. /**
  22. * 获取二维码完整路径,不存在则自动生成
  23. * @param string $name 路径名
  24. * @param string $link 需要生成二维码的跳转路径
  25. * @param int $type https 1 = http , 0 = https
  26. * @param bool $force 是否返回false
  27. * @return bool|mixed|string
  28. */
  29. public static function getWechatQrcodePath(string $name, string $link, bool $force = false)
  30. {
  31. /** @var SystemAttachmentServices $systemAttchment */
  32. $systemAttchment = app()->make(SystemAttachmentServices::class);
  33. try {
  34. $imageInfo = $systemAttchment->getInfo(['name' => $name]);
  35. $siteUrl = sys_config('site_url');
  36. if (!$imageInfo) {
  37. $codeUrl = UtilService::setHttpType($siteUrl . $link, request()->isSsl() ? 0 : 1);//二维码链接
  38. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  39. if (is_string($imageInfo) && $force)
  40. return false;
  41. if (is_array($imageInfo)) {
  42. $systemAttchment->attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  43. $url = $imageInfo['dir'];
  44. } else {
  45. $url = '';
  46. $imageInfo = ['image_type' => 0];
  47. }
  48. } else $url = $imageInfo['att_dir'];
  49. if ($imageInfo['image_type'] == 1 && $url) $url = $siteUrl . $url;
  50. return $url;
  51. } catch (\Throwable $e) {
  52. if ($force)
  53. return false;
  54. else
  55. return '';
  56. }
  57. }
  58. /**
  59. * 获取小程序分享二维码
  60. * @param int $id
  61. * @param int $uid
  62. * @param int $type 1 = 拼团,2 = 秒杀
  63. * @return bool|string
  64. */
  65. public static function getRoutineQrcodePath(int $id, int $uid, int $type, array $parame = [])
  66. {
  67. $page = '';
  68. $namePath = '';
  69. $data = 'id=' . $id . '&spid=' . $uid;
  70. switch ($type) {
  71. case 1:
  72. $page = 'pages/activity/goods_combination_details/index';
  73. $namePath = 'combination_' . $id . '_' . $uid . '.jpg';
  74. break;
  75. case 2:
  76. $page = 'pages/activity/goods_seckill_details/index';
  77. $namePath = 'seckill_' . $id . '_' . $uid . '.jpg';
  78. if (isset($parame['stop_time']) && $parame['stop_time']) {
  79. $data .= '&time=' . $parame['stop_time'];
  80. $namePath = $parame['stop_time'] . $namePath;
  81. }
  82. break;
  83. }
  84. if (!$page || !$namePath) {
  85. return false;
  86. }
  87. /** @var SystemAttachmentServices $systemAttchment */
  88. $systemAttchment = app()->make(SystemAttachmentServices::class);
  89. try {
  90. $imageInfo = $systemAttchment->getInfo($namePath, 'name');
  91. $siteUrl = sys_config('site_url');
  92. if (!$imageInfo) {
  93. $res = MiniProgram::appCodeUnlimit($page, $data, 280);
  94. if (!$res) return false;
  95. $uploadType = (int)sys_config('upload_type', 1);
  96. $upload = UploadService::init($uploadType);
  97. $res = (string)Utils::streamFor($res);
  98. $res = $upload->to('routine/product')->validate()->stream($res, $namePath);
  99. if ($res === false) {
  100. return false;
  101. }
  102. $imageInfo = $upload->getUploadInfo();
  103. $imageInfo['image_type'] = $uploadType;
  104. if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
  105. else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
  106. if (!$remoteImage['status']) return false;
  107. $systemAttchment->attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  108. $url = $imageInfo['dir'];
  109. } else $url = $imageInfo['att_dir'];
  110. if ($imageInfo['image_type'] == 1) $url = $siteUrl . $url;
  111. return $url;
  112. } catch (\Throwable $e) {
  113. return false;
  114. }
  115. }
  116. }