Extract.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\common\service\qrcode;
  10. use app\common\enum\OrderType as OrderTypeEnum;
  11. /**
  12. * 订单核销二维码
  13. * Class Extract
  14. * @package app\common\service\qrcode
  15. */
  16. class Extract extends Base
  17. {
  18. private $wxappId;
  19. /* @var int $user 用户 */
  20. private $user;
  21. private $orderId;
  22. private $orderType;
  23. /**
  24. * 构造方法
  25. * Extract constructor.
  26. * @param $wxappId
  27. * @param $user
  28. * @param $orderId
  29. * @param $orderType
  30. */
  31. public function __construct($wxappId, $user, $orderId, $orderType = OrderTypeEnum::MASTER)
  32. {
  33. parent::__construct();
  34. $this->wxappId = $wxappId;
  35. $this->user = $user;
  36. $this->orderId = $orderId;
  37. $this->orderType = $orderType;
  38. }
  39. /**
  40. * 获取小程序码
  41. * @return mixed
  42. * @throws \app\common\exception\BaseException
  43. * @throws \think\exception\DbException
  44. * @throws \Exception
  45. */
  46. public function getImage()
  47. {
  48. // 判断二维码文件存在则直接返回url
  49. if (file_exists($this->getPosterPath())) {
  50. return $this->getPosterUrl();
  51. }
  52. // 下载小程序码
  53. $qrcode = $this->saveQrcode(
  54. $this->wxappId,
  55. "oid:{$this->orderId},oty:{$this->orderType}",
  56. 'pages/store/check/order'
  57. );
  58. return $this->savePoster($qrcode);
  59. }
  60. private function savePoster($qrcode)
  61. {
  62. copy($qrcode, $this->getPosterPath());
  63. return $this->getPosterUrl();
  64. }
  65. /**
  66. * 二维码文件路径
  67. * @return string
  68. */
  69. private function getPosterPath()
  70. {
  71. // 保存路径
  72. $tempPath = WEB_PATH . "temp/{$this->wxappId}/";
  73. !is_dir($tempPath) && mkdir($tempPath, 0755, true);
  74. return $tempPath . $this->getPosterName();
  75. }
  76. /**
  77. * 二维码文件名称
  78. * @return string
  79. */
  80. private function getPosterName()
  81. {
  82. return 'extract_' . md5("{$this->orderId}_{$this->user['open_id']}}") . '.png';
  83. }
  84. /**
  85. * 二维码url
  86. * @return string
  87. */
  88. private function getPosterUrl()
  89. {
  90. return \base_url() . 'temp/' . $this->wxappId . '/' . $this->getPosterName() . '?t=' . time();
  91. }
  92. }