WechatLogic.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. // $elmUrl1 = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020002597");
  118. // $elmUrl2 = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020004284");
  119. // $elmUrl3 = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020005049");
  120. // $elmUrl4 = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020004425");
  121. // $elmLink1 = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
  122. //data-miniprogram-path='".$elmUrl1."'
  123. //href=".'"http://www.qq.com"'.">饿了么超大外卖红包</a>";
  124. // $elmLink2 = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
  125. //data-miniprogram-path='".$elmUrl2."'
  126. //href=".'"http://www.qq.com"'.">饿了么超大限时红包</a>";
  127. // $elmLink3 = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
  128. //data-miniprogram-path='".$elmUrl3."'
  129. //href=".'"http://www.qq.com"'.">饿了么最新红包</a>";
  130. // $elmLink4 = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
  131. //data-miniprogram-path='".$elmUrl4."'
  132. //href=".'"http://www.qq.com"'.">饿了么大额红包</a>";
  133. //判断EventKey是否存在
  134. $msg1 = new Image(env("WECHAT_IMAGE_MEDIA_ID"));
  135. $msg2 = <<<S
  136. $nickname
  137. 终于等到你啦!谢谢关注【返不停】
  138. 点击公众号菜单领取外卖大红包
  139. 回复关键字【帮助】,可查看各大电商平台返利使用步骤
  140. S;
  141. $msg2 = new Text($msg2);
  142. $res1 = $app->customer_service->message($msg1)->to($officialOpenId)->send();
  143. $res2 = $app->customer_service->message($msg2)->to($officialOpenId)->send();
  144. $instance->info('结果1:'.json_encode($res1));
  145. // $instance->info('结果2:'.json_encode($res2));
  146. break;
  147. case "unsubscribe"://取关公众号
  148. break;
  149. case "CLICK"://点击事件
  150. $date = date("m月d日");
  151. switch ($message["EventKey"]){
  152. case "elmwaimai":
  153. //饿了么外卖
  154. $url = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020002597");
  155. $link = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
  156. data-miniprogram-path='".$url."'
  157. href=".'"http://www.qq.com"'.">点我领取饿了么外卖红包</a>";
  158. break;
  159. case "elmxianshi":
  160. //饿了么限时红包
  161. $url = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020004284");
  162. $link = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
  163. data-miniprogram-path='".$url."'
  164. href=".'"http://www.qq.com"'.">点我领取饿了么限时红包</a>";
  165. break;
  166. case "elmzhaji":
  167. //饿了么炸鸡红包
  168. $url = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020005049");
  169. $link = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
  170. data-miniprogram-path='".$url."'
  171. href=".'"http://www.qq.com"'.">点我领取饿了么炸鸡红包</a>";
  172. break;
  173. case "elmxiawu":
  174. //饿了么下午茶红包
  175. $url = TaobaoLianMengUtil::getElmWaimaiSpreadUrl($userId,"20150318020004425");
  176. $link = "<a data-miniprogram-appid=".'"wxece3a9a4c82f58c9" '."
  177. data-miniprogram-path='".$url."'
  178. href=".'"http://www.qq.com"'.">点我领取饿了么下午茶红包</a>";
  179. break;
  180. case "mtwaimai":
  181. //美团外卖红包
  182. //获取美团的推广链接
  183. $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_MEITUAN,$userId);
  184. $url = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,2,MeiTuanLinkTypeEnum::H5);
  185. // $link = "<a data-miniprogram-appid=".'"wxde8ac0a21135c07d" '."
  186. //data-miniprogram-path='".$url."'
  187. //href=".'"http://www.qq.com"'.">点我领取美团外卖红包</a>";
  188. $link = "<a href='".$url."'>点我领取美团外卖红包</a>";
  189. break;
  190. case "mtjiudian":
  191. //美团酒店红包
  192. //获取美团的推广链接
  193. $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_MEITUAN,$userId);
  194. $url = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,7,MeiTuanLinkTypeEnum::MINIPROGRAME);
  195. $link = "<a data-miniprogram-appid=".'"wxde8ac0a21135c07d" '."
  196. data-miniprogram-path='".$url."'
  197. href=".'"http://www.qq.com"'.">点我领取美团酒店红包</a>";
  198. break;
  199. case "mtyouxuan":
  200. //美团优选红包
  201. //获取美团的推广链接
  202. $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_MEITUAN,$userId);
  203. $url = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,22,MeiTuanLinkTypeEnum::MINIPROGRAME);
  204. $link = "<a data-miniprogram-appid=".'"wxde8ac0a21135c07d" '."
  205. data-miniprogram-path='".$url."'href=".'"http://www.qq.com"'.">点我领取美团优选红包</a>";
  206. // $link = "<a href='".$url."'>点我领取美团优选红包</a>";
  207. break;
  208. case "mtshangou":
  209. //美团闪购红包
  210. //获取美团的推广链接
  211. $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_MEITUAN,$userId);
  212. $url = MeituanLianmengUtil::getMeituanWaimaiUrl($userId,$platformSid,4,MeiTuanLinkTypeEnum::H5);
  213. // $link = "<a data-miniprogram-appid=".'"wxde8ac0a21135c07d" '."
  214. //data-miniprogram-path='".$url."'
  215. //href=".'"http://www.qq.com"'.">点我领取美团闪购红包</a>";
  216. $link = "<a href='".$url."'>点我领取美团闪购红包</a>";
  217. break;
  218. case "help":
  219. //使用教程
  220. $msg = new Image(env("WECHAT_IMAGE_MEDIA_ID"));
  221. $res = $app->customer_service->message($msg)->to($officialOpenId)->send();
  222. return;
  223. break;
  224. }
  225. $msg = <<<S
  226. Hi,$nickname ,$date 红包已更新
  227. [红包] $link
  228. 祝用餐愉快
  229. S;
  230. $msg = new Text($msg);
  231. $res = $app->customer_service->message($msg)->to($officialOpenId)->send();
  232. $instance->info("返回结果:".json_encode($res));
  233. }
  234. }
  235. /**
  236. * 处理文字
  237. * @param $message
  238. * @param $app Application
  239. */
  240. public static function handleTxt($message,$app,$code)
  241. {
  242. $instance = new LoggerFactoryUtil(WechatLogic::class);
  243. $officialOpenId = $message["FromUserName"];
  244. $instance->info("用户信息:".$message["Content"]);
  245. $daogoUser = UserModel::query()->where("user_open_id",$officialOpenId)->first();
  246. $platformSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_PINGDUODUO,$daogoUser->id);
  247. $instance->info("pid:".$platformSid);
  248. //获取聚推客的推广位ID
  249. $jutuikeSid = BaseUtil::getPlatformUserSpreadId(PlatformTypeEnum::PLATFORM_JUTUIKE,$daogoUser->id);
  250. //判断是否是帮助
  251. if($message["Content"]=="帮助"){
  252. $msg1 = new Image(env("WECHAT_IMAGE_MEDIA_ID"));
  253. $app->customer_service->message($msg1)->to($officialOpenId)->send();
  254. return;
  255. }
  256. $wenti = new Text("稍等");
  257. $app->customer_service->message($wenti)->to($officialOpenId)->send();
  258. try{
  259. //1.判断是否匹配上拼多多商品链接
  260. $res = preg_match("/https\:\/\/mobile\.yangkeduo\.com/",$message["Content"],$match);
  261. if($res){
  262. //去除链接上的多余参数防止出现比价订单
  263. // $tmp = BaseUtil::getParamsByUrl($message["Content"]);
  264. // if(isset($tmp["goods_id"]) && isset($tmp["url"])){
  265. // $message["Content"] = $tmp["url"]."?goods_id=".$tmp["goods_id"];
  266. // }
  267. $instance->info("匹配到拼多多链接");
  268. // $goodsInfo = JutuikeUtil::transferGoodsLink($message["Content"]);
  269. // $tuiguangRes = JutuikeUtil::convert($goodsInfo["goodsId"],$jutuikeSid,"pdd");
  270. // $goodsTitle = $tuiguangRes["goodsName"];
  271. // $goodsPrice = $tuiguangRes["marketPrice"];
  272. // $couponPrice = $tuiguangRes["couponInfo"]["fav"];
  273. // $afterCouponPrice = $tuiguangRes["price"];
  274. // $rebeatPrice = round($tuiguangRes["commission"]*0.7,2);
  275. // $url = $tuiguangRes["url"];
  276. // $msg = <<<S
  277. //【 $goodsTitle 】
  278. //【原价】:$goodsPrice 元
  279. //【优惠券】:$couponPrice 元
  280. //【券后价】:$afterCouponPrice 元
  281. //【返现金额】:$rebeatPrice 元
  282. // -------------------------
  283. //点击链接购买:$url
  284. //具体返现金额,以实际支付金额为准
  285. //-------------------------
  286. //进入公众号=>我的=>钱包,可以1:1提现哦
  287. //S;
  288. // $wenti = new Text($msg);
  289. // $app->customer_service->message($wenti)->to($officialOpenId)->send();
  290. // return;
  291. // $instance->info("拼多多处理过的链接:".$message["Content"]);
  292. //获取商品详情
  293. $bean = new GoodsSearchParamBean(
  294. [
  295. "keyword"=>$message["Content"],
  296. "pid"=>"23985775_220421267"
  297. ]
  298. );
  299. $goodsData = DuoDuoKeUtil::goodsSearch($bean);
  300. $instance->info("拼多多返回商品详情数据:".json_encode($goodsData));
  301. //1.判断推广位是否备案
  302. $oauthRes = DuoDuoKeUtil::memberAuthorityQuery($platformSid);
  303. $instance->info("备案信息:".json_encode($oauthRes));
  304. //2.未备案这授权,已备案则直接获取推广链接
  305. if($oauthRes["authority_query_response"]["bind"]){
  306. //已备案
  307. $instance->info("拼多多转链用的url:".$message["Content"]);
  308. $data = DuoDuoKeUtil::goodsZsUnitUrlGen($platformSid,$message["Content"]);
  309. $instance->info("拼多多返回数据:".json_encode($data));
  310. $url = $data["goods_zs_unit_generate_response"]["short_url"];
  311. // $link = "<a href='".$url."'>点我购买,即刻拿优惠</a>";
  312. }else{
  313. //未备案,使用自己已备案的pid调用商品搜索接口获取goods_sign
  314. //获取到goods_sign
  315. $goods_sign = $goodsData["goods_search_response"]["goods_list"][0]["goods_sign"];
  316. $beanData = [
  317. "p_id"=>$platformSid,
  318. "goods_sign_list"=>json_encode([$goods_sign]),
  319. "generate_authority_url"=>"true",
  320. ];
  321. $generateBean = new GoodsPromotionUrlGenerateParamBean($beanData);
  322. $res = DuoDuoKeUtil::goodsPromotionUrlGenerate($generateBean);
  323. $instance->info("生成备案信息:".json_encode($res));
  324. $url = $res["goods_promotion_url_generate_response"]["goods_promotion_url_list"][0]["short_url"];
  325. // $link = "<a href='".$url."'>点击备案之后,直接购买</a>";
  326. }
  327. $goodsPrice = round($goodsData["goods_search_response"]["goods_list"][0]["min_group_price"]/100,2);
  328. $couponPrice = round($goodsData["goods_search_response"]["goods_list"][0]["coupon_discount"]/100,2);
  329. $afterCouponPrice = $goodsPrice-$couponPrice;
  330. $rebeatPrice = round($goodsData["goods_search_response"]["goods_list"][0]["promotion_rate"]*$afterCouponPrice*0.7/1000,2);
  331. $goodsTitle = $goodsData["goods_search_response"]["goods_list"][0]["goods_name"];
  332. $msg = <<<S
  333. 【 $goodsTitle 】
  334. 【原价】:$goodsPrice 元
  335. 【优惠券】:$couponPrice 元
  336. 【券后价】:$afterCouponPrice 元
  337. 【返现金额】:$rebeatPrice 元
  338. -------------------------
  339. 点击链接购买:$url
  340. 具体返现金额,以实际支付金额为准
  341. 拼多多存在比价订单(无佣金):自己登录后的查看详情页复制链接,进行转链
  342. 解决方法:退出app,浏览需要购买的商品页,然后复制链接转链,即可避免
  343. -------------------------
  344. 进入公众号=>我的=>钱包,可以1:1提现哦
  345. S;
  346. $wenti = new Text($msg);
  347. $app->customer_service->message($wenti)->to($officialOpenId)->send();
  348. return;
  349. }
  350. //3.判断是否匹配到维品会链接
  351. $res = preg_match("/https\:\/\/m\.vip\.com/",$message["Content"],$match);
  352. if($res){
  353. $instance->info("匹配到维品会链接");
  354. $goodsInfo = JutuikeUtil::transferGoodsLink($message["Content"]);
  355. $tuiguangRes = JutuikeUtil::convert($goodsInfo["goodsId"],$jutuikeSid,$goodsInfo["source"]);
  356. $goodsTitle = $tuiguangRes["goodsName"];
  357. $goodsPrice = $tuiguangRes["marketPrice"];
  358. $couponPrice = $tuiguangRes["couponInfo"]["fav"];
  359. $afterCouponPrice = $tuiguangRes["price"];
  360. $rebeatPrice = round($tuiguangRes["commission"]*0.7,2);
  361. $url = $tuiguangRes["url"];
  362. $msg = <<<S
  363. 【 $goodsTitle 】
  364. 【原价】:$goodsPrice 元
  365. 【优惠券】:$couponPrice 元
  366. 【券后价】:$afterCouponPrice 元
  367. 【返现金额】:$rebeatPrice 元
  368. -------------------------
  369. 点击链接购买:$url
  370. 具体返现金额,以实际支付金额为准
  371. -------------------------
  372. 进入公众号=>我的=>钱包,可以1:1提现哦
  373. S;
  374. $wenti = new Text($msg);
  375. $app->customer_service->message($wenti)->to($officialOpenId)->send();
  376. return;
  377. }
  378. //3.判断是否匹配到京东链接
  379. $res = preg_match("/https\:\/\/item\.m\.jd\.com/",$message["Content"],$match);
  380. if($res){
  381. $instance->info("匹配到京东链接");
  382. $goodsInfo = JutuikeUtil::transferGoodsLink($message["Content"]);
  383. $tuiguangRes = JutuikeUtil::convert($goodsInfo["goodsId"],$jutuikeSid,$goodsInfo["source"]);
  384. $goodsTitle = $tuiguangRes["goodsName"];
  385. $goodsPrice = $tuiguangRes["marketPrice"];
  386. $couponPrice = $tuiguangRes["couponInfo"]["fav"];
  387. $afterCouponPrice = $tuiguangRes["price"];
  388. $rebeatPrice = round($tuiguangRes["commission"]*0.7,2);
  389. $url = $tuiguangRes["url"];
  390. $msg = <<<S
  391. 【 $goodsTitle 】
  392. 【原价】:$goodsPrice 元
  393. 【优惠券】:$couponPrice 元
  394. 【券后价】:$afterCouponPrice 元
  395. 【返现金额】:$rebeatPrice 元
  396. -------------------------
  397. 点击链接购买:$url
  398. 具体返现金额,以实际支付金额为准
  399. -------------------------
  400. 进入公众号=>我的=>钱包,可以1:1提现哦
  401. S;
  402. $wenti = new Text($msg);
  403. $app->customer_service->message($wenti)->to($officialOpenId)->send();
  404. return;
  405. }
  406. //2.判断是否匹配上淘宝链接
  407. // $res = preg_match("/https\:\/\/m\.tb\.cn/",$message["Content"],$match);
  408. //
  409. // if($res){
  410. // $instance->info("匹配到淘宝链接");
  411. // $goodsInfo = JutuikeUtil::transferGoodsLink($message["Content"]);
  412. // $tuiguangRes = JutuikeUtil::convert($goodsInfo["goodsId"],$jutuikeSid);
  413. // $goodsTitle = $tuiguangRes["goodsName"];
  414. // $goodsPrice = $tuiguangRes["marketPrice"];
  415. // $couponPrice = $tuiguangRes["couponInfo"]["fav"];
  416. // $afterCouponPrice = $tuiguangRes["price"];
  417. // $rebeatPrice = round($tuiguangRes["commission"]*0.7,2);
  418. // $url = $tuiguangRes["url"];
  419. // $msg = <<<S
  420. //【 $goodsTitle 】
  421. //【原价】:$goodsPrice 元
  422. //【优惠券】:$couponPrice 元
  423. //【券后价】:$afterCouponPrice 元
  424. //【返现金额】:$rebeatPrice 元
  425. // -------------------------
  426. //复制这条信息:$url
  427. //打开【手机淘宝】即可查看
  428. //具体返现金额,以实际支付金额为准
  429. //-------------------------
  430. //进入公众号=>我的=>钱包,可以1:1提现哦
  431. //S;
  432. // $wenti = new Text($msg);
  433. // $app->customer_service->message($wenti)->to($officialOpenId)->send();
  434. // return;
  435. // }
  436. //默认是认为淘宝链接
  437. $instance->info("匹配到淘宝链接");
  438. $goodsInfo = TaobaoLianMengUtil::taokoulingConvert($message["Content"]);
  439. $tuiguangRes = JutuikeUtil::convert($goodsInfo["data"]["num_iid"],$jutuikeSid);
  440. $goodsTitle = $tuiguangRes["goodsName"];
  441. $goodsPrice = $tuiguangRes["marketPrice"];
  442. $couponPrice = $tuiguangRes["couponInfo"]["fav"];
  443. $afterCouponPrice = $tuiguangRes["price"];
  444. $rebeatPrice = round($tuiguangRes["commission"]*0.7,2);
  445. $url = $tuiguangRes["url"];
  446. $msg = <<<S
  447. 【 $goodsTitle 】
  448. 【原价】:$goodsPrice 元
  449. 【优惠券】:$couponPrice 元
  450. 【券后价】:$afterCouponPrice 元
  451. 【返现金额】:$rebeatPrice 元
  452. -------------------------
  453. 复制这条信息:$url
  454. 打开【手机淘宝】即可查看
  455. 具体返现金额,以实际支付金额为准
  456. -------------------------
  457. 进入公众号=>我的=>钱包,可以1:1提现哦
  458. S;
  459. $wenti = new Text($msg);
  460. $app->customer_service->message($wenti)->to($officialOpenId)->send();
  461. return;
  462. }catch (\Throwable $exception){
  463. $instance->info("异常信息:".$exception->getMessage());
  464. $wenti = new Text("请换个商品,该商品没有返利");
  465. $app->customer_service->message($wenti)->to($officialOpenId)->send();
  466. }
  467. }
  468. /**
  469. * 设置公众号菜单逻辑
  470. */
  471. public static function setMenuLogic()
  472. {
  473. $params = request()->all();
  474. //1.获取所有上线的一级菜单
  475. $menus = WechatAccountMenuConfigModel::getMenuByParentId(0);
  476. $configs = [];
  477. foreach ($menus as $menu){
  478. switch ($menu->menu_type){
  479. case WechatAccountMenuTypeEnum::CLICK:
  480. //点击菜单
  481. $configs[] = [
  482. "type" => "click",
  483. "name" => $menu->menu_name,
  484. "key" => $menu->menu_key
  485. ];
  486. break;
  487. case WechatAccountMenuTypeEnum::LINK:
  488. //商品链接
  489. $configs[] = [
  490. "type" => "view",
  491. "name" => $menu->menu_name,
  492. "url" => $menu->menu_url
  493. ];
  494. break;
  495. case WechatAccountMenuTypeEnum::MENU:
  496. //菜单
  497. //获取下级菜单
  498. $tmps = WechatAccountMenuConfigModel::getMenuByParentId($menu->id);
  499. $subMenus = [];
  500. foreach ($tmps as $tmp){
  501. switch ($tmp->menu_type) {
  502. case WechatAccountMenuTypeEnum::CLICK:
  503. //点击菜单
  504. $subMenus[] = [
  505. "type" => "click",
  506. "name" => $tmp->menu_name,
  507. "key" => $tmp->menu_key
  508. ];
  509. break;
  510. case WechatAccountMenuTypeEnum::LINK:
  511. //链接
  512. $subMenus[] = [
  513. "type" => "view",
  514. "name" => $tmp->menu_name,
  515. "url" => $tmp->menu_url
  516. ];
  517. break;
  518. }
  519. }
  520. $configs[] = [
  521. "name"=>$menu->menu_name,
  522. "sub_button"=>$subMenus
  523. ];
  524. break;
  525. }
  526. }
  527. $app = WechatAccountUtil::getApp($params["wechat_app_code"]);
  528. $instance = new LoggerFactoryUtil(WechatLogic::class);
  529. $instance->info("菜单:".json_encode($configs));
  530. return $app->menu->create($configs);
  531. }
  532. /**
  533. * 获取公众号配置
  534. */
  535. public static function accountConfigLogic()
  536. {
  537. $params = request()->all();
  538. $account = WechatAccountModel::findByWechatAppCode($params["wechat_app_code"]);
  539. $data = BaseUtil::getJsapiSign($account->wechat_app_id,$account->wechat_app_secret,$params["url"]);
  540. return $data;
  541. }
  542. /**
  543. * 微信公众号授权回调
  544. */
  545. public static function accountCallbackLogic()
  546. {
  547. $instance = new LoggerFactoryUtil(WechatLogic::class);
  548. $params = request()->all();
  549. $instance->info("微信回调数据:".json_encode($params));
  550. //切割公众号和邀请人
  551. $arr = explode("|",$params["state"]);
  552. $wechatAccount = WechatAccountModel::findByWechatAppId($arr[0]);
  553. //获取站点信息
  554. $webSite = WebSiteModel::findByWechatAccountId($wechatAccount->id);
  555. $app = WechatAccountUtil::getAppByID($wechatAccount->id);
  556. try{
  557. $userInfo = $app->oauth->user()->getOriginal();
  558. }catch (\Throwable $exception){
  559. if(env("APP_ENV")=="local"){
  560. $url = "http://127.0.0.1:8080";
  561. }else{
  562. $url = "http://".$webSite->web_site_host;
  563. }
  564. return $url;
  565. }
  566. $instance->info("用户信息:".json_encode($userInfo));
  567. //判断用户是否存在
  568. $user = UserModel::findByAccountOpenId($userInfo["openid"]);
  569. $inviteUserId = 0;
  570. if($arr[1]){
  571. //存在推荐人
  572. $inviteUserId = $arr[1];
  573. }
  574. if(!$user){
  575. //不存在则创建
  576. $userId = UserModel::query()->insertGetId(
  577. [
  578. "wechat_account_id"=>$wechatAccount->id,
  579. "web_site_id"=>$webSite->id,
  580. "user_open_id"=>$userInfo["openid"],
  581. "user_head_img_url"=>$userInfo["headimgurl"],
  582. "user_nickname"=>$userInfo["nickname"],
  583. "user_sex"=>$userInfo["sex"],
  584. "user_api_key"=>uniqid("fbt"),
  585. "user_api_key_expire_time"=>date("Y-m-d H:i:s",strtotime("+1 year")),
  586. "created_at"=>date("Y-m-d H:i:s"),
  587. "updated_at"=>date("Y-m-d H:i:s"),
  588. "invite_user_id"=>$inviteUserId
  589. ]
  590. );
  591. }else{
  592. //存在则更新用户头像
  593. UserModel::query()
  594. ->where("id",$user->id)
  595. ->update(
  596. [
  597. "user_head_img_url"=>$userInfo["headimgurl"],
  598. "user_nickname"=>$userInfo["nickname"],
  599. "wechat_account_id"=>$wechatAccount->id,
  600. "web_site_id"=>$webSite->id
  601. ]
  602. );
  603. $userId = $user->id;
  604. }
  605. $token = md5(uniqid("fbt"));
  606. BaseUtil::setRedisCache($token,$userId,24*3600);
  607. if(env("APP_ENV")=="local"){
  608. $url = "http://127.0.0.1:8080?wechatToken=".$token;
  609. }else{
  610. $url = "http://".$webSite->web_site_host."?wechatToken=".$token;
  611. }
  612. $instance->info("回调地址:".$url);
  613. return $url;
  614. }
  615. }