123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673 |
- <?php
- namespace App\Http\Logic;
- use App\Http\Bean\Util\Pdd\Ddk\GoodsPromotionUrlGenerateParamBean;
- use App\Http\Bean\Util\Pdd\Ddk\GoodsSearchParamBean;
- use App\Http\Enum\MeiTuanLinkTypeEnum;
- use App\Http\Enum\PlatformTypeEnum;
- use App\Http\Enum\WechatAccountMenuTypeEnum;
- use App\Http\Utils\BaseUtil;
- use App\Http\Utils\Jutuike\JutuikeUtil;
- use App\Http\Utils\LoggerFactoryUtil;
- use App\Http\Utils\Meituan\MeituanLianmengUtil;
- use App\Http\Utils\Pdd\DuoDuoKeUtil;
- use App\Http\Utils\TaoBao\TaobaoLianMengUtil;
- use App\Http\Utils\WechatAccountUtil;
- use App\Models\UserModel;
- use App\Models\WebSiteModel;
- use App\Models\WechatAccountMenuConfigModel;
- use App\Models\WechatAccountModel;
- use EasyWeChat\Kernel\Messages\Image;
- use EasyWeChat\Kernel\Messages\Text;
- class WechatLogic extends BaseLogic
- {
-
- public static function checkServerlogic($code)
- {
- $app = WechatAccountUtil::getApp($code);
- $instance = new LoggerFactoryUtil(WechatLogic::class);
- $app->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;
- }
-
- 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){
-
-
- $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":
-
-
- $msg1 = new Image(env("WECHAT_IMAGE_MEDIA_ID"));
- $msg2 = <<<S
- $nickname
- 终于等到你啦!谢谢关注【返不停】
- 点击公众号菜单领取外卖大红包
- 回复关键字【帮助】,可查看各大电商平台返利使用步骤
- S;
- $msg2 = new Text($msg2);
- $res1 = $app->customer_service->message($msg1)->to($officialOpenId)->send();
- $res2 = $app->customer_service->message($msg2)->to($officialOpenId)->send();
- $instance->info('结果1:'.json_encode($res1));
- break;
- case "unsubscribe":
- break;
- case "CLICK":
- $date = date("m月d日");
- switch ($message["EventKey"]){
- case "elmwaimai":
-
- $url = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020002597");
- $link = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
- data-miniprogram-path='".$url."'
- href=".'"http://www.qq.com"'.">点我领取饿了么外卖红包</a>";
- break;
- case "elmxianshi":
-
- $url = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020004284");
- $link = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
- data-miniprogram-path='".$url."'
- href=".'"http://www.qq.com"'.">点我领取饿了么限时红包</a>";
- break;
- case "elmzhaji":
-
- $url = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020005049");
- $link = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
- data-miniprogram-path='".$url."'
- href=".'"http://www.qq.com"'.">点我领取饿了么炸鸡红包</a>";
- break;
- case "elmxiawu":
-
- $url = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020004425");
- $link = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
- data-miniprogram-path='".$url."'
- href=".'"http://www.qq.com"'.">点我领取饿了么下午茶红包</a>";
- break;
- case "mtwaimai":
-
-
- $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_MEITUAN,$userId);
- $url = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,2,MeiTuanLinkTypeEnum::H5);
- $link = "<a href='".$url."'>点我领取美团外卖红包</a>";
- break;
- case "mtjiudian":
-
-
- $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_MEITUAN,$userId);
- $url = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,7,MeiTuanLinkTypeEnum::MINIPROGRAME);
- $link = "<a data-miniprogram-appid=".'"wxde8ac0a21135c07d" '."
- data-miniprogram-path='".$url."'
- href=".'"http://www.qq.com"'.">点我领取美团酒店红包</a>";
- break;
- case "mtyouxuan":
-
-
- $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_MEITUAN,$userId);
- $url = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,22,MeiTuanLinkTypeEnum::MINIPROGRAME);
- $link = "<a data-miniprogram-appid=".'"wxde8ac0a21135c07d" '."
- data-miniprogram-path='".$url."'href=".'"http://www.qq.com"'.">点我领取美团优选红包</a>";
- break;
- case "mtshangou":
-
-
- $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_MEITUAN,$userId);
- $url = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,4,MeiTuanLinkTypeEnum::H5);
- $link = "<a href='".$url."'>点我领取美团闪购红包</a>";
- break;
- case "help":
-
- $msg = new Image(env("WECHAT_IMAGE_MEDIA_ID"));
- $res = $app->customer_service->message($msg)->to($officialOpenId)->send();
- return;
- break;
- }
- $msg = <<<S
- Hi,$nickname ,$date 红包已更新
- [红包] $link
- 祝用餐愉快
- S;
- $msg = new Text($msg);
- $res = $app->customer_service->message($msg)->to($officialOpenId)->send();
- $instance->info("返回结果:".json_encode($res));
- }
- }
-
- 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);
-
- $jutuikeSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_JUTUIKE,$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;
- }
- $wenti = new Text("稍等");
- $app->customer_service->message($wenti)->to($officialOpenId)->send();
- try{
-
- $res = preg_match("/https\:\/\/mobile\.yangkeduo\.com/",$message["Content"],$match);
- if($res){
-
- $instance->info("匹配到拼多多链接");
-
- $bean = new GoodsSearchParamBean(
- [
- "keyword"=>$message["Content"],
- "pid"=>"23985775_220421267"
- ]
- );
- $goodsData = DuoDuoKeUtil::goodsSearch($bean);
- $instance->info("拼多多返回商品详情数据:".json_encode($goodsData));
-
- $oauthRes = DuoDuoKeUtil::memberAuthorityQuery($platformSid);
- $instance->info("备案信息:".json_encode($oauthRes));
-
- 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"];
- }else{
-
-
- $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"];
- }
- $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 = <<<S
- 【 $goodsTitle 】
- 【原价】:$goodsPrice 元
- 【优惠券】:$couponPrice 元
- 【券后价】:$afterCouponPrice 元
- 【返现金额】:$rebeatPrice 元
- -------------------------
- 点击链接购买:$url
- 具体返现金额,以实际支付金额为准
- 拼多多存在比价订单(无佣金):自己登录后的查看详情页复制链接,进行转链
- 解决方法:退出app,浏览需要购买的商品页,然后复制链接转链,即可避免
- -------------------------
- 进入公众号=>我的=>钱包,可以1:1提现哦
- S;
- $wenti = new Text($msg);
- $app->customer_service->message($wenti)->to($officialOpenId)->send();
- return;
- }
-
- $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 = <<<S
- 【 $goodsTitle 】
- 【原价】:$goodsPrice 元
- 【优惠券】:$couponPrice 元
- 【券后价】:$afterCouponPrice 元
- 【返现金额】:$rebeatPrice 元
- -------------------------
- 点击链接购买:$url
- 具体返现金额,以实际支付金额为准
- -------------------------
- 进入公众号=>我的=>钱包,可以1:1提现哦
- S;
- $wenti = new Text($msg);
- $app->customer_service->message($wenti)->to($officialOpenId)->send();
- return;
- }
-
- $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 = <<<S
- 【 $goodsTitle 】
- 【原价】:$goodsPrice 元
- 【优惠券】:$couponPrice 元
- 【券后价】:$afterCouponPrice 元
- 【返现金额】:$rebeatPrice 元
- -------------------------
- 点击链接购买:$url
- 具体返现金额,以实际支付金额为准
- -------------------------
- 进入公众号=>我的=>钱包,可以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));
- $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 = <<<S
- 【 $goodsTitle 】
- 【原价】:$goodsPrice 元
- 【优惠券】:$couponPrice 元
- 【券后价】:$afterCouponPrice 元
- 【返现金额】:$rebeatPrice 元
- -------------------------
- 复制这条信息:$url
- 打开【手机淘宝】即可查看
- 具体返现金额,以实际支付金额为准
- -------------------------
- 进入公众号=>我的=>钱包,可以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();
-
- $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;
- }
- }
|