WechatLogic.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. namespace App\Http\Logic;
  3. use App\Http\Enum\MeiTuanLinkTypeEnum;
  4. use App\Http\Enum\PlatformTypeEnum;
  5. use App\Http\Enum\WechatAccountMenuTypeEnum;
  6. use App\Http\Utils\BaseUtil;
  7. use App\Http\Utils\LoggerFactoryUtil;
  8. use App\Http\Utils\Meituan\MeituanLianmengUtil;
  9. use App\Http\Utils\WechatAccountUtil;
  10. use App\Models\UserModel;
  11. use App\Models\WechatAccountMenuConfigModel;
  12. use EasyWeChat\Kernel\Messages\Image;
  13. use EasyWeChat\Kernel\Messages\Text;
  14. class WechatLogic extends BaseLogic
  15. {
  16. /**
  17. * 校验微信服务器
  18. */
  19. public static function checkServerlogic($code)
  20. {
  21. $app = WechatAccountUtil::getApp($code);
  22. $instance = new LoggerFactoryUtil(WechatLogic::class);
  23. $app->server->push(function ($message)use ($instance,$app,$code) {
  24. $instance->info("信息:".json_encode($message));
  25. switch ($message['MsgType']) {
  26. case 'event'://事件
  27. self::handleClickEvent($message,$app);
  28. break;
  29. case 'text'://文字
  30. self::handleTxt($message,$app,$code);
  31. break;
  32. case 'image'://图片
  33. break;
  34. case 'voice'://语音
  35. break;
  36. case 'video'://视频
  37. break;
  38. case 'location'://坐标
  39. break;
  40. case 'link'://链接
  41. break;
  42. case 'file'://文件
  43. // ... 其它消息
  44. default:
  45. break;
  46. }
  47. });
  48. $response = $app->server->serve();
  49. return $response;
  50. }
  51. /**
  52. * 处理事件
  53. * @param $message array 时间信息
  54. * @param $app \EasyWeChat\OfficialAccount\Application
  55. * @throws \App\Exceptions\CommonException
  56. */
  57. public static function handleClickEvent($message,$app)
  58. {
  59. $instance = new LoggerFactoryUtil(WechatLogic::class);
  60. $officialOpenId = $message["FromUserName"];
  61. $log = new LoggerFactoryUtil(WechatLogic::class);
  62. switch ($message["Event"]){
  63. case "subscribe"://关注公众号
  64. $user = $app->user->get($officialOpenId);
  65. $log->info("用户信息".json_encode($user));
  66. $nickname = $user["nickname"];
  67. //判断用户是否存在
  68. $daogoUser = UserModel::query()->where("user_open_id",$officialOpenId)->first();
  69. if(!$daogoUser){
  70. //不存在则写入用户
  71. $userId = UserModel::query()->insertGetId(
  72. [
  73. "user_open_id"=>$officialOpenId,
  74. "user_head_img_url"=>$user["headimgurl"]??"",
  75. "user_nickname"=>$user["nickname"]??"",
  76. "user_sex"=>$user["sex"]??1,
  77. "user_api_key"=>md5(microtime(true)),
  78. "user_api_key_expire_time"=>date("Y-m-d H:i:s",strtotime("+1 year")),
  79. "created_at"=>date("Y-m-d H:i:s"),
  80. "updated_at"=>date("Y-m-d H:i:s"),
  81. ]
  82. );
  83. }else{
  84. $userId = $daogoUser->id;
  85. }
  86. //获取美团的推广链接
  87. $meituanKey = "meituanwaimai:".$userId;
  88. $val = BaseUtil::getCacheFromRedis($meituanKey);
  89. $instance->info("1");
  90. if(!$val){
  91. $instance->info("2");
  92. //缓存中不存在则从官方获取
  93. $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_MEITUAN,$userId);
  94. $val = MeituanLianmengUtil::generateLink(2,$platformSid,MeiTuanLinkTypeEnum::MINIPROGRAME,env("MEITUAN_LIANMENG_KEY"));
  95. $instance->info($val);
  96. BaseUtil::setRedisCache($meituanKey,$val,3600*24);
  97. }
  98. $instance->info("到这里里");
  99. //判断EventKey是否存在
  100. $msg1 = new Text('欢迎关注淘客兔!');
  101. $link = "<a data-miniprogram-appid=".'"wxde8ac0a21135c07d" '."
  102. data-miniprogram-path='".$val."'
  103. href=".'"http://www.qq.com"'.">美团超大外卖红包</a>";
  104. $msg2 = <<<S
  105. $nickname
  106. 终于等到你啦!谢谢关注ლ(╹◡╹ლ)
  107. 【外卖超省钱】点击立即 [红包] $link
  108. 【赚钱小窍门】
  109. 1. 自己注册使用淘客兔账号
  110. 2. 邀请更多好友一起注册使用、
  111. 3. 自用省钱,分享赚钱 √get
  112. 【更多福利】点击下方菜单栏,领取超多超值福利 ↓↓↓
  113. 【注意】有任何问题,点击 菜单-联系客服
  114. S;
  115. $msg2 = new Text($msg2);
  116. $res1 = $app->customer_service->message($msg1)->to($officialOpenId)->send();
  117. $res2 = $app->customer_service->message($msg2)->to($officialOpenId)->send();
  118. $instance->info('结果1:'.json_encode($res1));
  119. $instance->info('结果2:'.json_encode($res2));
  120. break;
  121. case "unsubscribe"://取关公众号
  122. break;
  123. case "CLICK"://点击时间
  124. if($message["EventKey"]=="wenti"){
  125. $link1 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=领取外卖红包遇到问题&msgmenuid='.uniqid().'"'.">1.领取外卖红包遇到问题</a>";
  126. $link2 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=话费充值问题&msgmenuid='.uniqid().'"'.">2.话费充值问题</a>";
  127. $link3 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=美团代金券问题&msgmenuid='.uniqid().'"'.">3.美团代金券问题</a>";
  128. $link4 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=超级吃货卡问题&msgmenuid='.uniqid().'"'.">4.超级吃货卡问题</a>";
  129. $link5 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=佣金提现问题&msgmenuid='.uniqid().'"'.">5.佣金提现问题</a>";
  130. $link6 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=其他问题&msgmenuid='.uniqid().'"'.">6.其他问题</a>";
  131. //常见问题
  132. $response = <<<STR
  133. 请问您遇到了什么问题呢?(点击下方蓝字)
  134. $link1
  135. $link2
  136. $link3
  137. $link4
  138. $link5
  139. $link6
  140. STR;
  141. $wenti = new Text($response);
  142. $app->customer_service->message($wenti)->to($officialOpenId)->send();
  143. }elseif($message["EventKey"]=="lianxi"){
  144. $mediaId = "VlvMS-GXy-81ZmdCPPsSsvpn8c6DBbofpWQAOnztQG0UA7FzAjXwnBXs5II0-3tB";
  145. $lianxi = new Image($mediaId);
  146. $app->customer_service->message($lianxi)->to($officialOpenId)->send();
  147. }
  148. break;
  149. }
  150. }
  151. /**
  152. * 处理文字
  153. * @param $message
  154. * @param $app Application
  155. */
  156. public static function handleTxt($message,$app,$code)
  157. {
  158. $instance = new LoggerFactoryUtil(WechatLogic::class);
  159. $officialOpenId = $message["FromUserName"];
  160. switch ($message["Content"]){
  161. case "领取外卖红包遇到问题":
  162. $link1 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=领了外卖红包,付款时却找不到可用红包&msgmenuid='.uniqid().'"'.">1.领了外卖红包,付款时却找不到可用红包</a>";
  163. $link2 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=如何更换领取红包的手机号&msgmenuid='.uniqid().'"'.">2.如何更换领取红包的手机号</a>";
  164. $response = <<<ST
  165. 请问您遇到了什么问题呢?(点击下方蓝字)
  166. $link1
  167. $link2
  168. ST;
  169. $wenti = new Text($response);
  170. break;
  171. case "领了外卖红包,付款时却找不到可用红包":
  172. $link1 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=更换手机号&msgmenuid='.uniqid().'"'."><更换手机号></a>";
  173. $response = <<<S
  174. 领取的红包不能用?可能是以下问题导致的:
  175. 1.你所选的店铺并不在红包可使用店铺范围;
  176. 2.你的订单金额还未达到红包满减门槛;
  177. 3.你领红包时所用的手机号与点外卖时所用的手机号不一致
  178. $link1
  179. S;
  180. $wenti = new Text($response);
  181. break;
  182. case "更换手机号":
  183. $link1 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=更换饿了么绑定手机号&msgmenuid='.uniqid().'"'.">1. 更换饿了么绑定手机号</a>";
  184. $link2 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=更换美团绑定手机号&msgmenuid='.uniqid().'"'.">2. 更换美团绑定手机号</a>";
  185. $response = <<<S
  186. 如何更换领红包时所用的手机号
  187. $link1
  188. $link2
  189. S;
  190. $wenti = new Text($response);
  191. break;
  192. case "如何更换领取红包的手机号":
  193. $link1 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=更换饿了么绑定手机号&msgmenuid='.uniqid().'"'.">1. 更换饿了么绑定手机号</a>";
  194. $link2 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=更换美团绑定手机号&msgmenuid='.uniqid().'"'.">2. 更换美团绑定手机号</a>";
  195. $response = <<<S
  196. 如何更换领红包时所用的手机号
  197. $link1
  198. $link2
  199. S;
  200. $wenti = new Text($response);
  201. break;
  202. case "更换饿了么绑定手机号":
  203. $response = <<<H
  204. 请根据以下步骤修改饿了么绑定手机号 :
  205. 1.打开饿了么外卖小程序;
  206. 2.点击右下角【我的】,再点击头像昵称退出登录;
  207. 3.重新进入【我的】页面,点击【立即登录】进入微信授权;
  208. 4.点击管理手机号码,输入新的手机号并验证,然后确认登录即可
  209. H;
  210. $wenti = new Text($response);
  211. break;
  212. case "更换美团绑定手机号":
  213. $response = <<<T
  214. 请根据以下步骤修改美团绑定手机号:
  215. 1.进入美团外卖小程序,点击右下角【我的】退出当前账号;
  216. 3.重新点击登录,进入微信授权;
  217. 4.点击管理手机号码,输入新的手机号并验证,然后确认登录即可
  218. T;
  219. $wenti = new Text($response);
  220. break;
  221. case "话费充值问题":
  222. $link1 = "<a href=".'"weixin://bizmsgmenu?msgmenucontent=联系客服&msgmenuid='.uniqid().'"'."><联系客服></a>";
  223. $response = <<<T
  224. 话费充值相关问题
  225. 1. 慢充方式充值话费到账时间为4~72小时,请耐心等待。
  226. 2. 充值金额可能会分笔到,如充100元,可能会分50 + 50到账,请您放心。
  227. 3. 超过72小时未到账,请联系客服申请退款$link1
  228. T;
  229. $wenti = new Text($response);
  230. break;
  231. case "美团代金券问题":
  232. $response = <<<Q
  233. 美团代金券相关问题
  234. 1. 购买的代金券迟迟不到账,请核对您购买时输入的手机号是否与美团使用的手机号一致。
  235. Q;
  236. $wenti = new Text($response);
  237. break;
  238. case "超级吃货卡问题":
  239. $response = <<<T
  240. 饿了么超级吃货卡相关问题
  241. 1. 购买的吃货卡未生效,请核对购买时输入的手机号与饿了么使用的手机号是否一致。
  242. 2. 吃货卡到账但是没有无门槛红包?无门槛红包每个月只在月初赠送一次,如吃货卡到账却没有无门槛红包,说明当月的红包已经赠送过,请耐心等待下个月月初。
  243. T;
  244. $wenti = new Text($response);
  245. break;
  246. case "佣金提现问题":
  247. $response = <<<T
  248. 佣金提现问题
  249. 1. 累计收益达到10元时方可提现,提现收取手续费和服务费。
  250. 2. 申请提现需要绑定手机号,设置支付密码,关注公众号。
  251. 3. 提现款项将由公众号转入用户微信账户,选择的打款方式不同,到账时间将有所不同。由于腾讯的微信钱款相关限制,会存在部分提现分多笔转入用户账户。
  252. 4. 因个别企业政策,部分收益款项会存在含税金额,提现时可提交发票;如无发票,淘客兔将会从提现金额中代扣相关税点。
  253. T;
  254. $wenti = new Text($response);
  255. break;
  256. case "联系客服":
  257. $response = <<<T
  258. 其他问题请联系客服解决
  259. T;
  260. $wenti = new Text($response);
  261. break;
  262. case "其他问题":
  263. $response = <<<T
  264. 其他问题请联系客服解决
  265. T;
  266. $wenti = new Text($response);
  267. break;
  268. default:
  269. $response = <<<T
  270. 其他问题请联系客服解决
  271. T;
  272. $wenti = new Text($response);
  273. break;
  274. }
  275. $app->customer_service->message($wenti)->to($officialOpenId)->send();
  276. }
  277. /**
  278. * 设置公众号菜单逻辑
  279. */
  280. public static function setMenuLogic()
  281. {
  282. $params = request()->all();
  283. //1.获取所有上线的一级菜单
  284. $menus = WechatAccountMenuConfigModel::getMenuByParentId(0);
  285. $configs = [];
  286. foreach ($menus as $menu){
  287. switch ($menu->menu_type){
  288. case WechatAccountMenuTypeEnum::CLICK:
  289. //点击菜单
  290. $configs[] = [
  291. "type" => "click",
  292. "name" => $menu->menu_name,
  293. "key" => $menu->menu_key
  294. ];
  295. break;
  296. case WechatAccountMenuTypeEnum::LINK:
  297. //商品链接
  298. $configs[] = [
  299. "type" => "view",
  300. "name" => $menu->menu_name,
  301. "url" => $menu->menu_url
  302. ];
  303. break;
  304. case WechatAccountMenuTypeEnum::MENU:
  305. //菜单
  306. //获取下级菜单
  307. $tmps = WechatAccountMenuConfigModel::getMenuByParentId($menu->id);
  308. $subMenus = [];
  309. foreach ($tmps as $tmp){
  310. switch ($tmp->menu_type) {
  311. case WechatAccountMenuTypeEnum::CLICK:
  312. //点击菜单
  313. $subMenus[] = [
  314. "type" => "click",
  315. "name" => $tmp->menu_name,
  316. "key" => $tmp->menu_key
  317. ];
  318. break;
  319. case WechatAccountMenuTypeEnum::LINK:
  320. //链接
  321. $subMenus[] = [
  322. "type" => "view",
  323. "name" => $tmp->menu_name,
  324. "url" => $tmp->menu_url
  325. ];
  326. break;
  327. }
  328. }
  329. $configs[] = [
  330. "name"=>$menu->menu_name,
  331. "sub_button"=>$subMenus
  332. ];
  333. break;
  334. }
  335. }
  336. $app = WechatAccountUtil::getApp($params["wechat_app_code"]);
  337. $instance = new LoggerFactoryUtil(WechatLogic::class);
  338. $instance->info("菜单:".json_encode($configs));
  339. return $app->menu->create($configs);
  340. }
  341. }