QrcodeService.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\LinkModel;
  13. use BaconQrCode\Encoder\QrCode;
  14. /**
  15. * 二维码管理-服务类
  16. * @author wesmiler
  17. * @since 2020/11/11
  18. * Class QrcodeService
  19. * @package App\Services
  20. */
  21. class QrcodeService extends BaseService
  22. {
  23. protected static $instance = null;
  24. /**
  25. * 构造函数
  26. * @author wesmiler
  27. * @since 2020/11/11
  28. * QrcodeService constructor.
  29. */
  30. public function __construct()
  31. {
  32. }
  33. /**
  34. * 静态入口
  35. * @return QrcodeService|null
  36. */
  37. public static function make(){
  38. if(!self::$instance){
  39. self::$instance = new QrcodeService();
  40. }
  41. return self::$instance;
  42. }
  43. /**
  44. * 生成自定义二维码
  45. * @param $userId 用户ID
  46. * @return false|string|\图片地址
  47. */
  48. public function makeQrcode($userId){
  49. $path = 'img/qrcode/U_'.$userId.'_'.base64_encode($userId).'.png';
  50. QrCode::format('png')->size(100)->encoding('UTF-8')->generate($userId,public_path($path));
  51. if(file_exists(public_path($path))){
  52. return get_image_url($path);
  53. }else{
  54. return false;
  55. }
  56. }
  57. }