MessageServices.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\message\wechat;
  12. use app\services\activity\bargain\StoreBargainServices;
  13. use app\services\activity\combination\StoreCombinationServices;
  14. use app\services\activity\combination\StorePinkServices;
  15. use app\services\activity\seckill\StoreSeckillServices;
  16. use app\services\BaseServices;
  17. use app\services\other\QrcodeServices;
  18. use app\services\product\product\StoreProductServices;
  19. use app\services\user\LoginServices;
  20. use app\services\user\UserCardServices;
  21. use app\services\user\UserServices;
  22. use app\services\wechat\WechatQrcodeServices;
  23. use app\services\wechat\WechatReplyServices;
  24. use app\services\wechat\WechatUserServices;
  25. use crmeb\services\CacheService;
  26. use crmeb\services\SystemConfigService;
  27. use crmeb\services\wechat\Messages;
  28. use crmeb\services\wechat\OfficialAccount;
  29. use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
  30. use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
  31. use EasyWeChat\Kernel\Exceptions\RuntimeException;
  32. use EasyWeChat\Kernel\Messages\Image;
  33. use EasyWeChat\Kernel\Messages\News;
  34. use EasyWeChat\Kernel\Messages\Text;
  35. use EasyWeChat\Kernel\Messages\Transfer;
  36. use EasyWeChat\Kernel\Messages\Voice;
  37. use think\facade\Log;
  38. /**
  39. * Class MessageServices
  40. * @package app\services\message\wechat
  41. */
  42. class MessageServices extends BaseServices
  43. {
  44. /**
  45. * 事件处理
  46. * @param $qrInfo
  47. * @param $openid
  48. * @return array|Image|News|Text|Transfer|Voice|string
  49. * @throws InvalidArgumentException
  50. * @throws InvalidConfigException
  51. * @throws RuntimeException
  52. */
  53. public function wechatEvent($qrInfo, $openid)
  54. {
  55. $response = Messages::transfer();
  56. $thirdType = explode('-', $qrInfo['third_type']);
  57. $baseUrl = sys_config('site_url');
  58. switch (strtolower($thirdType[0])) {
  59. case 'spread':
  60. try {
  61. $spreadUid = $qrInfo['third_id'];
  62. /** @var WechatUserServices $wechatUser */
  63. $wechatUser = app()->make(WechatUserServices::class);
  64. $uid = $wechatUser->getFieldValue($openid, 'openid', 'uid');
  65. /** @var UserServices $userService */
  66. $userService = app()->make(UserServices::class);
  67. $userInfo = $userService->get($uid);
  68. if ($userInfo && $spreadUid != $uid && !$userInfo['spread_uid']) {
  69. /** @var LoginServices $loginService */
  70. $loginService = app()->make(LoginServices::class);
  71. if ($loginService->updateUserInfo(['spread_uid' => $spreadUid], $userInfo)) {
  72. $response = '绑定推荐人失败!';
  73. }
  74. }
  75. $data = SystemConfigService::more(['site_name', 'site_logo', 'site_name']);
  76. $wechatNews['title'] = $data['site_name'] ?? '';
  77. $wechatNews['image'] = $data['site_logo'] ?? '';
  78. $wechatNews['description'] = $data['site_name'] ?? '';
  79. $wechatNews['url'] = $baseUrl . '/pages/index/index';
  80. $messages = Messages::newMessage($wechatNews);
  81. OfficialAccount::staffService()->message($messages)->to($openid)->send();
  82. } catch (\Exception $e) {
  83. $response = $e->getMessage();
  84. }
  85. break;
  86. case 'reply':
  87. /** @var WechatReplyServices $replyServices */
  88. $replyServices = app()->make(WechatReplyServices::class);
  89. $data = $replyServices->get($qrInfo['third_id']);
  90. if ($data) {
  91. $response = $replyServices->replyDataByMessage($data->toArray());
  92. }
  93. break;
  94. case 'product':
  95. /** @var StoreProductServices $productService */
  96. $productService = app()->make(StoreProductServices::class);
  97. $productInfo = $productService->get($thirdType[1] ?? 0);
  98. $wechatNews['title'] = $productInfo->store_name;
  99. $wechatNews['image'] = $productInfo->image;
  100. $wechatNews['description'] = $productInfo->store_info;
  101. $wechatNews['url'] = $baseUrl . '/pages/goods_details/index?id=' . $thirdType[1];
  102. $messages = Messages::newMessage($wechatNews);
  103. OfficialAccount::staffService()->message($messages)->to($openid)->send();
  104. break;
  105. case 'combination':
  106. /** @var StoreCombinationServices $combinationService */
  107. $combinationService = app()->make(StoreCombinationServices::class);
  108. $productInfo = $combinationService->get($thirdType[1] ?? 0);
  109. $wechatNews['title'] = $productInfo->title;
  110. $wechatNews['image'] = $productInfo->image;
  111. $wechatNews['description'] = $productInfo->info;
  112. $wechatNews['url'] = $baseUrl . '/pages/activity/goods_combination_details/index?id=' . $thirdType[1];
  113. $messages = Messages::newMessage($wechatNews);
  114. OfficialAccount::staffService()->message($messages)->to($openid)->send();
  115. break;
  116. case 'seckill':
  117. /** @var StoreSeckillServices $seckillService */
  118. $seckillService = app()->make(StoreSeckillServices::class);
  119. $productInfo = $seckillService->get($thirdType[1] ?? 0);
  120. $wechatNews['title'] = $productInfo->title;
  121. $wechatNews['image'] = $productInfo->image;
  122. $wechatNews['description'] = $productInfo->info;
  123. $wechatNews['url'] = $baseUrl . '/pages/activity/goods_seckill_details/index?id=' . $thirdType[1] . '&time=' . $thirdType[2] . '&status=' . $thirdType[3];
  124. $messages = Messages::newMessage($wechatNews);
  125. OfficialAccount::staffService()->message($messages)->to($openid)->send();
  126. break;
  127. case 'bargain':
  128. /** @var StoreBargainServices $bargainService */
  129. $bargainService = app()->make(StoreBargainServices::class);
  130. $productInfo = $bargainService->get($thirdType[1] ?? 0);
  131. $wechatNews['title'] = $productInfo->title;
  132. $wechatNews['image'] = $productInfo->image;
  133. $wechatNews['description'] = $productInfo->info;
  134. $wechatNews['url'] = $baseUrl . '/pages/activity/goods_bargain_details/index?id=' . $thirdType[1] . '&bargain=' . $thirdType[2];
  135. $messages = Messages::newMessage($wechatNews);
  136. OfficialAccount::staffService()->message($messages)->to($openid)->send();
  137. break;
  138. case 'pink':
  139. /** @var StorePinkServices $pinkService */
  140. $pinkService = app()->make(StorePinkServices::class);
  141. /** @var StoreCombinationServices $combinationService */
  142. $combinationService = app()->make(StoreCombinationServices::class);
  143. $pinktInfo = $pinkService->get($thirdType[1]);
  144. $productInfo = $combinationService->get($pinktInfo->cid);
  145. $wechatNews['title'] = $productInfo->title;
  146. $wechatNews['image'] = $productInfo->image;
  147. $wechatNews['description'] = $productInfo->info;
  148. $wechatNews['url'] = $baseUrl . '/pages/activity/goods_combination_status/index?id=' . $thirdType[1];
  149. $messages = Messages::newMessage($wechatNews);
  150. OfficialAccount::staffService()->message($messages)->to($openid)->send();
  151. break;
  152. case 'lucklottery':
  153. try {
  154. $lottery = $qrInfo['lottery'] ?? [];
  155. $wechatNews['title'] = $lottery['name'] ?? '关注成功,立即参与抽奖';
  156. $wechatNews['image'] = $lottery['image'] ?? '';
  157. $wechatNews['description'] = $lottery['name'] ?? '关注成功,获得一次抽奖机会';
  158. $wechatNews['url'] = $baseUrl . '/pages/goods/lottery/grids/index?type=5&spread=' . $thirdType[1] ?? '';
  159. $messages = Messages::newMessage($wechatNews);
  160. OfficialAccount::staffService()->message($messages)->to($openid)->send();
  161. } catch (\Exception $e) {
  162. \think\facade\Log::error('发送关注抽奖失败:' . $e->getMessage());
  163. $response = $e->getMessage();
  164. }
  165. break;
  166. case 'wechatqrcode'://渠道码
  167. /** @var WechatQrcodeServices $wechatQrcodeService */
  168. $wechatQrcodeService = app()->make(WechatQrcodeServices::class);
  169. /** @var WechatUserServices $wechatUser */
  170. $wechatUser = app()->make(WechatUserServices::class);
  171. /** @var UserServices $userService */
  172. $userService = app()->make(UserServices::class);
  173. /** @var LoginServices $loginService */
  174. $loginService = app()->make(LoginServices::class);
  175. try {
  176. //wechatqrcode类型的二维码数据中,third_id为渠道码的id
  177. $qrcodeInfo = $wechatQrcodeService->qrcodeInfo($qrInfo['third_id']);
  178. $spreadUid = $qrcodeInfo['uid'];
  179. $spreadInfo = $userService->get($spreadUid);
  180. $is_new = $wechatUser->saveUser($openid);
  181. $uid = $wechatUser->getFieldValue($openid, 'openid', 'uid', ['user_type', '<>', 'h5']);
  182. $userInfo = $userService->get($uid);
  183. if ($qrcodeInfo['status'] == 0 || $qrcodeInfo['is_del'] == 1 || ($qrcodeInfo['end_time'] < time() && $qrcodeInfo['end_time'] > 0)) {
  184. $response = '二维码已失效';
  185. } else if ($spreadUid == $uid) {
  186. $response = '自己不能推荐自己';
  187. } else if (!$userInfo) {
  188. $response = '用户不存在';
  189. } else if (!$spreadInfo) {
  190. $response = '上级用户不存在';
  191. } else if ($loginService->updateUserInfo(['code' => $spreadUid], $userInfo, $is_new)) {
  192. //写入扫码记录,返回内容
  193. $response = $wechatQrcodeService->wechatQrcodeRecord($qrcodeInfo, $userInfo, $spreadInfo, 1);
  194. }
  195. } catch (\Exception $e) {
  196. $response = $e->getMessage();
  197. }
  198. break;
  199. }
  200. return $response;
  201. }
  202. /**
  203. * 扫码发送图文消息
  204. * @param $title
  205. * @param $image
  206. * @param $info
  207. * @param $url
  208. * @param $openId
  209. * @throws InvalidArgumentException
  210. * @throws InvalidConfigException
  211. * @throws RuntimeException
  212. */
  213. public function sendMessage($title, $image, $info, $url, $openId)
  214. {
  215. $wechatNews['title'] = $title;
  216. $wechatNews['image'] = $image;
  217. $wechatNews['description'] = $info;
  218. $wechatNews['url'] = $url;
  219. $message = Messages::newMessage($wechatNews);
  220. OfficialAccount::staffService()->message($message)->to($openId)->send();
  221. }
  222. /**
  223. * 扫码
  224. * @param $message
  225. * @return array|Image|News|Text|Transfer|Voice|string
  226. * @throws InvalidArgumentException
  227. * @throws InvalidConfigException
  228. * @throws RuntimeException
  229. * @throws \think\db\exception\DataNotFoundException
  230. * @throws \think\db\exception\DbException
  231. * @throws \think\db\exception\ModelNotFoundException
  232. */
  233. public function wechatEventScan($message)
  234. {
  235. /** @var QrcodeServices $qrcodeService */
  236. $qrcodeService = app()->make(QrcodeServices::class);
  237. /** @var WechatReplyServices $wechatReplyService */
  238. $wechatReplyService = app()->make(WechatReplyServices::class);
  239. /** @var WechatUserServices $wechatUser */
  240. $wechatUser = app()->make(WechatUserServices::class);
  241. /** @var UserServices $userService */
  242. $userService = app()->make(UserServices::class);
  243. /** @var LoginServices $loginService */
  244. $loginService = app()->make(LoginServices::class);
  245. $qrInfo = [];
  246. $response = $wechatReplyService->reply('subscribe', $message['FromUserName']);
  247. if ($message['EventKey'] && ($qrInfo = $qrcodeService->getQrcode($message['Ticket'], 'ticket'))) {
  248. $qrcodeService->scanQrcode($message['Ticket'], 'ticket');
  249. $response = $this->wechatEvent($qrInfo, $message['FromUserName']);
  250. }
  251. if ($qrInfo && strtolower($qrInfo['third_type']) == 'wechatqrcode') {
  252. /** @var WechatQrcodeServices $wechatQrcodeService */
  253. $wechatQrcodeService = app()->make(WechatQrcodeServices::class);
  254. try {
  255. //wechatqrcode类型的二维码数据中,third_id为渠道码的id
  256. $qrcodeInfo = $wechatQrcodeService->qrcodeInfo($qrInfo['third_id']);
  257. $spreadUid = $qrcodeInfo['uid'];
  258. $spreadInfo = $userService->get($spreadUid);
  259. $is_new = $wechatUser->saveUser($message['FromUserName']);
  260. $uid = $wechatUser->getFieldValue($message['FromUserName'], 'openid', 'uid', ['user_type', '<>', 'h5']);
  261. $userInfo = $userService->get($uid);
  262. if ($qrcodeInfo['status'] == 0 || $qrcodeInfo['is_del'] == 1 || ($qrcodeInfo['end_time'] < time() && $qrcodeInfo['end_time'] > 0)) {
  263. $response = '二维码已失效';
  264. } else if ($spreadUid == $uid) {
  265. $response = '自己不能推荐自己';
  266. } else if (!$userInfo) {
  267. $response = '用户不存在';
  268. } else if (!$spreadInfo) {
  269. $response = '上级用户不存在';
  270. } else if ($loginService->updateUserInfo(['code' => $spreadUid], $userInfo, $is_new)) {
  271. //写入扫码记录,返回内容
  272. $response = $wechatQrcodeService->wechatQrcodeRecord($qrcodeInfo, $userInfo, $spreadInfo, 1);
  273. }
  274. } catch (\Exception $e) {
  275. $response = $e->getMessage();
  276. }
  277. }
  278. $ticket = $message['EventKey'];
  279. if (strpos($ticket, 'wechat_scan_login:') === 0) {
  280. $openId = $message['FromUserName'];
  281. $key = str_replace('wechat_scan_login:', '', $ticket);
  282. /** @var WechatUserServices $wechatUserSerives */
  283. $wechatUserSerives = app()->make(WechatUserServices::class);
  284. $wechatUser = $wechatUserSerives->getWechatUserInfo(['openid' => $openId, 'is_del' => 0]);
  285. if ($wechatUser) {
  286. CacheService::set('wechat_scan_login:' . $key, $wechatUser['uid']);
  287. }
  288. }
  289. return $response;
  290. }
  291. /**
  292. * 取消关注
  293. * @param $message
  294. */
  295. public function wechatEventUnsubscribe($message)
  296. {
  297. /** @var WechatUserServices $wechatUser */
  298. $wechatUser = app()->make(WechatUserServices::class);
  299. $wechatUser->unSubscribe($message['FromUserName']);
  300. }
  301. /**
  302. * 公众号关注
  303. * @param $message
  304. * @param $spread_uid
  305. * @return array|Image|News|Text|Transfer|Voice|string
  306. * @throws InvalidArgumentException
  307. * @throws InvalidConfigException
  308. * @throws RuntimeException
  309. * @throws \think\db\exception\DataNotFoundException
  310. * @throws \think\db\exception\DbException
  311. * @throws \think\db\exception\ModelNotFoundException
  312. */
  313. public function wechatEventSubscribe($message, $spread_uid)
  314. {
  315. /** @var WechatReplyServices $wechatReplyService */
  316. $wechatReplyService = app()->make(WechatReplyServices::class);
  317. $response = $wechatReplyService->reply('subscribe', $message['FromUserName']);
  318. if (isset($message['EventKey'])) {
  319. /** @var QrcodeServices $qrcodeService */
  320. $qrcodeService = app()->make(QrcodeServices::class);
  321. if ($message['EventKey'] && ($qrInfo = $qrcodeService->getQrcode($message['Ticket'], 'ticket'))) {
  322. $qrcodeService->scanQrcode($message['Ticket'], 'ticket');
  323. $response = $this->wechatEvent($qrInfo, $message['FromUserName']);
  324. }
  325. }
  326. //是否开启
  327. if (sys_config('create_wechat_user', 1)) {
  328. try {
  329. /** @var WechatUserServices $wechatUserSerives */
  330. $wechatUserSerives = app()->make(WechatUserServices::class);
  331. $wechatUserSerives->saveUser($message['FromUserName'], $spread_uid);
  332. } catch (\Throwable $e) {
  333. Log::error('关注公众号生成用户失败,原因:' . $e->getMessage() . $e->getFile() . $e->getLine());
  334. }
  335. }
  336. return $response;
  337. }
  338. /**
  339. * 位置 事件
  340. * @param $message
  341. * @return string
  342. */
  343. public function wechatEventLocation($message)
  344. {
  345. //return 'location';
  346. }
  347. /**
  348. * 跳转URL 事件
  349. * @param $message
  350. * @return string
  351. */
  352. public function wechatEventView($message)
  353. {
  354. //return 'view';
  355. }
  356. /**
  357. * 图片 消息
  358. * @param $message
  359. * @return string
  360. */
  361. public function wechatMessageImage($message)
  362. {
  363. //return 'image';
  364. }
  365. /**
  366. * 语音 消息
  367. * @param $message
  368. * @return string
  369. */
  370. public function wechatMessageVoice($message)
  371. {
  372. //return 'voice';
  373. }
  374. /**
  375. * 视频 消息
  376. * @param $message
  377. * @return string
  378. */
  379. public function wechatMessageVideo($message)
  380. {
  381. //return 'video';
  382. }
  383. /**
  384. * 位置 消息
  385. */
  386. public function wechatMessageLocation($message)
  387. {
  388. //return 'location';
  389. }
  390. /**
  391. * 链接 消息
  392. * @param $message
  393. * @return string
  394. */
  395. public function wechatMessageLink($message)
  396. {
  397. //return 'link';
  398. }
  399. /**
  400. * 其它消息 消息
  401. */
  402. public function wechatMessageOther($message)
  403. {
  404. //return 'other';
  405. }
  406. /**
  407. * 领取卡券
  408. * @param $message
  409. */
  410. public function wechatEventUserGetCard($message)
  411. {
  412. try {
  413. /** @var UserCardServices $userCardServices */
  414. $userCardServices = app()->make(UserCardServices::class);
  415. $userCardServices->userGetCard($message);
  416. } catch (\Throwable $e) {
  417. Log::error('领取微信卡券失败,原因:' . $e->getMessage() . $e->getFile() . $e->getLine());
  418. }
  419. }
  420. /**
  421. * 激活卡券
  422. * @param $message
  423. */
  424. public function wechatEventSubmitMembercardUserInfo($message)
  425. {
  426. try {
  427. /** @var UserCardServices $userCardServices */
  428. $userCardServices = app()->make(UserCardServices::class);
  429. $userCardServices->userSubmitCard($message);
  430. } catch (\Throwable $e) {
  431. Log::error('激活微信卡券失败,原因:' . $e->getMessage() . $e->getFile() . $e->getLine());
  432. }
  433. }
  434. /**
  435. * 删除卡券
  436. * @param $message
  437. */
  438. public function wechatEventUserDelCard($message)
  439. {
  440. /** @var UserCardServices $userCardServices */
  441. $userCardServices = app()->make(UserCardServices::class);
  442. $userCardServices->userDelCard($message);
  443. }
  444. }