QrcodeService.php 7.6 KB

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