1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- namespace app\common\server;
- use EasyWeChat\Factory;
- use EasyWeChat\Kernel\Exceptions\Exception;
- use EasyWeChat\Kernel\Http\StreamResponse;
-
- class QrCodeServer
- {
-
-
- public static function makeMpWechatQrcode(array $param, string $type = 'resource', array $extra = [])
- {
-
- try {
-
- $page = $param['page'] ?? '';
- $scene = $param['scene'] ?? 'null';
- $save_dir = $param['save_dir'] ?? 'uploads/qr_code/user_share/';
- $file_name = $param['file_name'] ?? time() . '.png';
- $config = WeChatServer::getMnpConfig();
- $app = Factory::miniProgram($config);
-
- $response = $app->app_code->getUnlimit($scene, [
- 'page' => $page,
- ]);
-
- if(is_array($response) && 41030 === $response['errcode']){
-
-
- if (41030 === $response['errcode']) {
- throw new Exception('所传page页面不存在,或者小程序没有发布');
- }
- throw new Exception($response['errmsg']);
- }
-
- $contents = $response->getBody()->getContents();
- switch ($type){
- case 'file':
- if ($response instanceof StreamResponse) {
- $file_name = $response->saveAs($save_dir, $file_name);
- $contents = $save_dir . $file_name;
- }
- break;
- case 'base64':
- $mp_base64 = chunk_split(base64_encode($contents));
- $contents = 'data:image/png;base64,' . $mp_base64;
- }
- return data_success('',['qr_code'=>$contents, 'extra' => $extra]);
-
-
- } catch (\EasyWeChat\Kernel\Exceptions\Exception $e){
- return data_error($e->getMessage());
- }
- }
-
- }
|