197 lines
5.2 KiB
PHP
197 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace app\common\library\share;
|
|
use think\Loader;
|
|
Loader::import('PhpQrcode.phpqrcode',EXTEND_PATH,'.php');
|
|
/**
|
|
* 二维码
|
|
*/
|
|
class Qrsharecode
|
|
{
|
|
|
|
/**
|
|
* 生成二维码
|
|
* @param $src
|
|
* @return void
|
|
*/
|
|
public static function mkQrcode($src){
|
|
|
|
$qrcodeLib = new \QRcode();
|
|
|
|
ob_start();
|
|
|
|
$errorLevel = 'H';
|
|
$size = 4;
|
|
|
|
$qrcodeLib::png($src,false,$errorLevel,$size);
|
|
$imgSrc = base64_encode(ob_get_contents());
|
|
ob_end_clean();
|
|
|
|
return $imgSrc;
|
|
}
|
|
|
|
/**
|
|
* 生成小程序码
|
|
* @param $src
|
|
* @return void
|
|
*/
|
|
public static function mkMpCode($page,$scene){
|
|
|
|
$wechat = new \app\common\library\Wechat('wxMiniProgram');
|
|
try{
|
|
$pageParams = [
|
|
'page' => $page,
|
|
'width'=>100
|
|
];
|
|
|
|
if($scene){
|
|
$pageParams['scene'] = $scene;
|
|
}
|
|
|
|
$code = $wechat->getApp()->app_code->getUnlimit('scene-value', $pageParams);
|
|
}catch (\Exception $e){
|
|
return false;
|
|
}
|
|
|
|
if(!$code || !is_object($code)){
|
|
return false;
|
|
}
|
|
$random = rand(1,9999);
|
|
|
|
$path = MODULE_ROOT."/public/uploads/poster/".$random.".png";
|
|
|
|
$filename = $code->saveAs(MODULE_ROOT."/public/uploads/poster/",$random. ".png");
|
|
|
|
if($filename){
|
|
$data = base64_encode(file_get_contents($path));
|
|
unlink($path);
|
|
return $data;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 生成抖音小程序码
|
|
* @param $src
|
|
* @return void
|
|
*/
|
|
public static function mkDouyinMpCode($page,$scene){
|
|
$dyMiniProgramConfig = \app\common\model\config\System::getConfig('dyMiniProgram');
|
|
|
|
$result = \douyin\Byte::init([
|
|
'app_id'=>$dyMiniProgramConfig['appid'],
|
|
'secret'=>$dyMiniProgramConfig['secret'],
|
|
])->qrcode($page.'?'.$scene);
|
|
|
|
|
|
|
|
if(!$result ||( is_array($result) && isset($result['errcode']))){
|
|
return base64_encode(file_get_contents(MODULE_ROOT.'/public/assets/image/avatar.png'));
|
|
}
|
|
|
|
try{
|
|
$random = rand(1,9999);
|
|
$path = MODULE_ROOT."/public/uploads/poster/".$random.".png";
|
|
file_put_contents($path, $result, true);
|
|
$data = base64_encode(file_get_contents($path));
|
|
unlink($path);
|
|
return $data;
|
|
}catch (\Exception $e){
|
|
return base64_encode(file_get_contents(MODULE_ROOT.'/public/assets/image/avatar.png'));
|
|
}
|
|
// $random = rand(1,9999);
|
|
// $path = MODULE_ROOT."/public/uploads/poster/".$random.".png";
|
|
// file_put_contents($path, $result, true);
|
|
// $data = base64_encode(file_get_contents($path));
|
|
// unlink($path);
|
|
// return $data;
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取小程序码
|
|
* @param $type
|
|
* @param $val
|
|
* @return false|null
|
|
*/
|
|
public static function getMpCode($type,$val=0,$platform = 'wechat'){
|
|
switch ($type){
|
|
case 'course':
|
|
$type = 'goods';
|
|
$params = [
|
|
'goods_id'=>$val
|
|
];
|
|
break;
|
|
|
|
case 'evaluation':
|
|
case 'exercises':
|
|
$type = 'exercises';
|
|
$params = [
|
|
'id'=>$val
|
|
];
|
|
break;
|
|
case 'test':
|
|
$type = 'test';
|
|
$params = [
|
|
'id'=>$val
|
|
];
|
|
break;
|
|
case 'coupon':
|
|
$type = 'coupon';
|
|
$params = [
|
|
'id'=>$val
|
|
];
|
|
break;
|
|
case 'page':
|
|
$params = [
|
|
'id'=>$val,
|
|
'path'=>'pages/index/index'
|
|
];
|
|
break;
|
|
case 'group':
|
|
$type = 'page';
|
|
$params = [
|
|
'id'=>$val,
|
|
'path'=>'pages/course/group/group'
|
|
];
|
|
break;
|
|
case 'vipcard':
|
|
$type = 'page';
|
|
$params = [
|
|
'id'=>$val,
|
|
'path'=>'pages/app/vip/center/center'
|
|
];
|
|
break;
|
|
case 'activity':
|
|
$type = 'activity';
|
|
$params = [
|
|
'id'=>$val,
|
|
'path'=>'pages/app/activity/detail/detail'
|
|
];
|
|
break;
|
|
case 'physical':
|
|
$type = 'physical';
|
|
$params = [
|
|
'id'=>$val,
|
|
'path'=>'pages/app/physical/detail/detail'
|
|
];
|
|
break;
|
|
case 'index':
|
|
$type = 'page';
|
|
$params = [
|
|
'path'=>'pages/index/index'
|
|
];
|
|
break;
|
|
}
|
|
|
|
$data = \app\common\library\share\Links::getMpLink($type,$params);
|
|
|
|
if($platform == 'wechat'){
|
|
$qrCode = self::mkMpCode($data['link'],$data['params']);
|
|
}else{
|
|
$qrCode = self::mkDouyinMpCode($data['link'],$data['params']);
|
|
}
|
|
|
|
return $qrCode;
|
|
}
|
|
} |