WechatLogic.php 17 KB

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