MessageServices.php 26 KB

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