You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UtilService.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace crmeb\services;
  3. use think\facade\Config;
  4. use Endroid\QrCode\Color\Color;
  5. use Endroid\QrCode\Encoding\Encoding;
  6. use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
  7. use Endroid\QrCode\QrCode;
  8. use Endroid\QrCode\Label\Label;
  9. use Endroid\QrCode\Logo\Logo;
  10. use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
  11. use Endroid\QrCode\Writer\PngWriter;
  12. /**
  13. * Class UtilService
  14. */
  15. class UtilService
  16. {
  17. /**
  18. * 获取小程序二维码是否生成
  19. * @param $url
  20. * @return array
  21. */
  22. public static function remoteImage($url)
  23. {
  24. $curl = curl_init();
  25. curl_setopt($curl, CURLOPT_URL, $url);
  26. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  27. $result = curl_exec($curl);
  28. $result = json_decode($result, true);
  29. if (is_array($result)) return ['status' => false, 'msg' => $result['errcode'] . '---' . $result['errmsg']];
  30. return ['status' => true];
  31. }
  32. /**
  33. * 修改 https 和 http 移动到common
  34. * @param $url $url 域名
  35. * @param int $type 0 返回https 1 返回 http
  36. * @return string
  37. */
  38. public static function setHttpType($url, $type = 0)
  39. {
  40. $domainTop = substr($url, 0, 5);
  41. if ($type) {
  42. if ($domainTop == 'https') $url = 'http' . substr($url, 5, strlen($url));
  43. } else {
  44. if ($domainTop != 'https') $url = 'https:' . substr($url, 5, strlen($url));
  45. }
  46. return $url;
  47. }
  48. /**
  49. * 获取二维码
  50. * @param $url
  51. * @param $name
  52. * @return array|bool|string
  53. */
  54. public static function getQRCodePath($url, $name)
  55. {
  56. if (!strlen(trim($url)) || !strlen(trim($name))) return false;
  57. try {
  58. $uploadType = sys_config('upload_type');
  59. // 没有选择默认使用本地上传
  60. if (!$uploadType) $uploadType = 1;
  61. $uploadType = (int)$uploadType;
  62. $siteUrl = sys_config('site_url');
  63. if (!$siteUrl) return '请前往后台设置->系统设置->网站域名 填写您的域名格式为:http://域名';
  64. $info = [];
  65. $outfiles = Config::get('qrcode.cache_dir');
  66. $root_outfile = root_path('public/' . $outfiles);
  67. if (!is_dir($root_outfile))
  68. mkdir($root_outfile, 0777, true);
  69. // Create QR code
  70. $writer = new PngWriter();
  71. $qrCode = QrCode::create($url)
  72. ->setEncoding(new Encoding('UTF-8'))
  73. ->setErrorCorrectionLevel(new ErrorCorrectionLevelLow())
  74. ->setSize(300)
  75. ->setMargin(10)
  76. ->setRoundBlockSizeMode(new RoundBlockSizeModeMargin())
  77. ->setForegroundColor(new Color(0, 0, 0))
  78. ->setBackgroundColor(new Color(255, 255, 255));
  79. $writer->write($qrCode)->saveToFile($root_outfile . $name);
  80. if ($uploadType === 1) {
  81. $info["code"] = 200;
  82. $info["name"] = $name;
  83. $info["dir"] = '/' . $outfiles . '/' . $name;
  84. $info["time"] = time();
  85. $info['size'] = 0;
  86. $info['type'] = 'image/png';
  87. $info["image_type"] = 1;
  88. $info['thumb_path'] = '/' . $outfiles . '/' . $name;
  89. return $info;
  90. } else {
  91. $upload = UploadService::init($uploadType);
  92. $content = file_get_contents($root_outfile . $name);
  93. $res = $upload->to($outfiles)->validate()->setAuthThumb(false)->stream($content, $name);
  94. if ($res === false) {
  95. return $upload->getError();
  96. }
  97. $info = $upload->getUploadInfo();
  98. $info['image_type'] = $uploadType;
  99. return $info;
  100. }
  101. } catch (\Exception $e) {
  102. return $e->getMessage();
  103. }
  104. }
  105. }