| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\utils;
- use Curl\Curl;
- use EasyWeChat\Factory;
- class GenerateShareQrcode
- {
- /**
- * @param $storeId integer 商城ID
- * @param $scene string 二维码参数
- * @param int $width 二维码大小
- * @param null $page 跳转页面
- * @param int $platform 小程序类型 0--微信 1--支付宝
- */
- public static function getQrcode($storeId, $scene, $width = 430, $page = null)
- {
- if ($page == null) {
- $page = 'pages/index/index';
- }
- $model = new GenerateShareQrcode();
- return $model->wechat($scene, $width, $page);
- }
- public function wechat($scene, $width = 430, $page = null)
- {
- /** @var \EasyWeChat\MiniProgram\Application $wechat */
- $wechat = \app\modules\client\models\ApiModel::getWechat();
- $data = [
- 'width' => $width,
- ];
- if ($page) {
- $data['page'] = $page;
- }
- \Yii::warning("GET WXAPP QRCODE:" . "scene => ".$scene." data => ". json_encode($data));
- $response = $wechat->app_code->getUnlimit($scene, $data);
- \Yii::warning($response);
- // 保存小程序码到文件
- if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
- if(!is_dir(\Yii::$app->runtimePath . '/image')) {
- mkdir(\Yii::$app->runtimePath . '/image');
- }
- $file_name = md5(base64_encode($response->getBody())) . '.jpg';
- $save_path = \Yii::$app->runtimePath . '/image/';
- $response->saveAs($save_path, $file_name);
- //返回图片
- return [
- 'code' => 0,
- 'file_path' => $save_path.$file_name,
- ];
- } else {
- //返回文字
- return [
- 'code' => 1,
- 'msg' => $response['errmsg'],
- ];
- }
- }
- //保存图片内容到临时文件
- private function saveTempImageByContent($content)
- {
- $save_path = \Yii::$app->runtimePath . '/image/' . md5(base64_encode($content)) . '.jpg';
- if(!is_dir(\Yii::$app->runtimePath . '/image')) {
- mkdir(\Yii::$app->runtimePath . '/image');
- }
- $fp = fopen($save_path, 'w');
- fwrite($fp, $content);
- fclose($fp);
- return $save_path;
- }
- }
|