QrcodeService.php 7.5 KB

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