Extract.php 2.2 KB

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