WechatLogic.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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. //判断是否是提现
  257. if($message["Content"]=="提现"){
  258. $wenti = new Text("点击公众号菜单=>个人中心=>我的=>可提现余额==>提现");
  259. $app->customer_service->message($wenti)->to($officialOpenId)->send();
  260. return;
  261. }
  262. $wenti = new Text("稍等");
  263. $app->customer_service->message($wenti)->to($officialOpenId)->send();
  264. try{
  265. //1.判断是否匹配上拼多多商品链接
  266. $res = preg_match("/https\:\/\/mobile\.yangkeduo\.com/",$message["Content"],$match);
  267. if($res){
  268. //去除链接上的多余参数防止出现比价订单
  269. // $tmp = BaseUtil::getParamsByUrl($message["Content"]);
  270. // if(isset($tmp["goods_id"]) && isset($tmp["url"])){
  271. // $message["Content"] = $tmp["url"]."?goods_id=".$tmp["goods_id"];
  272. // }
  273. $instance->info("匹配到拼多多链接");
  274. // $goodsInfo = JutuikeUtil::transferGoodsLink($message["Content"]);
  275. // $tuiguangRes = JutuikeUtil::convert($goodsInfo["goodsId"],$jutuikeSid,"pdd");
  276. // $goodsTitle = $tuiguangRes["goodsName"];
  277. // $goodsPrice = $tuiguangRes["marketPrice"];
  278. // $couponPrice = $tuiguangRes["couponInfo"]["fav"];
  279. // $afterCouponPrice = $tuiguangRes["price"];
  280. // $rebeatPrice = round($tuiguangRes["commission"]*0.7,2);
  281. // $url = $tuiguangRes["url"];
  282. // $msg = <<<S
  283. //【 $goodsTitle 】
  284. //【原价】:$goodsPrice 元
  285. //【优惠券】:$couponPrice 元
  286. //【券后价】:$afterCouponPrice 元
  287. //【返现金额】:$rebeatPrice 元
  288. // -------------------------
  289. //点击链接购买:$url
  290. //具体返现金额,以实际支付金额为准
  291. //-------------------------
  292. //进入公众号=>我的=>钱包,可以1:1提现哦
  293. //S;
  294. // $wenti = new Text($msg);
  295. // $app->customer_service->message($wenti)->to($officialOpenId)->send();
  296. // return;
  297. // $instance->info("拼多多处理过的链接:".$message["Content"]);
  298. //获取商品详情
  299. $bean = new GoodsSearchParamBean(
  300. [
  301. "keyword"=>$message["Content"],
  302. "pid"=>"23985775_220421267"
  303. ]
  304. );
  305. $goodsData = DuoDuoKeUtil::goodsSearch($bean);
  306. $instance->info("拼多多返回商品详情数据:".json_encode($goodsData));
  307. //1.判断推广位是否备案
  308. $oauthRes = DuoDuoKeUtil::memberAuthorityQuery($platformSid);
  309. $instance->info("备案信息:".json_encode($oauthRes));
  310. //2.未备案这授权,已备案则直接获取推广链接
  311. if($oauthRes["authority_query_response"]["bind"]){
  312. //已备案
  313. $instance->info("拼多多转链用的url:".$message["Content"]);
  314. $data = DuoDuoKeUtil::goodsZsUnitUrlGen($platformSid,$message["Content"]);
  315. $instance->info("拼多多返回数据:".json_encode($data));
  316. $url = $data["goods_zs_unit_generate_response"]["short_url"];
  317. // $link = "<a href='".$url."'>点我购买,即刻拿优惠</a>";
  318. }else{
  319. //未备案,使用自己已备案的pid调用商品搜索接口获取goods_sign
  320. //获取到goods_sign
  321. $goods_sign = $goodsData["goods_search_response"]["goods_list"][0]["goods_sign"];
  322. $beanData = [
  323. "p_id"=>$platformSid,
  324. "goods_sign_list"=>json_encode([$goods_sign]),
  325. "generate_authority_url"=>"true",
  326. ];
  327. $generateBean = new GoodsPromotionUrlGenerateParamBean($beanData);
  328. $res = DuoDuoKeUtil::goodsPromotionUrlGenerate($generateBean);
  329. $instance->info("生成备案信息:".json_encode($res));
  330. $url = $res["goods_promotion_url_generate_response"]["goods_promotion_url_list"][0]["short_url"];
  331. // $link = "<a href='".$url."'>点击备案之后,直接购买</a>";
  332. }
  333. $goodsPrice = round($goodsData["goods_search_response"]["goods_list"][0]["min_group_price"]/100,2);
  334. $couponPrice = round($goodsData["goods_search_response"]["goods_list"][0]["coupon_discount"]/100,2);
  335. $afterCouponPrice = $goodsPrice-$couponPrice;
  336. $rebeatPrice = round($goodsData["goods_search_response"]["goods_list"][0]["promotion_rate"]*$afterCouponPrice*0.7/1000,2);
  337. $goodsTitle = $goodsData["goods_search_response"]["goods_list"][0]["goods_name"];
  338. $msg = <<<S
  339. 【 $goodsTitle 】
  340. 【原价】:$goodsPrice 元
  341. 【优惠券】:$couponPrice 元
  342. 【券后价】:$afterCouponPrice 元
  343. 【返现金额】:$rebeatPrice 元
  344. -------------------------
  345. 点击链接购买:$url
  346. 具体返现金额,以实际支付金额为准
  347. 拼多多存在比价订单(无佣金):自己登录后的查看详情页复制链接,进行转链
  348. 解决方法:退出app,浏览需要购买的商品页,然后复制链接转链,即可避免
  349. -------------------------
  350. 进入公众号=>我的=>钱包,可以1:1提现哦
  351. S;
  352. $wenti = new Text($msg);
  353. $app->customer_service->message($wenti)->to($officialOpenId)->send();
  354. return;
  355. }
  356. //3.判断是否匹配到维品会链接
  357. $res = preg_match("/https\:\/\/m\.vip\.com/",$message["Content"],$match);
  358. if($res){
  359. $instance->info("匹配到维品会链接");
  360. $goodsInfo = JutuikeUtil::transferGoodsLink($message["Content"]);
  361. $tuiguangRes = JutuikeUtil::convert($goodsInfo["goodsId"],$jutuikeSid,$goodsInfo["source"]);
  362. $goodsTitle = $tuiguangRes["goodsName"];
  363. $goodsPrice = $tuiguangRes["marketPrice"];
  364. $couponPrice = $tuiguangRes["couponInfo"]["fav"];
  365. $afterCouponPrice = $tuiguangRes["price"];
  366. $rebeatPrice = round($tuiguangRes["commission"]*0.7,2);
  367. $url = $tuiguangRes["url"];
  368. $msg = <<<S
  369. 【 $goodsTitle 】
  370. 【原价】:$goodsPrice 元
  371. 【优惠券】:$couponPrice 元
  372. 【券后价】:$afterCouponPrice 元
  373. 【返现金额】:$rebeatPrice 元
  374. -------------------------
  375. 点击链接购买:$url
  376. 具体返现金额,以实际支付金额为准
  377. -------------------------
  378. 进入公众号=>我的=>钱包,可以1:1提现哦
  379. S;
  380. $wenti = new Text($msg);
  381. $app->customer_service->message($wenti)->to($officialOpenId)->send();
  382. return;
  383. }
  384. //3.判断是否匹配到京东链接
  385. $res = preg_match("/https\:\/\/item\.m\.jd\.com/",$message["Content"],$match);
  386. if($res){
  387. $instance->info("匹配到京东链接");
  388. $goodsInfo = JutuikeUtil::transferGoodsLink($message["Content"]);
  389. $tuiguangRes = JutuikeUtil::convert($goodsInfo["goodsId"],$jutuikeSid,$goodsInfo["source"]);
  390. $goodsTitle = $tuiguangRes["goodsName"];
  391. $goodsPrice = $tuiguangRes["marketPrice"];
  392. $couponPrice = $tuiguangRes["couponInfo"]["fav"];
  393. $afterCouponPrice = $tuiguangRes["price"];
  394. $rebeatPrice = round($tuiguangRes["commission"]*0.7,2);
  395. $url = $tuiguangRes["url"];
  396. $msg = <<<S
  397. 【 $goodsTitle 】
  398. 【原价】:$goodsPrice 元
  399. 【优惠券】:$couponPrice 元
  400. 【券后价】:$afterCouponPrice 元
  401. 【返现金额】:$rebeatPrice 元
  402. -------------------------
  403. 点击链接购买:$url
  404. 具体返现金额,以实际支付金额为准
  405. -------------------------
  406. 进入公众号=>我的=>钱包,可以1:1提现哦
  407. S;
  408. $wenti = new Text($msg);
  409. $app->customer_service->message($wenti)->to($officialOpenId)->send();
  410. return;
  411. }
  412. //2.判断是否匹配上淘宝链接
  413. // $res = preg_match("/https\:\/\/m\.tb\.cn/",$message["Content"],$match);
  414. //
  415. // if($res){
  416. // $instance->info("匹配到淘宝链接");
  417. // $goodsInfo = JutuikeUtil::transferGoodsLink($message["Content"]);
  418. // $tuiguangRes = JutuikeUtil::convert($goodsInfo["goodsId"],$jutuikeSid);
  419. // $goodsTitle = $tuiguangRes["goodsName"];
  420. // $goodsPrice = $tuiguangRes["marketPrice"];
  421. // $couponPrice = $tuiguangRes["couponInfo"]["fav"];
  422. // $afterCouponPrice = $tuiguangRes["price"];
  423. // $rebeatPrice = round($tuiguangRes["commission"]*0.7,2);
  424. // $url = $tuiguangRes["url"];
  425. // $msg = <<<S
  426. //【 $goodsTitle 】
  427. //【原价】:$goodsPrice 元
  428. //【优惠券】:$couponPrice 元
  429. //【券后价】:$afterCouponPrice 元
  430. //【返现金额】:$rebeatPrice 元
  431. // -------------------------
  432. //复制这条信息:$url
  433. //打开【手机淘宝】即可查看
  434. //具体返现金额,以实际支付金额为准
  435. //-------------------------
  436. //进入公众号=>我的=>钱包,可以1:1提现哦
  437. //S;
  438. // $wenti = new Text($msg);
  439. // $app->customer_service->message($wenti)->to($officialOpenId)->send();
  440. // return;
  441. // }
  442. //默认是认为淘宝链接
  443. $instance->info("匹配到淘宝链接");
  444. $goodsInfo = TaobaoLianMengUtil::taokoulingConvert($message["Content"]);
  445. $instance->info("淘宝返回数据:".json_encode($goodsInfo));
  446. $tuiguangRes = JutuikeUtil::convert($goodsInfo["num_iid"],$jutuikeSid);
  447. $goodsTitle = $tuiguangRes["goodsName"];
  448. $goodsPrice = $tuiguangRes["marketPrice"];
  449. $couponPrice = $tuiguangRes["couponInfo"]["fav"];
  450. $afterCouponPrice = $tuiguangRes["price"];
  451. $rebeatPrice = round($tuiguangRes["commission"]*0.7,2);
  452. $url = $tuiguangRes["url"];
  453. $msg = <<<S
  454. 【 $goodsTitle 】
  455. 【原价】:$goodsPrice 元
  456. 【优惠券】:$couponPrice 元
  457. 【券后价】:$afterCouponPrice 元
  458. 【返现金额】:$rebeatPrice 元
  459. -------------------------
  460. 复制这条信息:$url
  461. 打开【手机淘宝】即可查看
  462. 具体返现金额,以实际支付金额为准
  463. -------------------------
  464. 进入公众号=>我的=>钱包,可以1:1提现哦
  465. S;
  466. $wenti = new Text($msg);
  467. $app->customer_service->message($wenti)->to($officialOpenId)->send();
  468. return;
  469. }catch (\Throwable $exception){
  470. $instance->info("异常信息:".$exception->getMessage());
  471. $wenti = new Text("请换个商品,该商品没有返利");
  472. $app->customer_service->message($wenti)->to($officialOpenId)->send();
  473. }
  474. }
  475. /**
  476. * 设置公众号菜单逻辑
  477. */
  478. public static function setMenuLogic()
  479. {
  480. $params = request()->all();
  481. //1.获取所有上线的一级菜单
  482. $menus = WechatAccountMenuConfigModel::getMenuByParentId(0);
  483. $configs = [];
  484. foreach ($menus as $menu){
  485. switch ($menu->menu_type){
  486. case WechatAccountMenuTypeEnum::CLICK:
  487. //点击菜单
  488. $configs[] = [
  489. "type" => "click",
  490. "name" => $menu->menu_name,
  491. "key" => $menu->menu_key
  492. ];
  493. break;
  494. case WechatAccountMenuTypeEnum::LINK:
  495. //商品链接
  496. $configs[] = [
  497. "type" => "view",
  498. "name" => $menu->menu_name,
  499. "url" => $menu->menu_url
  500. ];
  501. break;
  502. case WechatAccountMenuTypeEnum::MENU:
  503. //菜单
  504. //获取下级菜单
  505. $tmps = WechatAccountMenuConfigModel::getMenuByParentId($menu->id);
  506. $subMenus = [];
  507. foreach ($tmps as $tmp){
  508. switch ($tmp->menu_type) {
  509. case WechatAccountMenuTypeEnum::CLICK:
  510. //点击菜单
  511. $subMenus[] = [
  512. "type" => "click",
  513. "name" => $tmp->menu_name,
  514. "key" => $tmp->menu_key
  515. ];
  516. break;
  517. case WechatAccountMenuTypeEnum::LINK:
  518. //链接
  519. $subMenus[] = [
  520. "type" => "view",
  521. "name" => $tmp->menu_name,
  522. "url" => $tmp->menu_url
  523. ];
  524. break;
  525. }
  526. }
  527. $configs[] = [
  528. "name"=>$menu->menu_name,
  529. "sub_button"=>$subMenus
  530. ];
  531. break;
  532. }
  533. }
  534. $app = WechatAccountUtil::getApp($params["wechat_app_code"]);
  535. $instance = new LoggerFactoryUtil(WechatLogic::class);
  536. $instance->info("菜单:".json_encode($configs));
  537. return $app->menu->create($configs);
  538. }
  539. /**
  540. * 获取公众号配置
  541. */
  542. public static function accountConfigLogic()
  543. {
  544. $params = request()->all();
  545. $account = WechatAccountModel::findByWechatAppCode($params["wechat_app_code"]);
  546. $data = BaseUtil::getJsapiSign($account->wechat_app_id,$account->wechat_app_secret,$params["url"]);
  547. return $data;
  548. }
  549. /**
  550. * 微信公众号授权回调
  551. */
  552. public static function accountCallbackLogic()
  553. {
  554. $instance = new LoggerFactoryUtil(WechatLogic::class);
  555. $params = request()->all();
  556. $instance->info("微信回调数据:".json_encode($params));
  557. //切割公众号和邀请人
  558. $arr = explode("|",$params["state"]);
  559. $wechatAccount = WechatAccountModel::findByWechatAppId($arr[0]);
  560. //获取站点信息
  561. $webSite = WebSiteModel::findByWechatAccountId($wechatAccount->id);
  562. $app = WechatAccountUtil::getAppByID($wechatAccount->id);
  563. try{
  564. $userInfo = $app->oauth->user()->getOriginal();
  565. }catch (\Throwable $exception){
  566. if(env("APP_ENV")=="local"){
  567. $url = "http://127.0.0.1:8080";
  568. }else{
  569. $url = "http://".$webSite->web_site_host;
  570. }
  571. return $url;
  572. }
  573. $instance->info("用户信息:".json_encode($userInfo));
  574. //判断用户是否存在
  575. $user = UserModel::findByAccountOpenId($userInfo["openid"]);
  576. $inviteUserId = 0;
  577. if($arr[1]){
  578. //存在推荐人
  579. $inviteUserId = $arr[1];
  580. }
  581. if(!$user){
  582. //不存在则创建
  583. $userId = UserModel::query()->insertGetId(
  584. [
  585. "wechat_account_id"=>$wechatAccount->id,
  586. "web_site_id"=>$webSite->id,
  587. "user_open_id"=>$userInfo["openid"],
  588. "user_head_img_url"=>$userInfo["headimgurl"],
  589. "user_nickname"=>$userInfo["nickname"],
  590. "user_sex"=>$userInfo["sex"],
  591. "user_api_key"=>uniqid("fbt"),
  592. "user_api_key_expire_time"=>date("Y-m-d H:i:s",strtotime("+1 year")),
  593. "created_at"=>date("Y-m-d H:i:s"),
  594. "updated_at"=>date("Y-m-d H:i:s"),
  595. "invite_user_id"=>$inviteUserId
  596. ]
  597. );
  598. }else{
  599. //存在则更新用户头像
  600. UserModel::query()
  601. ->where("id",$user->id)
  602. ->update(
  603. [
  604. "user_head_img_url"=>$userInfo["headimgurl"],
  605. "user_nickname"=>$userInfo["nickname"],
  606. "wechat_account_id"=>$wechatAccount->id,
  607. "web_site_id"=>$webSite->id
  608. ]
  609. );
  610. $userId = $user->id;
  611. }
  612. $token = md5(uniqid("fbt"));
  613. BaseUtil::setRedisCache($token,$userId,24*3600);
  614. if(env("APP_ENV")=="local"){
  615. $url = "http://127.0.0.1:8080?wechatToken=".$token;
  616. }else{
  617. $url = "http://".$webSite->web_site_host."?wechatToken=".$token;
  618. }
  619. $instance->info("回调地址:".$url);
  620. return $url;
  621. }
  622. }