QrcodeService.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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)
  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. $imageInfo = $this->getQRCodePath($codeUrl, $name);
  80. if (is_string($imageInfo) && $force)
  81. return false;
  82. if (is_array($imageInfo)) {
  83. $imageInfo['dir'] = tidy_url($imageInfo['dir'], null, $siteUrl);
  84. app()->make(AttachmentRepository::class)->create($imageInfo['image_type'], -1, 0, [
  85. 'attachment_category_id' => 0,
  86. 'attachment_name' => $imageInfo['name'],
  87. 'attachment_src' => $imageInfo['dir']
  88. ]);
  89. $url = $imageInfo['dir'];
  90. } else {
  91. $url = '';
  92. }
  93. } else $url = $imageInfo['attachment_src'];
  94. return $url;
  95. } catch (\Throwable $e) {
  96. if ($force)
  97. return false;
  98. else
  99. return '';
  100. }
  101. }
  102. /**
  103. * 获取小程序分享二维码
  104. * @param int $id
  105. * @param int $uid
  106. * @param int $type 1 = 拼团,2 = 秒杀
  107. * @param array $parame
  108. * @return bool|string
  109. */
  110. public function hotQrcodePath(int $id, int $uid, int $type, array $parame = [])
  111. {
  112. $page = '';
  113. $namePath = '';
  114. $data = 'id=' . $id . '&spid=' . $uid;
  115. switch ($type) {
  116. case 1:
  117. $page = 'pages/activity/goods_combination_details/index';
  118. $namePath = 'combination_' . $id . '_' . $uid . '.jpg';
  119. break;
  120. case 2:
  121. $page = 'pages/activity/goods_seckill_details/index';
  122. $namePath = 'seckill_' . $id . '_' . $uid . '.jpg';
  123. if (isset($parame['stop_time']) && $parame['stop_time']) {
  124. $data .= '&time=' . $parame['stop_time'];
  125. $namePath = $parame['stop_time'] . $namePath;
  126. }
  127. break;
  128. }
  129. if (!$page || !$namePath) {
  130. return false;
  131. }
  132. return $this->getRoutineQrcodePath($namePath, $page, $data);
  133. }
  134. /**
  135. * @param $namePath
  136. * @param $page
  137. * @param $data
  138. * @return bool|int|mixed|string
  139. * @author xaboy
  140. * @day 2020/6/18
  141. */
  142. public function getRoutineQrcodePath($namePath, $page, $data)
  143. {
  144. try {
  145. $imageInfo = app()->make(AttachmentRepository::class)->getWhere(['attachment_name' => $namePath]);
  146. if (!$imageInfo) {
  147. $res = app()->make(RoutineQrcodeRepository::class)->getPageCode($page, $data, 280);
  148. if (!$res) return false;
  149. $uploadType = (int)systemConfig('upload_type') ?: 1;
  150. $upload = UploadService::create($uploadType);
  151. $res = $upload->to('routine/product')->validate()->stream($res, $namePath);
  152. if ($res === false) {
  153. return false;
  154. }
  155. $imageInfo = $upload->getUploadInfo();
  156. $imageInfo['image_type'] = $uploadType;
  157. $imageInfo['dir'] = tidy_url($imageInfo['dir'], 0);
  158. $remoteImage = remoteImage($imageInfo['dir']);
  159. if (!$remoteImage['status']) return false;
  160. app()->make(AttachmentRepository::class)->create($uploadType, -1, 0, [
  161. 'attachment_category_id' => 0,
  162. 'attachment_name' => $imageInfo['name'],
  163. 'attachment_src' => $imageInfo['dir']
  164. ]);
  165. $url = $imageInfo['dir'];
  166. } else $url = $imageInfo['attachment_src'];
  167. return $url;
  168. } catch (\Throwable $e) {
  169. return false;
  170. }
  171. }
  172. }