WechatLogic.php 30 KB

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