huangsijun
2025-09-22 a78c011de350b188afb03beb2f26a73f35f71986
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
 
namespace app\common\service\qrcode;
 
use app\common\library\easywechat\AppWx;
use Endroid\QrCode\QrCode;
/**
 * 二维码服务基类
 */
class Base
{
    /**
     * 构造方法
     */
    public function __construct()
    {
    }
 
    /**
     * 保存小程序码到文件
     */
    protected function saveQrcode($app_id, $scene, $page)
    {
        // 文件目录
        $dirPath = root_path('public') . "/temp/{$app_id}/image_wx";
        !is_dir($dirPath) && mkdir($dirPath, 0755, true);
        // 文件名称
        $fileName = 'qrcode_' . md5($app_id . $scene . $page) . '.png';
        // 文件路径
        $savePath = "{$dirPath}/{$fileName}";
        if (file_exists($savePath)) return $savePath;
        // 小程序配置信息
        $app = AppWx::getApp($app_id);
        // 请求api获取小程序码
        $response = $app->app_code->getUnlimit($scene, [
            'page' => $page,
            'width' => 430
        ]);
        // 保存小程序码到文件
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $response->saveAs($dirPath, $fileName);
        }
        return $savePath;
    }
 
    /**
     * 保存普通二维码到文件
     */
    protected function saveMpQrcode(\Endroid\QrCode\QrCode $qrcode, $app_id, $scene, $source)
    {
        // 文件目录
        $dirPath = root_path('public') ."/temp/{$app_id}/{$source}";
        !is_dir($dirPath) && mkdir($dirPath, 0755, true);
        // 文件名称
        $fileName = 'qrcode_' . md5($app_id . $scene) . '.png';
        // 文件路径
        $savePath = "{$dirPath}/{$fileName}";
        if (file_exists($savePath)) return $savePath;
        // 保存二维码到文件
        $qrcode->writeFile($savePath);
        return $savePath;
    }
 
    /**
     * 获取网络图片到临时目录
     */
    protected function saveTempImage($app_id, $url, $mark = 'temp')
    {
        $dirPath = root_path('public') . "temp/{$app_id}/{$mark}";
        !is_dir($dirPath) && mkdir($dirPath, 0755, true);
        $savePath = $dirPath . '/' . $mark . '_' . md5($url) . '.png';
        if (file_exists($savePath)) return $savePath;
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        $img = curl_exec($ch);
        curl_close($ch);
        $fp = fopen($savePath, 'w');
        fwrite($fp, $img);
        fclose($fp);
        return $savePath;
    }
 
    /**
     * 保存小程序码到文件
     */
    protected function saveSupplierQrcodeToDir($app_id, $page, $savePath, $shop_supplier_id)
    {
        // 小程序配置信息
        $app = AppWx::getApp($app_id);
        // 小程序码参数
        $scene = "shop_supplier_id:{$shop_supplier_id}";
        // 文件名称
        $fileName = 'qrcode_' . md5($app_id . $scene . $page) . '.png';
        // 请求api获取小程序码
        $response = $app->app_code->getUnlimit($scene, [
            'page' => $page,
            'width' => 430
        ]);
        // 保存小程序码到文件
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $response->saveAs($savePath, $fileName);
        }
        return true;
    }
 
    /**
     * 保存二维码码到文件
     */
    protected function saveSupplierMpQrcodeToDir($page, $savePath, $shop_supplier_id, $app_id)
    {
        $qrcode = new QrCode(base_url() . $page . '?app_id=' . $app_id . '&shop_supplier_id=' . $shop_supplier_id);
        $scene = "shop_supplier_id:{$shop_supplier_id}";
        // 文件名称
        $fileName = 'qrcode_' . md5($app_id . $scene) . '.png';
        // 保存二维码到文件
        $path = "{$savePath}{$fileName}";
        $qrcode->writeFile($path);
        return true;
    }
 
}