quanwei
2025-11-03 ef5d748e3e3331bd20d4065c33e7867c2637d1db
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
 
namespace app\common\service\qrcode;
 
use Endroid\QrCode\QrCode;
 
/**
 * 订单核销二维码
 */
class ExtractService extends Base
{
    private $appId;
 
    //用户
    private $user;
 
    private $orderId;
 
    private $orderNo;
 
    private $source;
 
    private $type;
 
    /**
     * 构造方法
     */
    public function __construct($appId, $user, $orderId, $source, $orderNo, $type = 'order')
    {
        parent::__construct();
        $this->appId = $appId;
        $this->user = $user;
        $this->orderId = $orderId;
        $this->source = $source;
        $this->orderNo = $orderNo;
        $this->type = $type;
    }
 
    /**
     * 获取小程序码
     */
    public function getImage()
    {
        // 判断二维码文件存在则直接返回url
        if (file_exists($this->getPosterPath())) {
            return $this->getPosterUrl();
        }
        $qrcode = new QrCode($this->orderNo);
        $qrcode = $this->saveMpQrcode($qrcode, $this->appId, $this->orderId, 'image_mp');
        return $this->savePoster($qrcode);
    }
 
    private function savePoster($qrcode)
    {
        copy($qrcode, $this->getPosterPath());
        return $this->getPosterUrl();
    }
 
    /**
     * 二维码文件路径
     */
    private function getPosterPath()
    {
        $web_path = $_SERVER['DOCUMENT_ROOT'];
        // 保存路径
        $tempPath = $web_path . "/temp/{$this->appId}/{$this->source}/";
        !is_dir($tempPath) && mkdir($tempPath, 0755, true);
        return $tempPath . $this->getPosterName();
    }
 
    /**
     * 二维码文件名称
     */
    private function getPosterName()
    {
        return $this->type . '_' . md5("{$this->orderId}_{$this->user['user_id']}}") . '.png';
    }
 
    /**
     * 二维码url
     */
    private function getPosterUrl()
    {
        return \base_url() . "temp/{$this->appId}/{$this->source}/{$this->getPosterName()}" . '?t=' . time();
    }
 
}