quanwei
2025-11-28 3ea53e61cc23fdb3ddf8b38a199ca60a6da8c407
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?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;
    }
 
    /**
     * 保存小程序码到文件
     */
    protected function saveBranchQrcodeToDir($app_id, $page, $savePath, $branch_id)
    {
        // 小程序配置信息
        $app = AppWx::getApp($app_id);
        // 小程序码参数
        $scene = "branch_id:{$branch_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 saveBranchMpQrcodeToDir($page, $savePath, $branch_id, $app_id)
    {
        $qrcode = new QrCode(base_url() . $page . '?app_id=' . $app_id . '&branch_id=' . $branch_id);
        $scene = "branch_id:{$branch_id}";
        // 文件名称
        $fileName = 'qrcode_' . md5($app_id . $scene) . '.png';
        // 保存二维码到文件
        $path = "{$savePath}{$fileName}";
        $qrcode->writeFile($path);
        return true;
    }
 
    /**
     * 保存二维码码到文件
     */
    protected function saveBranchActivityQrcodeToDir($app_id, $page, $savePath, $scene)
    {
        // 小程序配置信息
        $app = AppWx::getApp($app_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 $fileName;
    }
 
    /**
     * 保存二维码码到文件
     */
    protected function saveBranchActivityVerifyMpQrcodeToDir($page, $savePath, $activity_id, $app_id)
    {
        $qrcode = new QrCode(base_url() . $page . '?app_id=' . $app_id . '&activity_id=' . $activity_id. '&user_verify=1');
        $scene = "activity_id:{$activity_id}";
        // 文件名称
        $fileName = 'qrcode_' . md5($app_id . $scene) . '.png';
        // 保存二维码到文件
        $path = "{$savePath}{$fileName}";
        $qrcode->writeFile($path);
        return $fileName;;
    }
 
}