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;
- }
- /**
- * 处理事件
- * @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 = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
- //data-miniprogram-path='".$elmUrl1."'
- //href=".'"http://www.qq.com"'.">饿了么超大外卖红包</a>";
- // $elmLink2 = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
- //data-miniprogram-path='".$elmUrl2."'
- //href=".'"http://www.qq.com"'.">饿了么超大限时红包</a>";
- // $elmLink3 = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
- //data-miniprogram-path='".$elmUrl3."'
- //href=".'"http://www.qq.com"'.">饿了么最新红包</a>";
- // $elmLink4 = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
- //data-miniprogram-path='".$elmUrl4."'
- //href=".'"http://www.qq.com"'.">饿了么大额红包</a>";
- //判断EventKey是否存在
- $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));
- // $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 = "<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 data-miniprogram-appid=".'"wxde8ac0a21135c07d" '."
- //data-miniprogram-path='".$url."'
- //href=".'"http://www.qq.com"'.">点我领取美团外卖红包</a>";
- $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>";
- // $link = "<a href='".$url."'>点我领取美团优选红包</a>";
- break;
- case "mtshangou":
- //美团闪购红包
- //获取美团的推广链接
- $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_MEITUAN,$userId);
- $url = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,4,MeiTuanLinkTypeEnum::H5);
- // $link = "<a data-miniprogram-appid=".'"wxde8ac0a21135c07d" '."
- //data-miniprogram-path='".$url."'
- //href=".'"http://www.qq.com"'.">点我领取美团闪购红包</a>";
- $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));
- }
- }
- /**
- * 处理文字
- * @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);
- //判断是否是帮助
- 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{
- //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 = <<<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("拼多多处理过的链接:".$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 = "<a href='".$url."'>点我购买,即刻拿优惠</a>";
- }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 = "<a href='".$url."'>点击备案之后,直接购买</a>";
- }
- $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;
- }
- //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 = <<<S
- 【 $goodsTitle 】
- 【原价】:$goodsPrice 元
- 【优惠券】:$couponPrice 元
- 【券后价】:$afterCouponPrice 元
- 【返现金额】:$rebeatPrice 元
- -------------------------
- 点击链接购买:$url
- 具体返现金额,以实际支付金额为准
- -------------------------
- 进入公众号=>我的=>钱包,可以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 = <<<S
- 【 $goodsTitle 】
- 【原价】:$goodsPrice 元
- 【优惠券】:$couponPrice 元
- 【券后价】:$afterCouponPrice 元
- 【返现金额】:$rebeatPrice 元
- -------------------------
- 点击链接购买:$url
- 具体返现金额,以实际支付金额为准
- -------------------------
- 进入公众号=>我的=>钱包,可以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 = <<<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();
- //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;
- }
- }
|