QrcodeService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace library\services;
  3. use app\model\routine\RoutineCode;
  4. use app\model\system\Attachment;
  5. use app\model\wechat\WechatQrcode as QrcodeModel;
  6. class QrcodeService
  7. {
  8. /**
  9. * 获取临时二维码 单个
  10. * */
  11. public static function getTemporaryQrcode($type, $id)
  12. {
  13. return QrcodeModel::getTemporaryQrcode($type, $id)->toArray();
  14. }
  15. /**
  16. * 获取永久二维码 单个
  17. * */
  18. public static function getForeverQrcode($type, $id)
  19. {
  20. return QrcodeModel::getForeverQrcode($type, $id)->toArray();
  21. }
  22. public static function getQrcode($id, $type = 'id')
  23. {
  24. return QrcodeModel::getQrcode($id, $type);
  25. }
  26. public static function scanQrcode($id, $type = 'id')
  27. {
  28. return QrcodeModel::scanQrcode($id, $type);
  29. }
  30. /**
  31. * 获取二维码完整路径,不存在则自动生成
  32. * @param string $name 路径名
  33. * @param string $link 需要生成二维码的跳转路径
  34. * @param int $type https 1 = http , 0 = https
  35. * @param bool $force 是否返回false
  36. * @return bool|mixed|string
  37. */
  38. public static function getWechatQrcodePath(string $name, string $link, bool $force = false)
  39. {
  40. try {
  41. $imageInfo = Attachment::getInfo($name, 'name');
  42. $siteUrl = config('app')['API_URL'];
  43. if (!$imageInfo) {
  44. $codeUrl = UtilService::setHttpType($siteUrl . $link, request()->isSsl() ? 0 : 1);//二维码链接
  45. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  46. if (is_string($imageInfo) && $force)
  47. return false;
  48. if (is_array($imageInfo)) {
  49. Attachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  50. $url = $imageInfo['dir'];
  51. } else {
  52. $url = '';
  53. $imageInfo = ['image_type' => 0];
  54. }
  55. } else $url = $imageInfo['att_dir'];
  56. if ($imageInfo['image_type'] == 1 && $url) $url = $siteUrl . $url;
  57. return $url;
  58. } catch (\Throwable $e) {
  59. if ($force)
  60. return false;
  61. else
  62. return '';
  63. }
  64. }
  65. /**
  66. * 获取小程序分享二维码
  67. * @param int $id
  68. * @param int $uid
  69. * @param int $type 1 = 拼团,2 = 秒杀
  70. * @return bool|string
  71. */
  72. public static function getRoutineQrcodePath(int $id, int $uid, int $type, array $parame = [])
  73. {
  74. $page = '';
  75. $namePath = '';
  76. $data = 'id=' . $id . '&pid=' . $uid;
  77. switch ($type) {
  78. case 1:
  79. $page = 'pages/activity/goods_combination_details/index';
  80. $namePath = 'combination_' . $id . '_' . $uid . '.jpg';
  81. break;
  82. case 2:
  83. $page = 'pages/activity/goods_seckill_details/index';
  84. $namePath = 'seckill_' . $id . '_' . $uid . '.jpg';
  85. if (isset($parame['stop_time']) && $parame['stop_time']) {
  86. $data .= '&time=' . $parame['stop_time'];
  87. $namePath = $parame['stop_time'] . $namePath;
  88. }
  89. break;
  90. }
  91. if (!$page || !$namePath) {
  92. return false;
  93. }
  94. try {
  95. $imageInfo = Attachment::getInfo($namePath, 'name');
  96. $siteUrl = config('app')['API_URL'];
  97. if (!$imageInfo) {
  98. $res = RoutineCode::getPageCode($page, $data, 280);
  99. if (!$res) return false;
  100. $uploadType = (int)sys_config('upload_type', 1);
  101. $upload = UploadService::init();
  102. $res = $upload->to('routine/product')->validate()->stream($res, $namePath);
  103. if ($res === false) {
  104. return false;
  105. }
  106. $imageInfo = $upload->getUploadInfo();
  107. $imageInfo['image_type'] = $uploadType;
  108. if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
  109. else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
  110. if (!$remoteImage['status']) return false;
  111. Attachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  112. $url = $imageInfo['dir'];
  113. } else $url = $imageInfo['att_dir'];
  114. if ($imageInfo['image_type'] == 1) $url = $siteUrl . $url;
  115. return $url;
  116. } catch (\Throwable $e) {
  117. return false;
  118. }
  119. }
  120. }