123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- <?php
- namespace App\Http\Logic;
- use App\Http\Enum\MeiTuanLinkTypeEnum;
- use App\Http\Enum\PlatformTypeEnum;
- use App\Http\Enum\WechatAccountMenuTypeEnum;
- use App\Http\Utils\BaseUtil;
- use App\Http\Utils\LoggerFactoryUtil;
- use App\Http\Utils\Meituan\MeituanLianmengUtil;
- use App\Http\Utils\WechatAccountUtil;
- use App\Models\UserModel;
- use App\Models\WechatAccountMenuConfigModel;
- 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);
- 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 = "<a data-miniprogram-appid=".'"wxde8ac0a21135c07d" '."
- data-miniprogram-path='".$val."'
- href=".'"http://www.qq.com"'.">[红包] 美团超大外卖红包</a>";
- $msg2 = <<<S
- $nickname
- 终于等到你啦!谢谢关注ლ(╹◡╹ლ)
- 【外卖超省钱】点击立即 $link
- 【赚钱小窍门】
- 1. 自己注册使用淘客兔账号
- 2. 邀请更多好友一起注册使用、
- 3. 自用省钱,分享赚钱 √get
- 【更多福利】点击下方菜单栏,领取超多超值福利 ↓↓↓
- 【注意】有任何问题,点击 菜单-联系客服
- 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"://点击时间
- if($message["EventKey"]=="wenti"){
- $link1 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=领取外卖红包遇到问题&msgmenuid='.uniqid().'"'.">1.领取外卖红包遇到问题</a>";
- $link2 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=话费充值问题&msgmenuid='.uniqid().'"'.">2.话费充值问题</a>";
- $link3 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=美团代金券问题&msgmenuid='.uniqid().'"'.">3.美团代金券问题</a>";
- $link4 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=超级吃货卡问题&msgmenuid='.uniqid().'"'.">4.超级吃货卡问题</a>";
- $link5 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=佣金提现问题&msgmenuid='.uniqid().'"'.">5.佣金提现问题</a>";
- $link6 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=其他问题&msgmenuid='.uniqid().'"'.">6.其他问题</a>";
- //常见问题
- $response = <<<STR
- 请问您遇到了什么问题呢?(点击下方蓝字)
- $link1
- $link2
- $link3
- $link4
- $link5
- $link6
- STR;
- $wenti = new Text($response);
- $app->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 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=领了外卖红包,付款时却找不到可用红包&msgmenuid='.uniqid().'"'.">1.领了外卖红包,付款时却找不到可用红包</a>";
- $link2 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=如何更换领取红包的手机号&msgmenuid='.uniqid().'"'.">2.如何更换领取红包的手机号</a>";
- $response = <<<ST
- 请问您遇到了什么问题呢?(点击下方蓝字)
- $link1
- $link2
- ST;
- $wenti = new Text($response);
- break;
- case "领了外卖红包,付款时却找不到可用红包":
- $link1 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=更换手机号&msgmenuid='.uniqid().'"'."><更换手机号></a>";
- $response = <<<S
- 领取的红包不能用?可能是以下问题导致的:
- 1.你所选的店铺并不在红包可使用店铺范围;
- 2.你的订单金额还未达到红包满减门槛;
- 3.你领红包时所用的手机号与点外卖时所用的手机号不一致
- $link1
- S;
- $wenti = new Text($response);
- break;
- case "更换手机号":
- $link1 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=更换饿了么绑定手机号&msgmenuid='.uniqid().'"'.">1. 更换饿了么绑定手机号</a>";
- $link2 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=更换美团绑定手机号&msgmenuid='.uniqid().'"'.">2. 更换美团绑定手机号</a>";
- $response = <<<S
- 如何更换领红包时所用的手机号
- $link1
- $link2
- S;
- $wenti = new Text($response);
- break;
- case "如何更换领取红包的手机号":
- $link1 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=更换饿了么绑定手机号&msgmenuid='.uniqid().'"'.">1. 更换饿了么绑定手机号</a>";
- $link2 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=更换美团绑定手机号&msgmenuid='.uniqid().'"'.">2. 更换美团绑定手机号</a>";
- $response = <<<S
- 如何更换领红包时所用的手机号
- $link1
- $link2
- S;
- $wenti = new Text($response);
- break;
- case "更换饿了么绑定手机号":
- $response = <<<H
- 请根据以下步骤修改饿了么绑定手机号 :
- 1.打开饿了么外卖小程序;
- 2.点击右下角【我的】,再点击头像昵称退出登录;
- 3.重新进入【我的】页面,点击【立即登录】进入微信授权;
- 4.点击管理手机号码,输入新的手机号并验证,然后确认登录即可
- H;
- $wenti = new Text($response);
- break;
- case "更换美团绑定手机号":
- $response = <<<T
- 请根据以下步骤修改美团绑定手机号:
- 1.进入美团外卖小程序,点击右下角【我的】退出当前账号;
- 3.重新点击登录,进入微信授权;
- 4.点击管理手机号码,输入新的手机号并验证,然后确认登录即可
- T;
- $wenti = new Text($response);
- break;
- case "话费充值问题":
- $link1 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=联系客服&msgmenuid='.uniqid().'"'."><联系客服></a>";
- $response = <<<T
- 话费充值相关问题
- 1. 慢充方式充值话费到账时间为4~72小时,请耐心等待。
- 2. 充值金额可能会分笔到,如充100元,可能会分50 + 50到账,请您放心。
- 3. 超过72小时未到账,请联系客服申请退款$link1
- T;
- $wenti = new Text($response);
- break;
- case "美团代金券问题":
- $response = <<<Q
- 美团代金券相关问题
- 1. 购买的代金券迟迟不到账,请核对您购买时输入的手机号是否与美团使用的手机号一致。
- Q;
- $wenti = new Text($response);
- break;
- case "超级吃货卡问题":
- $response = <<<T
- 饿了么超级吃货卡相关问题
- 1. 购买的吃货卡未生效,请核对购买时输入的手机号与饿了么使用的手机号是否一致。
- 2. 吃货卡到账但是没有无门槛红包?无门槛红包每个月只在月初赠送一次,如吃货卡到账却没有无门槛红包,说明当月的红包已经赠送过,请耐心等待下个月月初。
- T;
- $wenti = new Text($response);
- break;
- case "佣金提现问题":
- $response = <<<T
- 佣金提现问题
- 1. 累计收益达到10元时方可提现,提现收取手续费和服务费。
- 2. 申请提现需要绑定手机号,设置支付密码,关注公众号。
- 3. 提现款项将由公众号转入用户微信账户,选择的打款方式不同,到账时间将有所不同。由于腾讯的微信钱款相关限制,会存在部分提现分多笔转入用户账户。
- 4. 因个别企业政策,部分收益款项会存在含税金额,提现时可提交发票;如无发票,淘客兔将会从提现金额中代扣相关税点。
- T;
- $wenti = new Text($response);
- break;
- case "联系客服":
- $response = <<<T
- 其他问题请联系客服解决
- T;
- $wenti = new Text($response);
- break;
- case "其他问题":
- $response = <<<T
- 其他问题请联系客服解决
- T;
- $wenti = new Text($response);
- break;
- default:
- $response = <<<T
- 其他问题请联系客服解决
- T;
- $wenti = new Text($response);
- break;
- }
- $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);
- }
- }
|