QrcodeService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace liuniu;
  3. use app\admin\model\wechat\WechatQrcode as QrcodeModel;
  4. use app\common\model\Attachment;
  5. use think\Request;
  6. class QrcodeService
  7. {
  8. /**
  9. * 获取临时二维码 单个
  10. * @param $type
  11. * @param $id
  12. * @return array
  13. */
  14. public static function getTemporaryQrcode($type, $id)
  15. {
  16. return QrcodeModel::getTemporaryQrcode($type, $id)->toArray();
  17. }
  18. /**
  19. * 获取永久二维码 单个
  20. * @param $type
  21. * @param $id
  22. * @return array
  23. */
  24. public static function getForeverQrcode($type, $id)
  25. {
  26. return QrcodeModel::getForeverQrcode($type, $id)->toArray();
  27. }
  28. /**
  29. * 从数据库获取二维码
  30. * @param $id
  31. * @param string $type
  32. * @return array|\think\Model|null
  33. */
  34. public static function getQrcode($id, $type = 'id')
  35. {
  36. return QrcodeModel::getQrcode($id, $type);
  37. }
  38. /**
  39. * 增加某个二维码扫描的次数
  40. * @param $id
  41. * @param string $type
  42. * @return mixed
  43. */
  44. public static function scanQrcode($id, $type = 'id')
  45. {
  46. return QrcodeModel::scanQrcode($id, $type);
  47. }
  48. /**
  49. * 获取二维码完整路径,不存在则自动生成
  50. * @param string $name 路径名
  51. * @param string $link 需要生成二维码的跳转路径
  52. * @param int $type https 1 = http , 0 = https
  53. * @param bool $force 是否返回false
  54. * @return bool|mixed|string
  55. */
  56. public static function getWechatQrcodePath(string $name, string $link, int $type = 1, bool $force = false)
  57. {
  58. try {
  59. $imageInfo = Attachment::getInfo($name, 'name');
  60. $siteUrl = Request::instance()->domain();
  61. if (!$imageInfo) {
  62. $codeUrl = UtilService::setHttpType($siteUrl . $link, $type);//二维码链接
  63. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  64. if (is_string($imageInfo) && $force)
  65. return false;
  66. if (is_array($imageInfo)) {
  67. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  68. $url = $imageInfo['dir'];
  69. } else {
  70. $url = '';
  71. $imageInfo = ['image_type' => 0];
  72. }
  73. } else $url = $imageInfo['att_dir'];
  74. if ($imageInfo['image_type'] == 1 && $url) $url = $siteUrl . $url;
  75. return $url;
  76. } catch (\Throwable $e) {
  77. if ($force)
  78. return false;
  79. else
  80. return '';
  81. }
  82. }
  83. /**
  84. * 获取二维码
  85. * @param $url
  86. * @param $name
  87. * @return array|bool|string
  88. */
  89. public static function getQRCodePath($url, $name)
  90. {
  91. if (!strlen(trim($url)) || !strlen(trim($name))) return false;
  92. try {
  93. $uploadType = 1;
  94. //TODO 没有选择默认使用本地上传
  95. if (!$uploadType) $uploadType = 1;
  96. $uploadType = (int)$uploadType;
  97. $siteUrl = sys_config('site_url');
  98. if (!$siteUrl) return '请前往后台设置->系统设置->网站域名 填写您的域名格式为:http://域名';
  99. $info = [];
  100. $outfile = Config::get('qrcode.cache_dir');
  101. $code = new QRcode();
  102. $wapCodePath = $code->png($url, $outfile . '/' . $name)->getPath(); //获取二维码生成的地址
  103. $content = file_get_contents('.' . $wapCodePath);
  104. if ($uploadType === 1) {
  105. $info["code"] = 200;
  106. $info["name"] = $name;
  107. $info["dir"] = $wapCodePath;
  108. $info["time"] = time();
  109. $info['size'] = 0;
  110. $info['type'] = 'image/png';
  111. $info["image_type"] = 1;
  112. $info['thumb_path'] = $wapCodePath;
  113. return $info;
  114. } else {
  115. $upload = new Upload($uploadType, [
  116. 'accessKey' => sys_config('accessKey'),
  117. 'secretKey' => sys_config('secretKey'),
  118. 'uploadUrl' => sys_config('uploadUrl'),
  119. 'storageName' => sys_config('storage_name'),
  120. 'storageRegion' => sys_config('storage_region'),
  121. ]);
  122. $res = $upload->to($outfile)->validate()->stream($content, $name);
  123. if ($res === false) {
  124. return $upload->getError();
  125. }
  126. $info = $upload->getUploadInfo();
  127. $info['image_type'] = $uploadType;
  128. return $info;
  129. }
  130. } catch (\Exception $e) {
  131. return $e->getMessage();
  132. }
  133. }
  134. }