WechatLogic.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. <?php
  2. namespace App\Http\Logic;
  3. use App\Http\Bean\Util\Pdd\Ddk\GoodsPromotionUrlGenerateParamBean;
  4. use App\Http\Bean\Util\Pdd\Ddk\GoodsSearchParamBean;
  5. use App\Http\Enum\MeiTuanLinkTypeEnum;
  6. use App\Http\Enum\PlatformTypeEnum;
  7. use App\Http\Enum\WechatAccountMenuTypeEnum;
  8. use App\Http\Utils\BaseUtil;
  9. use App\Http\Utils\Jutuike\JutuikeUtil;
  10. use App\Http\Utils\LoggerFactoryUtil;
  11. use App\Http\Utils\Meituan\MeituanLianmengUtil;
  12. use App\Http\Utils\Pdd\DuoDuoKeUtil;
  13. use App\Http\Utils\TaoBao\TaobaoLianMengUtil;
  14. use App\Http\Utils\WechatAccountUtil;
  15. use App\Models\UserModel;
  16. use App\Models\WebSiteModel;
  17. use App\Models\WechatAccountMenuConfigModel;
  18. use App\Models\WechatAccountModel;
  19. use EasyWeChat\Kernel\Messages\Image;
  20. use EasyWeChat\Kernel\Messages\Text;
  21. class WechatLogic extends BaseLogic
  22. {
  23. /**
  24. * 校验微信服务器
  25. */
  26. public static function checkServerlogic($code)
  27. {
  28. $app = WechatAccountUtil::getApp($code);
  29. $instance = new LoggerFactoryUtil(WechatLogic::class);
  30. $app->server->push(function ($message)use ($instance,$app,$code) {
  31. $instance->info("信息:".json_encode($message));
  32. switch ($message['MsgType']) {
  33. case 'event'://事件
  34. self::handleClickEvent($message,$app,$code);
  35. break;
  36. case 'text'://文字
  37. self::handleTxt($message,$app,$code);
  38. break;
  39. case 'image'://图片
  40. break;
  41. case 'voice'://语音
  42. break;
  43. case 'video'://视频
  44. break;
  45. case 'location'://坐标
  46. break;
  47. case 'link'://链接
  48. break;
  49. case 'file'://文件
  50. // ... 其它消息
  51. default:
  52. break;
  53. }
  54. });
  55. $response = $app->server->serve();
  56. return $response;
  57. }
  58. /**
  59. * 处理事件
  60. * @param $message array 时间信息
  61. * @param $app \EasyWeChat\OfficialAccount\Application
  62. * @throws \App\Exceptions\CommonException
  63. */
  64. public static function handleClickEvent($message,$app,$code)
  65. {
  66. $instance = new LoggerFactoryUtil(WechatLogic::class);
  67. $officialOpenId = $message["FromUserName"];
  68. $user = $app->user->get($officialOpenId);
  69. $log = new LoggerFactoryUtil(WechatLogic::class);
  70. $log->info("用户信息".json_encode($user));
  71. //判断用户是否存在
  72. $daogoUser = UserModel::query()->where("user_open_id",$officialOpenId)->first();
  73. $instance->info("数据库用户:".json_encode($daogoUser));
  74. //获取公众号信息
  75. $account = WechatAccountModel::findByWechatAppCode($code);
  76. //获取所属站点信息
  77. $webSite = WebSiteModel::findByWechatAccountId($account->id);
  78. if(!$daogoUser){
  79. //不存在则写入用户
  80. //判断是否存在上级用户ID
  81. $inviteUserId = 0;
  82. if($message["Event"]=="subscribe" && ($user["qr_scene"] || $user["qr_scene_str"])){
  83. $instance->info("邀请用户ID:".$user["qr_scene"]."邀请ID".$user["qr_scene_str"]);
  84. if($user["qr_scene_str"]){
  85. $inviteUserId = $user["qr_scene_str"];
  86. }
  87. if($user["qr_scene"]){
  88. $inviteUserId = $user["qr_scene"];
  89. }
  90. }
  91. $userId = UserModel::query()->insertGetId(
  92. [
  93. "invite_user_id"=>$inviteUserId,
  94. "user_open_id"=>$officialOpenId,
  95. "user_head_img_url"=>$user["headimgurl"]??"",
  96. "user_nickname"=>$user["nickname"]??"",
  97. "user_sex"=>$user["sex"]??1,
  98. "user_api_key"=>md5(microtime(true)),
  99. "user_api_key_expire_time"=>date("Y-m-d H:i:s",strtotime("+1 year")),
  100. "created_at"=>date("Y-m-d H:i:s"),
  101. "updated_at"=>date("Y-m-d H:i:s"),
  102. "wechat_account_id"=>$account->id,
  103. "web_site_id"=>$webSite->id
  104. ]
  105. );
  106. }else{
  107. $userId = $daogoUser->id;
  108. }
  109. $nickname = $user["nickname"];
  110. switch ($message["Event"]){
  111. case "subscribe"://关注公众号
  112. //获取美团的推广链接
  113. $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_MEITUAN,$userId);
  114. //外卖
  115. $meituanUrl1 = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,2,MeiTuanLinkTypeEnum::H5);
  116. //闪购
  117. // $meituanUrl2 = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,4,MeiTuanLinkTypeEnum::MINIPROGRAME);
  118. // //酒店
  119. // $meituanUrl3 = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,7,MeiTuanLinkTypeEnum::MINIPROGRAME);
  120. // //优选
  121. // $meituanUrl4 = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,22,MeiTuanLinkTypeEnum::MINIPROGRAME);
  122. // $meituanLink1 = "<a data-miniprogram-appid=".'"wxde8ac0a21135c07d" '."
  123. //data-miniprogram-path='".$meituanUrl1."'
  124. //href=".'"http://www.qq.com"'.">美团超大外卖红包</a>";
  125. $meituanLink1 = "<a href='".$meituanUrl1."'>美团超大外卖红包</a>";
  126. // $meituanLink2 = "<a data-miniprogram-appid=".'"wxde8ac0a21135c07d" '."
  127. //data-miniprogram-path='".$meituanUrl2."'
  128. //href=".'"http://www.qq.com"'.">美团超大闪购红包</a>";
  129. // $meituanLink3 = "<a data-miniprogram-appid=".'"wxde8ac0a21135c07d" '."
  130. //data-miniprogram-path='".$meituanUrl3."'
  131. //href=".'"http://www.qq.com"'.">美团超大酒店红包</a>";
  132. // $meituanLink4 = "<a data-miniprogram-appid=".'"wxde8ac0a21135c07d" '."
  133. //data-miniprogram-path='".$meituanUrl4."'
  134. //href=".'"http://www.qq.com"'.">美团超大优选红包</a>";
  135. //获取饿了么的推广链接
  136. $elmUrl1 = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020002597");
  137. $elmUrl2 = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020004284");
  138. $elmUrl3 = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020005049");
  139. $elmUrl4 = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020004425");
  140. $elmLink1 = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
  141. data-miniprogram-path='".$elmUrl1."'
  142. href=".'"http://www.qq.com"'.">饿了么超大外卖红包</a>";
  143. $elmLink2 = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
  144. data-miniprogram-path='".$elmUrl2."'
  145. href=".'"http://www.qq.com"'.">饿了么超大限时红包</a>";
  146. $elmLink3 = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
  147. data-miniprogram-path='".$elmUrl3."'
  148. href=".'"http://www.qq.com"'.">饿了么最新红包</a>";
  149. $elmLink4 = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
  150. data-miniprogram-path='".$elmUrl4."'
  151. href=".'"http://www.qq.com"'.">饿了么大额红包</a>";
  152. //判断EventKey是否存在
  153. $msg1 = new Text('欢迎关注!');
  154. $msg2 = <<<S
  155. $nickname
  156. 终于等到你啦!谢谢关注
  157. 【返不停】点击领取
  158. 饿了么红包大全(哪个大用哪个)
  159. [红包] $elmLink1
  160. [红包] $elmLink2
  161. [红包] $elmLink3
  162. [红包] $elmLink4
  163. S;
  164. $msg2 = new Text($msg2);
  165. $res1 = $app->customer_service->message($msg1)->to($officialOpenId)->send();
  166. $res2 = $app->customer_service->message($msg2)->to($officialOpenId)->send();
  167. $instance->info('结果1:'.json_encode($res1));
  168. $instance->info('结果2:'.json_encode($res2));
  169. break;
  170. case "unsubscribe"://取关公众号
  171. break;
  172. case "CLICK"://点击事件
  173. $date = date("m月d日");
  174. switch ($message["EventKey"]){
  175. case "elmwaimai":
  176. //饿了么外卖
  177. $url = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020002597");
  178. $link = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
  179. data-miniprogram-path='".$url."'
  180. href=".'"http://www.qq.com"'.">点我领取饿了么外卖红包</a>";
  181. break;
  182. case "elmxianshi":
  183. //饿了么限时红包
  184. $url = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020004284");
  185. $link = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
  186. data-miniprogram-path='".$url."'
  187. href=".'"http://www.qq.com"'.">点我领取饿了么限时红包</a>";
  188. break;
  189. case "elmzhaji":
  190. //饿了么炸鸡红包
  191. $url = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020005049");
  192. $link = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
  193. data-miniprogram-path='".$url."'
  194. href=".'"http://www.qq.com"'.">点我领取饿了么炸鸡红包</a>";
  195. break;
  196. case "elmxiawu":
  197. //饿了么下午茶红包
  198. $url = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020004425");
  199. $link = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
  200. data-miniprogram-path='".$url."'
  201. href=".'"http://www.qq.com"'.">点我领取饿了么下午茶红包</a>";
  202. break;
  203. case "mtwaimai":
  204. //美团外卖红包
  205. //获取美团的推广链接
  206. $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_MEITUAN,$userId);
  207. $url = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,2,MeiTuanLinkTypeEnum::H5);
  208. // $link = "<a data-miniprogram-appid=".'"wxde8ac0a21135c07d" '."
  209. //data-miniprogram-path='".$url."'
  210. //href=".'"http://www.qq.com"'.">点我领取美团外卖红包</a>";
  211. $link = "<a href='".$url."'>点我领取美团外卖红包</a>";
  212. break;
  213. case "mtjiudian":
  214. //美团酒店红包
  215. //获取美团的推广链接
  216. $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_MEITUAN,$userId);
  217. $url = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,7,MeiTuanLinkTypeEnum::MINIPROGRAME);
  218. $link = "<a data-miniprogram-appid=".'"wxde8ac0a21135c07d" '."
  219. data-miniprogram-path='".$url."'
  220. href=".'"http://www.qq.com"'.">点我领取美团酒店红包</a>";
  221. break;
  222. case "mtyouxuan":
  223. //美团优选红包
  224. //获取美团的推广链接
  225. $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_MEITUAN,$userId);
  226. $url = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,22,MeiTuanLinkTypeEnum::MINIPROGRAME);
  227. $link = "<a data-miniprogram-appid=".'"wxde8ac0a21135c07d" '."
  228. data-miniprogram-path='".$url."'href=".'"http://www.qq.com"'.">点我领取美团优选红包</a>";
  229. // $link = "<a href='".$url."'>点我领取美团优选红包</a>";
  230. break;
  231. case "mtshangou":
  232. //美团闪购红包
  233. //获取美团的推广链接
  234. $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_MEITUAN,$userId);
  235. $url = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,4,MeiTuanLinkTypeEnum::H5);
  236. // $link = "<a data-miniprogram-appid=".'"wxde8ac0a21135c07d" '."
  237. //data-miniprogram-path='".$url."'
  238. //href=".'"http://www.qq.com"'.">点我领取美团闪购红包</a>";
  239. $link = "<a href='".$url."'>点我领取美团闪购红包</a>";
  240. break;
  241. case "help":
  242. //使用教程
  243. $msg = <<<S
  244. 1.点击公众号菜单栏,选择需要的红包
  245. 2.点击公众号返回的链接,进入对应的平台领取大红包[红包]
  246. 3.使用红包下单,完成优惠
  247. 红包每天都可以领哦 [开心]
  248. S;
  249. $msg = new Text($msg);
  250. $res = $app->customer_service->message($msg)->to($officialOpenId)->send();
  251. return;
  252. break;
  253. }
  254. $msg = <<<S
  255. Hi,$nickname ,$date 红包已更新
  256. [红包] $link
  257. 祝用餐愉快
  258. S;
  259. $msg = new Text($msg);
  260. $res = $app->customer_service->message($msg)->to($officialOpenId)->send();
  261. $instance->info("返回结果:".json_encode($res));
  262. }
  263. }
  264. /**
  265. * 处理文字
  266. * @param $message
  267. * @param $app Application
  268. */
  269. public static function handleTxt($message,$app,$code)
  270. {
  271. $instance = new LoggerFactoryUtil(WechatLogic::class);
  272. $officialOpenId = $message["FromUserName"];
  273. $instance->info("用户信息:".$message["Content"]);
  274. $daogoUser = UserModel::query()->where("user_open_id",$officialOpenId)->first();
  275. $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_PINGDUODUO,$daogoUser->id);
  276. $instance->info("pid:".$platformSid);
  277. $wenti = new Text("稍等");
  278. $app->customer_service->message($wenti)->to($officialOpenId)->send();
  279. try{
  280. //1.判断是否匹配上拼多多商品链接
  281. $res = preg_match("/https\:\/\/mobile\.yangkeduo\.com/",$message["Content"],$match);
  282. if($res){
  283. $instance->info("匹配到拼多多链接");
  284. //获取商品详情
  285. $bean = new GoodsSearchParamBean(
  286. [
  287. "keyword"=>$message["Content"],
  288. "pid"=>"23985775_220421267"
  289. ]
  290. );
  291. $goodsData = DuoDuoKeUtil::goodsSearch($bean);
  292. $instance->info("拼多多返回商品详情数据:".json_encode($goodsData));
  293. //1.判断推广位是否备案
  294. $oauthRes = DuoDuoKeUtil::memberAuthorityQuery($platformSid);
  295. $instance->info("备案信息:".json_encode($oauthRes));
  296. //2.未备案这授权,已备案则直接获取推广链接
  297. if($oauthRes["authority_query_response"]["bind"]){
  298. //已备案
  299. $data = DuoDuoKeUtil::goodsZsUnitUrlGen($platformSid,$message["Content"]);
  300. $instance->info("拼多多返回数据:".json_encode($data));
  301. $url = $data["goods_zs_unit_generate_response"]["short_url"];
  302. // $link = "<a href='".$url."'>点我购买,即刻拿优惠</a>";
  303. }else{
  304. //未备案,使用自己已备案的pid调用商品搜索接口获取goods_sign
  305. //获取到goods_sign
  306. $goods_sign = $goodsData["goods_search_response"]["goods_list"][0]["goods_sign"];
  307. $beanData = [
  308. "p_id"=>$platformSid,
  309. "goods_sign_list"=>json_encode([$goods_sign]),
  310. "generate_authority_url"=>"true",
  311. ];
  312. $generateBean = new GoodsPromotionUrlGenerateParamBean($beanData);
  313. $res = DuoDuoKeUtil::goodsPromotionUrlGenerate($generateBean);
  314. $instance->info("生成备案信息:".json_encode($res));
  315. $url = $res["goods_promotion_url_generate_response"]["goods_promotion_url_list"][0]["short_url"];
  316. // $link = "<a href='".$url."'>点击备案之后,直接购买</a>";
  317. }
  318. $goodsPrice = round($goodsData["goods_search_response"]["goods_list"][0]["min_group_price"]/100,2);
  319. $couponPrice = round($goodsData["goods_search_response"]["goods_list"][0]["coupon_discount"]/100,2);
  320. $afterCouponPrice = $goodsPrice-$couponPrice;
  321. $rebeatPrice = round($goodsData["goods_search_response"]["goods_list"][0]["promotion_rate"]*$afterCouponPrice*0.7/1000,2);
  322. $goodsTitle = $goodsData["goods_search_response"]["goods_list"][0]["goods_name"];
  323. $msg = <<<S
  324. 【 $goodsTitle 】
  325. 【原价】:$goodsPrice 元
  326. 【优惠券】:$couponPrice 元
  327. 【券后价】:$afterCouponPrice 元
  328. 【返现金额】:$rebeatPrice 元
  329. -------------------------
  330. 点击链接购买:$url
  331. 具体返现金额,以实际支付金额为准
  332. -------------------------
  333. 进入公众号=>我的=>钱包,可以1:1提现哦
  334. S;
  335. $wenti = new Text($msg);
  336. $app->customer_service->message($wenti)->to($officialOpenId)->send();
  337. return;
  338. }
  339. //2.判断是否匹配上淘宝链接
  340. $res = preg_match("/https\:\/\/m\.tb\.cn/",$message["Content"],$match);
  341. //获取聚推客的推广位ID
  342. $jutuikeSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_JUTUIKE,$daogoUser->id);
  343. if($res){
  344. $instance->info("匹配到淘宝链接");
  345. $goodsInfo = JutuikeUtil::transferGoodsLink($message["Content"]);
  346. $tuiguangRes = JutuikeUtil::convert($goodsInfo["goodsId"],$jutuikeSid);
  347. $goodsTitle = $tuiguangRes["goodsName"];
  348. $goodsPrice = $tuiguangRes["marketPrice"];
  349. $couponPrice = $tuiguangRes["couponInfo"]["fav"];
  350. $afterCouponPrice = $tuiguangRes["price"];
  351. $rebeatPrice = round($tuiguangRes["commission"]*0.7,2);
  352. $url = $tuiguangRes["url"];
  353. $msg = <<<S
  354. 【 $goodsTitle 】
  355. 【原价】:$goodsPrice 元
  356. 【优惠券】:$couponPrice 元
  357. 【券后价】:$afterCouponPrice 元
  358. 【返现金额】:$rebeatPrice 元
  359. -------------------------
  360. 复制这条信息:$url
  361. 打开【手机淘宝】即可查看
  362. 具体返现金额,以实际支付金额为准
  363. -------------------------
  364. 进入公众号=>我的=>钱包,可以1:1提现哦
  365. S;
  366. $wenti = new Text($msg);
  367. $app->customer_service->message($wenti)->to($officialOpenId)->send();
  368. return;
  369. }
  370. //3.判断是否匹配到维品会链接
  371. $res = preg_match("/https\:\/\/m\.vip\.com/",$message["Content"],$match);
  372. if($res){
  373. $instance->info("匹配到维品会链接");
  374. $goodsInfo = JutuikeUtil::transferGoodsLink($message["Content"]);
  375. $tuiguangRes = JutuikeUtil::convert($goodsInfo["goodsId"],$jutuikeSid,$goodsInfo["source"]);
  376. $goodsTitle = $tuiguangRes["goodsName"];
  377. $goodsPrice = $tuiguangRes["marketPrice"];
  378. $couponPrice = $tuiguangRes["couponInfo"]["fav"];
  379. $afterCouponPrice = $tuiguangRes["price"];
  380. $rebeatPrice = round($tuiguangRes["commission"]*0.7,2);
  381. $url = $tuiguangRes["url"];
  382. $msg = <<<S
  383. 【 $goodsTitle 】
  384. 【原价】:$goodsPrice 元
  385. 【优惠券】:$couponPrice 元
  386. 【券后价】:$afterCouponPrice 元
  387. 【返现金额】:$rebeatPrice 元
  388. -------------------------
  389. 点击链接购买:$url
  390. 具体返现金额,以实际支付金额为准
  391. -------------------------
  392. 进入公众号=>我的=>钱包,可以1:1提现哦
  393. S;
  394. $wenti = new Text($msg);
  395. $app->customer_service->message($wenti)->to($officialOpenId)->send();
  396. return;
  397. }
  398. //3.判断是否匹配到京东链接
  399. $res = preg_match("/https\:\/\/item\.m\.jd\.com/",$message["Content"],$match);
  400. if($res){
  401. $instance->info("匹配到京东链接");
  402. $goodsInfo = JutuikeUtil::transferGoodsLink($message["Content"]);
  403. $tuiguangRes = JutuikeUtil::convert($goodsInfo["goodsId"],$jutuikeSid,$goodsInfo["source"]);
  404. $goodsTitle = $tuiguangRes["goodsName"];
  405. $goodsPrice = $tuiguangRes["marketPrice"];
  406. $couponPrice = $tuiguangRes["couponInfo"]["fav"];
  407. $afterCouponPrice = $tuiguangRes["price"];
  408. $rebeatPrice = round($tuiguangRes["commission"]*0.7,2);
  409. $url = $tuiguangRes["url"];
  410. $msg = <<<S
  411. 【 $goodsTitle 】
  412. 【原价】:$goodsPrice 元
  413. 【优惠券】:$couponPrice 元
  414. 【券后价】:$afterCouponPrice 元
  415. 【返现金额】:$rebeatPrice 元
  416. -------------------------
  417. 点击链接购买:$url
  418. 具体返现金额,以实际支付金额为准
  419. -------------------------
  420. 进入公众号=>我的=>钱包,可以1:1提现哦
  421. S;
  422. $wenti = new Text($msg);
  423. $app->customer_service->message($wenti)->to($officialOpenId)->send();
  424. return;
  425. }
  426. }catch (\Throwable $exception){
  427. $wenti = new Text("请换个商品,该商品没有返利");
  428. $app->customer_service->message($wenti)->to($officialOpenId)->send();
  429. }
  430. }
  431. /**
  432. * 设置公众号菜单逻辑
  433. */
  434. public static function setMenuLogic()
  435. {
  436. $params = request()->all();
  437. //1.获取所有上线的一级菜单
  438. $menus = WechatAccountMenuConfigModel::getMenuByParentId(0);
  439. $configs = [];
  440. foreach ($menus as $menu){
  441. switch ($menu->menu_type){
  442. case WechatAccountMenuTypeEnum::CLICK:
  443. //点击菜单
  444. $configs[] = [
  445. "type" => "click",
  446. "name" => $menu->menu_name,
  447. "key" => $menu->menu_key
  448. ];
  449. break;
  450. case WechatAccountMenuTypeEnum::LINK:
  451. //商品链接
  452. $configs[] = [
  453. "type" => "view",
  454. "name" => $menu->menu_name,
  455. "url" => $menu->menu_url
  456. ];
  457. break;
  458. case WechatAccountMenuTypeEnum::MENU:
  459. //菜单
  460. //获取下级菜单
  461. $tmps = WechatAccountMenuConfigModel::getMenuByParentId($menu->id);
  462. $subMenus = [];
  463. foreach ($tmps as $tmp){
  464. switch ($tmp->menu_type) {
  465. case WechatAccountMenuTypeEnum::CLICK:
  466. //点击菜单
  467. $subMenus[] = [
  468. "type" => "click",
  469. "name" => $tmp->menu_name,
  470. "key" => $tmp->menu_key
  471. ];
  472. break;
  473. case WechatAccountMenuTypeEnum::LINK:
  474. //链接
  475. $subMenus[] = [
  476. "type" => "view",
  477. "name" => $tmp->menu_name,
  478. "url" => $tmp->menu_url
  479. ];
  480. break;
  481. }
  482. }
  483. $configs[] = [
  484. "name"=>$menu->menu_name,
  485. "sub_button"=>$subMenus
  486. ];
  487. break;
  488. }
  489. }
  490. $app = WechatAccountUtil::getApp($params["wechat_app_code"]);
  491. $instance = new LoggerFactoryUtil(WechatLogic::class);
  492. $instance->info("菜单:".json_encode($configs));
  493. return $app->menu->create($configs);
  494. }
  495. /**
  496. * 获取公众号配置
  497. */
  498. public static function accountConfigLogic()
  499. {
  500. $params = request()->all();
  501. $account = WechatAccountModel::findByWechatAppCode($params["wechat_app_code"]);
  502. $data = BaseUtil::getJsapiSign($account->wechat_app_id,$account->wechat_app_secret,$params["url"]);
  503. return $data;
  504. }
  505. /**
  506. * 微信公众号授权回调
  507. */
  508. public static function accountCallbackLogic()
  509. {
  510. $instance = new LoggerFactoryUtil(WechatLogic::class);
  511. $params = request()->all();
  512. $instance->info("微信回调数据:".json_encode($params));
  513. //切割公众号和邀请人
  514. $arr = explode("|",$params["state"]);
  515. $wechatAccount = WechatAccountModel::findByWechatAppId($arr[0]);
  516. $app = WechatAccountUtil::getAppByID($wechatAccount->id);
  517. $userInfo = $app->oauth->user()->getOriginal();
  518. $instance->info("用户信息:".json_encode($userInfo));
  519. //获取站点信息
  520. $webSite = WebSiteModel::findByWechatAccountId($wechatAccount->id);
  521. //判断用户是否存在
  522. $user = UserModel::findByAccountOpenId($userInfo["openid"]);
  523. $inviteUserId = 0;
  524. if($arr[1]){
  525. //存在推荐人
  526. $inviteUserId = $arr[1];
  527. }
  528. if(!$user){
  529. //不存在则创建
  530. $userId = UserModel::query()->insertGetId(
  531. [
  532. "wechat_account_id"=>$wechatAccount->id,
  533. "web_site_id"=>$webSite->id,
  534. "user_open_id"=>$userInfo["openid"],
  535. "user_head_img_url"=>$userInfo["headimgurl"],
  536. "user_nickname"=>$userInfo["nickname"],
  537. "user_sex"=>$userInfo["sex"],
  538. "user_api_key"=>uniqid("fbt"),
  539. "user_api_key_expire_time"=>date("Y-m-d H:i:s",strtotime("+1 year")),
  540. "created_at"=>date("Y-m-d H:i:s"),
  541. "updated_at"=>date("Y-m-d H:i:s"),
  542. "invite_user_id"=>$inviteUserId
  543. ]
  544. );
  545. }else{
  546. //存在则更新用户头像
  547. UserModel::query()
  548. ->where("id",$user->id)
  549. ->update(
  550. [
  551. "user_head_img_url"=>$userInfo["headimgurl"],
  552. "user_nickname"=>$userInfo["nickname"],
  553. "wechat_account_id"=>$wechatAccount->id,
  554. "web_site_id"=>$webSite->id
  555. ]
  556. );
  557. $userId = $user->id;
  558. }
  559. $token = md5(uniqid("fbt"));
  560. BaseUtil::setRedisCache($token,$userId,24*3600);
  561. if(env("APP_ENV")=="local"){
  562. $url = "http://127.0.0.1:8080?wechatToken=".$token;
  563. }else{
  564. $url = "http://".$webSite->web_site_host."?wechatToken=".$token;
  565. }
  566. $instance->info("回调地址:".$url);
  567. return $url;
  568. }
  569. }