QrcodeService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/10/24
  6. */
  7. namespace crmeb\services;
  8. use app\models\routine\RoutineCode;
  9. use app\models\system\SystemAttachment;
  10. use app\models\wechat\WechatQrcode as QrcodeModel;
  11. class QrcodeService
  12. {
  13. /**
  14. * 获取临时二维码 单个
  15. * */
  16. public static function getTemporaryQrcode($type, $id)
  17. {
  18. return QrcodeModel::getTemporaryQrcode($type, $id)->toArray();
  19. }
  20. /**
  21. * 获取永久二维码 单个
  22. * */
  23. public static function getForeverQrcode($type, $id)
  24. {
  25. return QrcodeModel::getForeverQrcode($type, $id)->toArray();
  26. }
  27. public static function getQrcode($id, $type = 'id')
  28. {
  29. return QrcodeModel::getQrcode($id, $type);
  30. }
  31. public static function scanQrcode($id, $type = 'id')
  32. {
  33. return QrcodeModel::scanQrcode($id, $type);
  34. }
  35. /**
  36. * 获取二维码完整路径,不存在则自动生成
  37. * @param string $name 路径名
  38. * @param string $link 需要生成二维码的跳转路径
  39. * @param int $type https 1 = http , 0 = https
  40. * @param bool $force 是否返回false
  41. * @return bool|mixed|string
  42. */
  43. public static function getWechatQrcodePath(string $name, string $link, bool $force = false)
  44. {
  45. try {
  46. $imageInfo = SystemAttachment::getInfo($name, 'name');
  47. $siteUrl = sys_config('site_url');
  48. if (!$imageInfo) {
  49. $codeUrl = UtilService::setHttpType($siteUrl . $link, request()->isSsl() ? 0 : 1);//二维码链接
  50. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  51. if (is_string($imageInfo) && $force)
  52. return false;
  53. if (is_array($imageInfo)) {
  54. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  55. $url = $imageInfo['dir'];
  56. } else {
  57. $url = '';
  58. $imageInfo = ['image_type' => 0];
  59. }
  60. } else $url = $imageInfo['att_dir'];
  61. if ($imageInfo['image_type'] == 1 && $url) $url = $siteUrl . $url;
  62. return $url;
  63. } catch (\Throwable $e) {
  64. if ($force)
  65. return false;
  66. else
  67. return '';
  68. }
  69. }
  70. /**
  71. * 获取h5页面二维码完整路径,不存在则自动生成
  72. * @param string $name 路径名
  73. * @param string $link 需要生成二维码的跳转路径
  74. * @param int $type https 1 = http , 0 = https
  75. * @param bool $force 是否返回false
  76. * @return bool|mixed|string
  77. */
  78. public static function getQrcodePath(string $name, string $link, bool $force = false, $mer_id = false)
  79. {
  80. try {
  81. $imageInfo = SystemAttachment::getInfo($name, 'name');
  82. $siteUrl = sys_config('site_url');
  83. if (!$imageInfo) {
  84. $codeUrl = UtilService::setHttpType('https://auti.boofly.cn' . $link, request()->isSsl() ? 0 : 1);//二维码链接
  85. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  86. if (is_string($imageInfo) && $force)
  87. return false;
  88. if (is_array($imageInfo)) {
  89. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 7, $imageInfo['image_type'], $imageInfo['time'], 2, $mer_id);
  90. $url = $imageInfo['dir'];
  91. } else {
  92. $url = '';
  93. $imageInfo = ['image_type' => 0];
  94. }
  95. } else $url = $imageInfo['att_dir'];
  96. if ($imageInfo['image_type'] == 1 && $url) $url = $siteUrl . $url;
  97. return $url;
  98. } catch (\Throwable $e) {
  99. if ($force)
  100. return false;
  101. else
  102. return '';
  103. }
  104. }
  105. /**
  106. * 获取小程序分享二维码
  107. * @param int $id
  108. * @param int $uid
  109. * @param int $type 1 = 拼团,2 = 秒杀,3 = 助力
  110. * @param array $parame
  111. * @param bool $mer_id
  112. * @return bool|string
  113. */
  114. public static function getRoutineQrcodePath(int $id, int $uid, int $type, array $parame = [], $mer_id = false)
  115. {
  116. $page = '';
  117. $namePath = '';
  118. $data = 'id=' . $id . '&pid=' . $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. case 3:
  133. $page = 'pages/zhuli/product_detail/index';
  134. $namePath = 'assistance_' . $id . '_' . $uid . '.jpg';
  135. break;
  136. case 4:
  137. $page = 'pages/zhuli/help_zhuli/index';
  138. $namePath = 'assistance_active_' . $id . '_' . $uid . '.jpg';
  139. break;
  140. }
  141. if (!$page || !$namePath) {
  142. return false;
  143. }
  144. try {
  145. $imageInfo = SystemAttachment::getInfo($namePath, 'name');
  146. $siteUrl = sys_config('site_url', '', $mer_id);
  147. if (!$imageInfo) {
  148. $res = RoutineCode::getPageCode($page, $data, 280, $mer_id);
  149. if (!$res) return false;
  150. $uploadType = (int)sys_config('upload_type', 1, $mer_id);
  151. $upload = UploadService::init(null, $mer_id);
  152. $res = $upload->to('routine/product')->validate()->stream($res, $namePath);
  153. if ($res === false) {
  154. return false;
  155. }
  156. $imageInfo = $upload->getUploadInfo();
  157. $imageInfo['image_type'] = $uploadType;
  158. if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
  159. else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
  160. if (!$remoteImage['status']) return false;
  161. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  162. $url = $imageInfo['dir'];
  163. } else $url = $imageInfo['att_dir'];
  164. if ($imageInfo['image_type'] == 1) $url = $siteUrl . $url;
  165. return $url;
  166. } catch (\Throwable $e) {
  167. return false;
  168. }
  169. }
  170. }