server->push(function ($message)use ($instance,$app,$code) {
$instance->info("信息:".json_encode($message));
switch ($message['MsgType']) {
case 'event'://事件
self::handleClickEvent($message,$app);
break;
case 'text'://文字
self::handleTxt($message,$app,$code);
break;
case 'image'://图片
break;
case 'voice'://语音
break;
case 'video'://视频
break;
case 'location'://坐标
break;
case 'link'://链接
break;
case 'file'://文件
// ... 其它消息
default:
break;
}
});
$response = $app->server->serve();
return $response;
}
/**
* 处理事件
* @param $message array 时间信息
* @param $app \EasyWeChat\OfficialAccount\Application
* @throws \App\Exceptions\CommonException
*/
public static function handleClickEvent($message,$app)
{
$instance = new LoggerFactoryUtil(WechatLogic::class);
$officialOpenId = $message["FromUserName"];
$log = new LoggerFactoryUtil(WechatLogic::class);
switch ($message["Event"]){
case "subscribe"://关注公众号
$user = $app->user->get($officialOpenId);
$log->info("用户信息".json_encode($user));
$nickname = $user["nickname"];
//判断用户是否存在
$daogoUser = UserModel::query()->where("user_open_id",$officialOpenId)->first();
if(!$daogoUser){
//不存在则写入用户
$userId = UserModel::query()->insertGetId(
[
"user_open_id"=>$officialOpenId,
"user_head_img_url"=>$user["headimgurl"]??"",
"user_nickname"=>$user["nickname"]??"",
"user_sex"=>$user["sex"]??1,
"user_api_key"=>md5(microtime(true)),
"user_api_key_expire_time"=>date("Y-m-d H:i:s",strtotime("+1 year")),
"created_at"=>date("Y-m-d H:i:s"),
"updated_at"=>date("Y-m-d H:i:s"),
]
);
}else{
$userId = $daogoUser->id;
}
//获取美团的推广链接
$meituanKey = "meituanwaimai:".$userId;
$val = BaseUtil::getCacheFromRedis($meituanKey);
$instance->info("1");
if(!$val){
$instance->info("2");
//缓存中不存在则从官方获取
$platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_MEITUAN,$userId);
$val = MeituanLianmengUtil::generateLink(2,$platformSid,MeiTuanLinkTypeEnum::MINIPROGRAME,env("MEITUAN_LIANMENG_KEY"));
$instance->info($val);
BaseUtil::setRedisCache($meituanKey,$val,3600*24);
}
$instance->info("到这里里");
//判断EventKey是否存在
$msg1 = new Text('欢迎关注淘客兔!');
$link = "[红包] 美团超大外卖红包";
$msg2 = <<customer_service->message($msg1)->to($officialOpenId)->send();
$res2 = $app->customer_service->message($msg2)->to($officialOpenId)->send();
$instance->info('结果1:'.json_encode($res1));
$instance->info('结果2:'.json_encode($res2));
break;
case "unsubscribe"://取关公众号
break;
case "CLICK"://点击时间
if($message["EventKey"]=="wenti"){
$link1 = "1.领取外卖红包遇到问题";
$link2 = "2.话费充值问题";
$link3 = "3.美团代金券问题";
$link4 = "4.超级吃货卡问题";
$link5 = "5.佣金提现问题";
$link6 = "6.其他问题";
//常见问题
$response = <<customer_service->message($wenti)->to($officialOpenId)->send();
}elseif($message["EventKey"]=="lianxi"){
$mediaId = "VlvMS-GXy-81ZmdCPPsSsvpn8c6DBbofpWQAOnztQG0UA7FzAjXwnBXs5II0-3tB";
$lianxi = new Image($mediaId);
$app->customer_service->message($lianxi)->to($officialOpenId)->send();
}
break;
}
}
/**
* 处理文字
* @param $message
* @param $app Application
*/
public static function handleTxt($message,$app,$code)
{
$instance = new LoggerFactoryUtil(WechatLogic::class);
$officialOpenId = $message["FromUserName"];
switch ($message["Content"]){
case "领取外卖红包遇到问题":
$link1 = "1.领了外卖红包,付款时却找不到可用红包";
$link2 = "2.如何更换领取红包的手机号";
$response = <<<更换手机号>";
$response = <<1. 更换饿了么绑定手机号";
$link2 = "2. 更换美团绑定手机号";
$response = <<1. 更换饿了么绑定手机号";
$link2 = "2. 更换美团绑定手机号";
$response = <<<联系客服>";
$response = <<customer_service->message($wenti)->to($officialOpenId)->send();
}
/**
* 设置公众号菜单逻辑
*/
public static function setMenuLogic()
{
$params = request()->all();
//1.获取所有上线的一级菜单
$menus = WechatAccountMenuConfigModel::getMenuByParentId(0);
$configs = [];
foreach ($menus as $menu){
switch ($menu->menu_type){
case WechatAccountMenuTypeEnum::CLICK:
//点击菜单
$configs[] = [
"type" => "click",
"name" => $menu->menu_name,
"key" => $menu->menu_key
];
break;
case WechatAccountMenuTypeEnum::LINK:
//商品链接
$configs[] = [
"type" => "view",
"name" => $menu->menu_name,
"url" => $menu->menu_url
];
break;
case WechatAccountMenuTypeEnum::MENU:
//菜单
//获取下级菜单
$tmps = WechatAccountMenuConfigModel::getMenuByParentId($menu->id);
$subMenus = [];
foreach ($tmps as $tmp){
switch ($tmp->menu_type) {
case WechatAccountMenuTypeEnum::CLICK:
//点击菜单
$subMenus[] = [
"type" => "click",
"name" => $tmp->menu_name,
"key" => $tmp->menu_key
];
break;
case WechatAccountMenuTypeEnum::LINK:
//链接
$subMenus[] = [
"type" => "view",
"name" => $tmp->menu_name,
"url" => $tmp->menu_url
];
break;
}
}
$configs[] = [
"name"=>$menu->menu_name,
"sub_button"=>$subMenus
];
break;
}
}
$app = WechatAccountUtil::getApp($params["wechat_app_code"]);
$instance = new LoggerFactoryUtil(WechatLogic::class);
$instance->info("菜单:".json_encode($configs));
return $app->menu->create($configs);
}
}