123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <?php
- namespace app\main\service;
- use app\common\library\Redis;
- use EasyWeChat\Factory;
- use \EasyWeChat\Kernel\Http\StreamResponse;
- use fast\Random;
- use think\Config;
- class MiniProgramService
- {
- protected static $self = null;
- private static $appId;
- private static $secret;
- public static $theme;
- public static function instance()
- {
- if (self::$self == null) {
- self::$appId = config('site.mini_appid');
- self::$secret = config('site.mini_secret');
- self::$theme = config('site.theme');
- if (!self::$appId){
- self::getMiniFromRedis();
- }
- self::$self = new self();
- }
- return self::$self;
- }
- public static function initMiniProgram()
- {
- $wConfig = config::get('wechat');
- $config['app_id'] = self::$appId;
- $config['secret'] = self::$secret;
- $config['log'] = $wConfig['log'];
- $app = Factory::miniProgram($config);
- return $app;
- }
- public static function getMiniFromRedis(){
- $site = Redis::instance()->get('site');
- $site = json_decode($site,true);
- self::$theme = $site['theme']??'qt';
- self::$appId = $site['mini_appid'] ?? 'wx5413e3ee18d63891';
- self::$secret = $site['mini_secret'] ?? '3f90edade24ecbff6353e9ba9d175049';
- }
- /**
- * 生成发送的小程序内容
- * @param $item
- * @param $channelId
- * @return string
- */
- public function getMiniProgram($mini,$channelId)
- {
- $channelId = self::$theme.'_'.$channelId;
- if ( !isset($mini['type']) || !$mini['type']){
- return '';
- }
- $content = $mini;
- $content['page'] = trim($content['page']);
- $t = '';
- switch ($content['type']){
- case 1:
- $content['page'] .= '?channelCode='.$channelId;
- $t = self::getMiniText($content['page'],$content['content']);
- break;
- case 2:
- $content['page'] .= '?channelCode='.$channelId;
- $t = self::getMiniImg($content['page'],$content['pic']);
- // print_r($t);exit;
- break;
- case 3:
- $content['page'] .= '?channelCode='.$channelId;
- $t = self::getMiniCard($content['page'],$content['title'],$content['pic']);
- break;
- case 4:
- $scene = '@channelCode='.$channelId;
- $t = self::getAppCode($scene,$content['page'],$channelId);
- break;
- default:
- break;
- }
- return $t;
- }
- public static function getAppCode($scene,$page,$channelId, $cache = false)
- {
- $redisKey = 'MINI:'.$channelId.$page;
- if($cache){
- $url = Redis::instance()->get($redisKey);
- if (!empty($url)){
- return $url;
- }
- }
- $app = self::initMiniProgram();
- $path = '/uploads/wcode/'.date('Ymd').'/';
- $picPath = ROOT_PATH.'public/'.$path;
- $page = trim($page,'/');
- $picFileName = time().$channelId.rand(1000,9999).'.png';
- try{
- $response = $app->app_code->getUnlimit($scene,['page'=>$page]);
- if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
- $response->saveAs($picPath, $picFileName);
- } else {
- $msg = var_export($response, true);
- LogService::info('高级群发生成小程序码异常01'.$msg);
- }
- }catch (\Throwable $th){
- LogService::error('高级群发生成小程序码异常02'.$th->getMessage());
- return '';
- }
- if ($cache){
- Redis::instance()->setex($redisKey,86400,$path.$picFileName);
- }
- return $path.$picFileName;
- }
- /**
- * 小程序卡片
- * @param $appId
- * @param $page
- * @param $title
- * @param $pic
- * @return string
- */
- public static function getMiniCard($page,$title,$pic){
- $str = '<mp-miniprogram data-miniprogram-appid="'.self::$appId.'" data-miniprogram-path="'.$page.'" data-miniprogram-title="'.$title.'" data-miniprogram-imageurl="'.$pic.'"></mp-miniprogram>';
- return $str;
- }
- /**
- * 文字跳转小程序
- * @param $appId
- * @param $page
- * @param $title
- * @return string
- */
- public static function getMiniText($page,$title,$channelId=0)
- {
- if ($channelId){
- $page = trim($page).'?channelCode='.self::$theme.'_'.$channelId;
- }
- return '<a data-miniprogram-appid="'.self::$appId.'" data-miniprogram-path="'.$page.'" href="">'.$title.'</a>';
- }
- /**
- * 文字跳转小程序
- * @param $appId
- * @param $page
- * @param $title
- * @return string
- */
- public static function getMiniText2($page,$title,$channelId=0)
- {
- if ($channelId){
- $page = trim($page).'?channelCode='.self::$theme.'_'.$channelId;
- }
- return "<a data-miniprogram-appid='".self::$appId."' data-miniprogram-path='".$page."' href=''>".$title."</a>";
- }
- /**
- * 图片跳转小程序
- * @param $appId
- * @param $page
- * @param $pic
- * @return string
- */
- public static function getMiniImg($page,$pic)
- {
- $str = "<p><a data-miniprogram-appid='".self::$appId;
- $str .= "' data-miniprogram-path='".$page;
- $str .= "' href=''><img src='".$pic;
- $str .= "' alt= data-width='200px' data-ratio=''></a></p>";
- return $str;
- }
- /**
- * 小程序渠道权限
- * @param int $channelId
- * @return int
- */
- public function miniWrite($channelId=0,$type)
- {
- if (in_array($type,[1,2,6])){//超管 管理员 运营
- return 1;
- }
- $re = 0;
- if ($channelId){
- $re = (int)model('ChannelSpecialManage')->isWhite('sendMini', $channelId);
- }
- if (!$re){
- $ids = config('site.mini_ids');
- if ($ids == -1 ){
- $re = 1;
- }
- if (strstr($ids.',',(string)$channelId.',') !== false){
- $re = 1;
- }
- }
- return $re;
- }
- /**
- * 配置小程序入口页面
- * @return array
- */
- public function getMiniPage()
- {
- $pageStr = config('site.mini_page');
- if (!$pageStr){
- return [];
- }
- $pageArr = explode(PHP_EOL,$pageStr);
- foreach ($pageArr as $k=>$v){
- $temp = explode('@',$v);
- $miniPage[trim($temp[1])] = $temp[0];
- }
- return $miniPage;
- }
- /**
- * 客服消息,处理小程序
- * @param $channelId
- * @param $args
- * @return string
- */
- public function getMiniContent($channelId, $args)
- {
- $miniContent = [];
- $args['page'] = trim($args['page']);
- if (isset($args['mini_type'])){
- $miniContent = [
- 'mini_type'=>$args['mini_type'],
- 'appid'=>self::$appId,
- ];
- switch ($args['mini_type']){//1是文本类型 2 图片 3小程序卡片 4小程序码
- case '1':
- $miniContent['title'] = $args['title'];
- $miniContent['page'] = $args['page'].'?@channelCode='.self::$theme.'_'.$channelId;
- break;
- case '2':
- break;
- case '3':
- $miniContent['title'] = $args['title'];
- $miniContent['page'] = $args['page'].'?@channelCode='.self::$theme.'_'.$channelId;
- $res = HigeMessageService::instance()->image2media($channelId, $args['image']);
- $miniContent['media_id'] = $res['media_id']??'';
- break;
- case '4':
- $scene = '@channelCode='.self::$theme.'_'.$channelId;
- $miniCode = self::getAppCode($scene,$args['page'],$channelId,true);
- $res = HigeMessageService::instance()->image2media($channelId,$miniCode);
- $miniContent['media_id'] = $res['media_id']??'';
- break;
- }
- }
- return (string)json_encode($miniContent);
- }
- }
|