QrcodeService.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace ln\services;
  3. use app\common\repositories\system\attachment\AttachmentRepository;
  4. use app\common\repositories\system\merchant\MerchantRepository;
  5. use app\common\repositories\wechat\RoutineQrcodeRepository;
  6. use Endroid\QrCode\QrCode;
  7. use think\facade\Config;
  8. class QrcodeService
  9. {
  10. /**
  11. * 获取二维码
  12. * @param $url
  13. * @param $name
  14. * @return array|bool|string
  15. */
  16. public function getQRCodePath($url, $name)
  17. {
  18. if (!strlen(trim($url)) || !strlen(trim($name))) return false;
  19. try {
  20. $uploadType = systemConfig('upload_type');
  21. //TODO 没有选择默认使用本地上传
  22. if (!$uploadType) $uploadType = 1;
  23. $uploadType = (int)$uploadType;
  24. $siteUrl = systemConfig('site_url');
  25. if (!$siteUrl) return '请前往后台设置->系统设置->网站域名 填写您的域名格式为:http://域名';
  26. $info = [];
  27. $outfile = Config::get('qrcode.cache_dir');
  28. if (!is_dir('./public/' . $outfile))
  29. mkdir('./public/' . $outfile, 0777, true);
  30. $code = new QrCode($url);
  31. $code->writeFile('./public/' . $outfile . '/' . $name);
  32. if ($uploadType === 1) {
  33. $info["code"] = 200;
  34. $info["name"] = $name;
  35. $info["dir"] = rtrim($siteUrl, '/') . '/' . $outfile . '/' . $name;
  36. $info["time"] = time();
  37. $info['size'] = 0;
  38. $info['type'] = 'image/png';
  39. $info["image_type"] = 1;
  40. $info['thumb_path'] = $info["dir"];
  41. return $info;
  42. } else {
  43. $upload = UploadService::create($uploadType);
  44. $res = $upload->to('./public/' . $outfile)->validate()->stream($code->writeString(), $name);
  45. if ($res === false) {
  46. return $upload->getError();
  47. }
  48. $info = $upload->getUploadInfo();
  49. $info['image_type'] = $uploadType;
  50. return $info;
  51. }
  52. } catch (\Exception $e) {
  53. return $e->getMessage();
  54. }
  55. }
  56. /**
  57. * 获取二维码完整路径,不存在则自动生成
  58. * @param string $name 路径名
  59. * @param string $link 需要生成二维码的跳转路径
  60. * @param bool $force 是否返回false
  61. * @return bool|mixed|string
  62. */
  63. public function getWechatQrcodePath(string $name, string $link, bool $force = false, $key = '')
  64. {
  65. try {
  66. $imageInfo = app()->make(AttachmentRepository::class)->getWhere(['attachment_name' => $name]);
  67. if (!$imageInfo) {
  68. $siteUrl = systemConfig('site_url');
  69. $codeUrl = tidy_url($link, null, $siteUrl);
  70. if ($key && systemConfig('open_wechat_share')) {
  71. $qrcode = WechatService::create(false)->qrcodeService();
  72. $codeUrl = $qrcode->forever('_scan_url_' . $key)->url;
  73. }
  74. $imageInfo = $this->getQRCodePath($codeUrl, $name);
  75. if (is_string($imageInfo) && $force)
  76. return false;
  77. if (is_array($imageInfo)) {
  78. $imageInfo['dir'] = tidy_url($imageInfo['dir'], null, $siteUrl);
  79. app()->make(AttachmentRepository::class)->create($imageInfo['image_type'], -1, 0, [
  80. 'attachment_category_id' => 0,
  81. 'attachment_name' => $imageInfo['name'],
  82. 'attachment_src' => $imageInfo['dir']
  83. ]);
  84. $url = $imageInfo['dir'];
  85. } else {
  86. $url = '';
  87. }
  88. } else $url = $imageInfo['attachment_src'];
  89. return $url;
  90. } catch (\Throwable $e) {
  91. if ($force)
  92. return false;
  93. else
  94. return '';
  95. }
  96. }
  97. /**
  98. * 获取小程序分享二维码
  99. * @param int $id
  100. * @param int $uid
  101. * @param int $type 1 = 拼团,2 = 秒杀
  102. * @param array $parame
  103. * @return bool|string
  104. */
  105. public function hotQrcodePath(int $id, int $uid, int $type, array $parame = [])
  106. {
  107. $page = '';
  108. $namePath = '';
  109. $data = 'id=' . $id . '&spid=' . $uid;
  110. switch ($type) {
  111. case 1:
  112. $page = 'pages/activity/goods_combination_details/index';
  113. $namePath = 'combination_' . $id . '_' . $uid . '.jpg';
  114. break;
  115. case 2:
  116. $page = 'pages/activity/goods_seckill_details/index';
  117. $namePath = 'seckill_' . $id . '_' . $uid . '.jpg';
  118. if (isset($parame['stop_time']) && $parame['stop_time']) {
  119. $data .= '&time=' . $parame['stop_time'];
  120. $namePath = $parame['stop_time'] . $namePath;
  121. }
  122. break;
  123. }
  124. if (!$page || !$namePath) {
  125. return false;
  126. }
  127. return $this->getRoutineQrcodePath($namePath, $page, $data);
  128. }
  129. /**
  130. * @param $namePath
  131. * @param $page
  132. * @param $data
  133. * @return bool|int|mixed|string
  134. * @author xaboy
  135. * @day 2020/6/18
  136. */
  137. public function getRoutineQrcodePath($namePath, $page, $data)
  138. {
  139. try {
  140. $imageInfo = app()->make(AttachmentRepository::class)->getWhere(['attachment_name' => $namePath]);
  141. if (!$imageInfo) {
  142. $res = app()->make(RoutineQrcodeRepository::class)->getPageCode($page, $data, 280);
  143. if (!$res) return false;
  144. $uploadType = (int)systemConfig('upload_type') ?: 1;
  145. $upload = UploadService::create($uploadType);
  146. $res = $upload->to('routine/product')->validate()->stream($res, $namePath);
  147. if ($res === false) {
  148. return false;
  149. }
  150. $imageInfo = $upload->getUploadInfo();
  151. $imageInfo['image_type'] = $uploadType;
  152. $imageInfo['dir'] = tidy_url($imageInfo['dir'], 0);
  153. $remoteImage = remoteImage($imageInfo['dir']);
  154. if (!$remoteImage['status']) return false;
  155. app()->make(AttachmentRepository::class)->create($uploadType, -1, 0, [
  156. 'attachment_category_id' => 0,
  157. 'attachment_name' => $imageInfo['name'],
  158. 'attachment_src' => $imageInfo['dir']
  159. ]);
  160. $url = $imageInfo['dir'];
  161. } else $url = $imageInfo['attachment_src'];
  162. return $url;
  163. } catch (\Throwable $e) {
  164. return false;
  165. }
  166. }
  167. }