QrcodeService.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace crmeb\services;
  3. use app\admin\model\system\SystemAttachment;
  4. use app\admin\model\wechat\WechatQrcode as QrcodeModel;
  5. class QrcodeService
  6. {
  7. /**
  8. * 获取临时二维码 单个
  9. * @param $type
  10. * @param $id
  11. * @return array
  12. */
  13. public static function getTemporaryQrcode($type, $id)
  14. {
  15. return QrcodeModel::getTemporaryQrcode($type, $id)->toArray();
  16. }
  17. /**
  18. * 获取永久二维码 单个
  19. * @param $type
  20. * @param $id
  21. * @return array
  22. */
  23. public static function getForeverQrcode($type, $id)
  24. {
  25. return QrcodeModel::getForeverQrcode($type, $id)->toArray();
  26. }
  27. /**
  28. * 从数据库获取二维码
  29. * @param $id
  30. * @param string $type
  31. * @return array|\think\Model|null
  32. */
  33. public static function getQrcode($id, $type = 'id')
  34. {
  35. return QrcodeModel::getQrcode($id, $type);
  36. }
  37. /**
  38. * 增加某个二维码扫描的次数
  39. * @param $id
  40. * @param string $type
  41. * @return mixed
  42. */
  43. public static function scanQrcode($id, $type = 'id')
  44. {
  45. return QrcodeModel::scanQrcode($id, $type);
  46. }
  47. /**
  48. * 获取二维码完整路径,不存在则自动生成
  49. * @param string $name 路径名
  50. * @param string $link 需要生成二维码的跳转路径
  51. * @param int $type https 1 = http , 0 = https
  52. * @param bool $force 是否返回false
  53. * @return bool|mixed|string
  54. */
  55. public static function getWechatQrcodePath(string $name, string $link, int $type = 1, bool $force = false)
  56. {
  57. try {
  58. $imageInfo = SystemAttachment::getInfo($name, 'name');
  59. $siteUrl = sys_config('site_url');
  60. if (!$imageInfo) {
  61. $codeUrl = UtilService::setHttpType($siteUrl . $link, $type); //二维码链接
  62. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  63. if (is_string($imageInfo) && $force)
  64. return false;
  65. if (is_array($imageInfo)) {
  66. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  67. $url = $imageInfo['dir'];
  68. } else {
  69. $url = '';
  70. $imageInfo = ['image_type' => 0];
  71. }
  72. } else $url = $imageInfo['att_dir'];
  73. if ($imageInfo['image_type'] == 1 && $url) $url = $siteUrl . $url;
  74. return $url;
  75. } catch (\Throwable $e) {
  76. if ($force)
  77. return false;
  78. else
  79. return '';
  80. }
  81. }
  82. }