UtilService.php 4.5 KB

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