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 = ''; 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 ''.$title.''; } /** * 文字跳转小程序 * @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 "".$title.""; } /** * 图片跳转小程序 * @param $appId * @param $page * @param $pic * @return string */ public static function getMiniImg($page,$pic) { $str = "

data-width='200px'

"; 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); } }