QrcodeService.php 2.7 KB

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