WechatLogic.php 30 KB

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