huangsijun
2025-11-06 372494883079be03b921f6686691271355bfdd14
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
<?php
 
namespace app\common\service\qrcode;
 
use Endroid\QrCode\QrCode;
 
/**
 * 公众号登录二维码
 */
class MpLoginService extends Base
{
    private $appId;
 
    /**
     * 构造方法
     */
    public function __construct($appId)
    {
        parent::__construct();
        $this->appId = $appId;
    }
 
    /**
     * 获取
     */
    public function getImage()
    {
        // 判断二维码文件存在则直接返回url
        if (file_exists($this->getPosterPath())) {
            return $this->getPosterUrl();
        }
        $qrcode = new QrCode(base_url().'h5/pages2/login/mplogin?app_id=' . $this->appId);
        $qrcode = $this->saveMpQrcode($qrcode, $this->appId, '', 'image_mplogin');
        return $this->getPosterUrl();
    }
 
    /**
     * 二维码文件路径
     */
    private function getPosterPath()
    {
        $web_path = $_SERVER['DOCUMENT_ROOT'];
        // 保存路径
        $tempPath = $web_path . "/temp/{$this->appId}/image_mplogin/";
        !is_dir($tempPath) && mkdir($tempPath, 0755, true);
        return $tempPath . $this->getPosterName();
    }
 
    /**
     * 二维码文件名称
     */
    private function getPosterName()
    {
        return 'qrcode_' . md5("{$this->appId}") . '.png';
    }
 
    /**
     * 二维码url
     */
    private function getPosterUrl()
    {
        return \base_url() . "temp/{$this->appId}/image_mplogin/{$this->getPosterName()}" . '?t=' . time();
    }
 
}