server->push(function ($message)use ($instance,$app,$code) { $instance->info("信息:".json_encode($message)); switch ($message['MsgType']) { case 'event'://事件 self::handleClickEvent($message,$app,$code); 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,$code) { $instance = new LoggerFactoryUtil(WechatLogic::class); $officialOpenId = $message["FromUserName"]; $user = $app->user->get($officialOpenId); $log = new LoggerFactoryUtil(WechatLogic::class); $log->info("用户信息".json_encode($user)); //判断用户是否存在 $daogoUser = UserModel::query()->where("user_open_id",$officialOpenId)->first(); $instance->info("数据库用户:".json_encode($daogoUser)); //获取公众号信息 $account = WechatAccountModel::findByWechatAppCode($code); //获取所属站点信息 $webSite = WebSiteModel::findByWechatAccountId($account->id); if(!$daogoUser){ //不存在则写入用户 //判断是否存在上级用户ID $inviteUserId = 0; if($message["Event"]=="subscribe" && ($user["qr_scene"] || $user["qr_scene_str"])){ $instance->info("邀请用户ID:".$user["qr_scene"]."邀请ID".$user["qr_scene_str"]); if($user["qr_scene_str"]){ $inviteUserId = $user["qr_scene_str"]; } if($user["qr_scene"]){ $inviteUserId = $user["qr_scene"]; } } $userId = UserModel::query()->insertGetId( [ "invite_user_id"=>$inviteUserId, "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"), "wechat_account_id"=>$account->id, "web_site_id"=>$webSite->id ] ); }else{ $userId = $daogoUser->id; } $nickname = $user["nickname"]; switch ($message["Event"]){ case "subscribe"://关注公众号 //获取美团的推广链接 // $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_MEITUAN,$userId); // //外卖 // $meituanUrl1 = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,2,MeiTuanLinkTypeEnum::H5); // //获取饿了么的推广链接 // $elmUrl1 = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020002597"); // $elmUrl2 = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020004284"); // $elmUrl3 = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020005049"); // $elmUrl4 = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020004425"); // $elmLink1 = "饿了么超大外卖红包"; // $elmLink2 = "饿了么超大限时红包"; // $elmLink3 = "饿了么最新红包"; // $elmLink4 = "饿了么大额红包"; //判断EventKey是否存在 $msg1 = new Image(env("WECHAT_IMAGE_MEDIA_ID")); $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"://点击事件 $date = date("m月d日"); switch ($message["EventKey"]){ case "elmwaimai": //饿了么外卖 $url = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020002597"); $link = "点我领取饿了么外卖红包"; break; case "elmxianshi": //饿了么限时红包 $url = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020004284"); $link = "点我领取饿了么限时红包"; break; case "elmzhaji": //饿了么炸鸡红包 $url = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020005049"); $link = "点我领取饿了么炸鸡红包"; break; case "elmxiawu": //饿了么下午茶红包 $url = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020004425"); $link = "点我领取饿了么下午茶红包"; break; case "mtwaimai": //美团外卖红包 //获取美团的推广链接 $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_MEITUAN,$userId); $url = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,2,MeiTuanLinkTypeEnum::H5); // $link = "点我领取美团外卖红包"; $link = "点我领取美团外卖红包"; break; case "mtjiudian": //美团酒店红包 //获取美团的推广链接 $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_MEITUAN,$userId); $url = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,7,MeiTuanLinkTypeEnum::MINIPROGRAME); $link = "点我领取美团酒店红包"; break; case "mtyouxuan": //美团优选红包 //获取美团的推广链接 $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_MEITUAN,$userId); $url = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,22,MeiTuanLinkTypeEnum::MINIPROGRAME); $link = "点我领取美团优选红包"; // $link = "点我领取美团优选红包"; break; case "mtshangou": //美团闪购红包 //获取美团的推广链接 $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_MEITUAN,$userId); $url = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,4,MeiTuanLinkTypeEnum::H5); // $link = "点我领取美团闪购红包"; $link = "点我领取美团闪购红包"; break; case "help": //使用教程 $msg = new Image(env("WECHAT_IMAGE_MEDIA_ID")); $res = $app->customer_service->message($msg)->to($officialOpenId)->send(); return; break; } $msg = <<customer_service->message($msg)->to($officialOpenId)->send(); $instance->info("返回结果:".json_encode($res)); } } /** * 处理文字 * @param $message * @param $app Application */ public static function handleTxt($message,$app,$code) { $instance = new LoggerFactoryUtil(WechatLogic::class); $officialOpenId = $message["FromUserName"]; $instance->info("用户信息:".$message["Content"]); $daogoUser = UserModel::query()->where("user_open_id",$officialOpenId)->first(); $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_PINGDUODUO,$daogoUser->id); $instance->info("pid:".$platformSid); //获取聚推客的推广位ID $jutuikeSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_JUTUIKE,$daogoUser->id); //获取淘宝推广位ID $taobaoSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_TAOBAO,$daogoUser->id); //判断是否是帮助 if($message["Content"]=="帮助"){ $msg1 = new Image(env("WECHAT_IMAGE_MEDIA_ID")); $app->customer_service->message($msg1)->to($officialOpenId)->send(); return; } //判断是否是提现 if($message["Content"]=="提现"){ $wenti = new Text("点击公众号菜单=>个人中心=>我的=>可提现余额==>提现"); $app->customer_service->message($wenti)->to($officialOpenId)->send(); return; } //判断是否余额 if($message["Content"]=="余额"){ $wenti = new Text("点击公众号菜单=>个人中心,可查看当月预估收益和上月收益"); $app->customer_service->message($wenti)->to($officialOpenId)->send(); return; } $wenti = new Text("稍等"); $app->customer_service->message($wenti)->to($officialOpenId)->send(); try{ //1.判断是否匹配上拼多多商品链接 $res = preg_match("/https\:\/\/mobile\.yangkeduo\.com/",$message["Content"],$match); if($res){ //去除链接上的多余参数防止出现比价订单 // $tmp = BaseUtil::getParamsByUrl($message["Content"]); // if(isset($tmp["goods_id"]) && isset($tmp["url"])){ // $message["Content"] = $tmp["url"]."?goods_id=".$tmp["goods_id"]; // } $instance->info("匹配到拼多多链接"); // $goodsInfo = JutuikeUtil::transferGoodsLink($message["Content"]); // $tuiguangRes = JutuikeUtil::convert($goodsInfo["goodsId"],$jutuikeSid,"pdd"); // $goodsTitle = $tuiguangRes["goodsName"]; // $goodsPrice = $tuiguangRes["marketPrice"]; // $couponPrice = $tuiguangRes["couponInfo"]["fav"]; // $afterCouponPrice = $tuiguangRes["price"]; // $rebeatPrice = round($tuiguangRes["commission"]*0.7,2); // $url = $tuiguangRes["url"]; // $msg = <<我的=>钱包,可以1:1提现哦 //S; // $wenti = new Text($msg); // $app->customer_service->message($wenti)->to($officialOpenId)->send(); // return; // $instance->info("拼多多处理过的链接:".$message["Content"]); //获取商品详情 $bean = new GoodsSearchParamBean( [ "keyword"=>$message["Content"], "pid"=>"23985775_220421267" ] ); $goodsData = DuoDuoKeUtil::goodsSearch($bean); $instance->info("拼多多返回商品详情数据:".json_encode($goodsData)); //1.判断推广位是否备案 $oauthRes = DuoDuoKeUtil::memberAuthorityQuery($platformSid); $instance->info("备案信息:".json_encode($oauthRes)); //2.未备案这授权,已备案则直接获取推广链接 if($oauthRes["authority_query_response"]["bind"]){ //已备案 $instance->info("拼多多转链用的url:".$message["Content"]); $data = DuoDuoKeUtil::goodsZsUnitUrlGen($platformSid,$message["Content"]); $instance->info("拼多多返回数据:".json_encode($data)); $url = $data["goods_zs_unit_generate_response"]["short_url"]; // $link = "点我购买,即刻拿优惠"; }else{ //未备案,使用自己已备案的pid调用商品搜索接口获取goods_sign //获取到goods_sign $goods_sign = $goodsData["goods_search_response"]["goods_list"][0]["goods_sign"]; $beanData = [ "p_id"=>$platformSid, "goods_sign_list"=>json_encode([$goods_sign]), "generate_authority_url"=>"true", ]; $generateBean = new GoodsPromotionUrlGenerateParamBean($beanData); $res = DuoDuoKeUtil::goodsPromotionUrlGenerate($generateBean); $instance->info("生成备案信息:".json_encode($res)); $url = $res["goods_promotion_url_generate_response"]["goods_promotion_url_list"][0]["short_url"]; // $link = "点击备案之后,直接购买"; } $goodsPrice = round($goodsData["goods_search_response"]["goods_list"][0]["min_group_price"]/100,2); $couponPrice = round($goodsData["goods_search_response"]["goods_list"][0]["coupon_discount"]/100,2); $afterCouponPrice = $goodsPrice-$couponPrice; $rebeatPrice = round($goodsData["goods_search_response"]["goods_list"][0]["promotion_rate"]*$afterCouponPrice*0.7/1000,2); $goodsTitle = $goodsData["goods_search_response"]["goods_list"][0]["goods_name"]; $msg = <<我的=>钱包,可以1:1提现哦 S; $wenti = new Text($msg); $app->customer_service->message($wenti)->to($officialOpenId)->send(); return; } //3.判断是否匹配到维品会链接 $res = preg_match("/https\:\/\/m\.vip\.com/",$message["Content"],$match); if($res){ $instance->info("匹配到维品会链接"); $goodsInfo = JutuikeUtil::transferGoodsLink($message["Content"]); $tuiguangRes = JutuikeUtil::convert($goodsInfo["goodsId"],$jutuikeSid,$goodsInfo["source"]); $goodsTitle = $tuiguangRes["goodsName"]; $goodsPrice = $tuiguangRes["marketPrice"]; $couponPrice = $tuiguangRes["couponInfo"]["fav"]; $afterCouponPrice = $tuiguangRes["price"]; $rebeatPrice = round($tuiguangRes["commission"]*0.7,2); $url = $tuiguangRes["url"]; $msg = <<我的=>钱包,可以1:1提现哦 S; $wenti = new Text($msg); $app->customer_service->message($wenti)->to($officialOpenId)->send(); return; } //3.判断是否匹配到京东链接 $res = preg_match("/https\:\/\/item\.m\.jd\.com/",$message["Content"],$match); if($res){ $instance->info("匹配到京东链接"); $goodsInfo = JutuikeUtil::transferGoodsLink($message["Content"]); $tuiguangRes = JutuikeUtil::convert($goodsInfo["goodsId"],$jutuikeSid,$goodsInfo["source"]); $goodsTitle = $tuiguangRes["goodsName"]; $goodsPrice = $tuiguangRes["marketPrice"]; $couponPrice = $tuiguangRes["couponInfo"]["fav"]; $afterCouponPrice = $tuiguangRes["price"]; $rebeatPrice = round($tuiguangRes["commission"]*0.7,2); $url = $tuiguangRes["url"]; $msg = <<我的=>钱包,可以1:1提现哦 S; $wenti = new Text($msg); $app->customer_service->message($wenti)->to($officialOpenId)->send(); return; } //2.判断是否匹配上淘宝链接 // $res = preg_match("/https\:\/\/m\.tb\.cn/",$message["Content"],$match); // // if($res){ // $instance->info("匹配到淘宝链接"); // $goodsInfo = JutuikeUtil::transferGoodsLink($message["Content"]); // $tuiguangRes = JutuikeUtil::convert($goodsInfo["goodsId"],$jutuikeSid); // $goodsTitle = $tuiguangRes["goodsName"]; // $goodsPrice = $tuiguangRes["marketPrice"]; // $couponPrice = $tuiguangRes["couponInfo"]["fav"]; // $afterCouponPrice = $tuiguangRes["price"]; // $rebeatPrice = round($tuiguangRes["commission"]*0.7,2); // $url = $tuiguangRes["url"]; // $msg = <<我的=>钱包,可以1:1提现哦 //S; // $wenti = new Text($msg); // $app->customer_service->message($wenti)->to($officialOpenId)->send(); // return; // } //默认是认为淘宝链接 $instance->info("匹配到淘宝链接"); $goodsInfo = TaobaoLianMengUtil::taokoulingConvert($message["Content"]); $instance->info("淘宝返回数据:".json_encode($goodsInfo)); //获取商品详情 // $goodsItem = TaobaoLianMengUtil::getGoodsInfo($goodsInfo["num_iid"]); //获取淘口令 $tuiguangRes = JutuikeUtil::convert($goodsInfo["num_iid"],$jutuikeSid); $goodsTitle = $tuiguangRes["goodsName"]; $goodsPrice = $tuiguangRes["marketPrice"]; $couponPrice = $tuiguangRes["couponInfo"]["fav"]; $afterCouponPrice = $tuiguangRes["price"]; $rebeatPrice = round($tuiguangRes["commission"]*0.7,2); $url = $tuiguangRes["url"]; $msg = <<我的=>钱包,可以1:1提现哦 S; $wenti = new Text($msg); $app->customer_service->message($wenti)->to($officialOpenId)->send(); return; }catch (\Throwable $exception){ $instance->info("异常信息:".$exception->getMessage()); $wenti = new Text("请换个商品,该商品没有返利"); $app->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); } /** * 获取公众号配置 */ public static function accountConfigLogic() { $params = request()->all(); $account = WechatAccountModel::findByWechatAppCode($params["wechat_app_code"]); $data = BaseUtil::getJsapiSign($account->wechat_app_id,$account->wechat_app_secret,$params["url"]); return $data; } /** * 微信公众号授权回调 */ public static function accountCallbackLogic() { $instance = new LoggerFactoryUtil(WechatLogic::class); $params = request()->all(); $instance->info("微信回调数据:".json_encode($params)); //切割公众号和邀请人 $arr = explode("|",$params["state"]); $wechatAccount = WechatAccountModel::findByWechatAppId($arr[0]); //获取站点信息 $webSite = WebSiteModel::findByWechatAccountId($wechatAccount->id); $app = WechatAccountUtil::getAppByID($wechatAccount->id); try{ $userInfo = $app->oauth->user()->getOriginal(); }catch (\Throwable $exception){ if(env("APP_ENV")=="local"){ $url = "http://127.0.0.1:8080"; }else{ $url = "http://".$webSite->web_site_host; } return $url; } $instance->info("用户信息:".json_encode($userInfo)); //判断用户是否存在 $user = UserModel::findByAccountOpenId($userInfo["openid"]); $inviteUserId = 0; if($arr[1]){ //存在推荐人 $inviteUserId = $arr[1]; } if(!$user){ //不存在则创建 $userId = UserModel::query()->insertGetId( [ "wechat_account_id"=>$wechatAccount->id, "web_site_id"=>$webSite->id, "user_open_id"=>$userInfo["openid"], "user_head_img_url"=>$userInfo["headimgurl"], "user_nickname"=>$userInfo["nickname"], "user_sex"=>$userInfo["sex"], "user_api_key"=>uniqid("fbt"), "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"), "invite_user_id"=>$inviteUserId ] ); }else{ //存在则更新用户头像 UserModel::query() ->where("id",$user->id) ->update( [ "user_head_img_url"=>$userInfo["headimgurl"], "user_nickname"=>$userInfo["nickname"], "wechat_account_id"=>$wechatAccount->id, "web_site_id"=>$webSite->id ] ); $userId = $user->id; } $token = md5(uniqid("fbt")); BaseUtil::setRedisCache($token,$userId,24*3600); if(env("APP_ENV")=="local"){ $url = "http://127.0.0.1:8080?wechatToken=".$token; }else{ $url = "http://".$webSite->web_site_host."?wechatToken=".$token; } $instance->info("回调地址:".$url); return $url; } }