QrcodeService.php 7.2 KB

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