123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\services\message\wechat;
- use app\services\activity\bargain\StoreBargainServices;
- use app\services\activity\combination\StoreCombinationServices;
- use app\services\activity\combination\StorePinkServices;
- use app\services\activity\seckill\StoreSeckillServices;
- use app\services\BaseServices;
- use app\services\other\QrcodeServices;
- use app\services\product\product\StoreProductServices;
- use app\services\user\LoginServices;
- use app\services\user\UserServices;
- use app\services\wechat\WechatQrcodeServices;
- use app\services\wechat\WechatReplyServices;
- use app\services\wechat\WechatUserServices;
- use crmeb\services\app\WechatService;
- use think\facade\Log;
- class MessageServices extends BaseServices
- {
- /**
- * 扫码
- * @param $message
- * @return array|\EasyWeChat\Message\Image|\EasyWeChat\Message\News|\EasyWeChat\Message\Text|\EasyWeChat\Message\Transfer|\EasyWeChat\Message\Voice|mixed|string
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author 吴汐
- * @email 442384644@qq.com
- * @date 2023/02/24
- */
- public function wechatEventScan($message)
- {
- /** @var QrcodeServices $qrcodeService */
- $qrcodeService = app()->make(QrcodeServices::class);
- /** @var WechatReplyServices $wechatReplyService */
- $wechatReplyService = app()->make(WechatReplyServices::class);
- /** @var WechatUserServices $wechatUser */
- $wechatUser = app()->make(WechatUserServices::class);
- /** @var LoginServices $loginService */
- $loginService = app()->make(LoginServices::class);
- /** @var UserServices $userService */
- $userService = app()->make(UserServices::class);
- $response = $wechatReplyService->reply('subscribe');
- if ($message->EventKey && ($qrInfo = $qrcodeService->getQrcode($message->Ticket, 'ticket'))) {
- $qrcodeService->scanQrcode($message->Ticket, 'ticket');
- $thirdType = explode('-', $qrInfo['third_type']);
- $baseUrl = sys_config('site_url');
- if (in_array(strtolower($thirdType[0]), ['spread', 'agent', 'wechatqrcode', 'product', 'combination', 'seckill', 'bargain', 'pink'])) {
- //扫码需要生成用户流程
- $spreadUid = $qrInfo['third_id'];
- $spreadInfo = $userService->get($spreadUid);
- $is_new = $wechatUser->saveUser($message->FromUserName);
- $uid = $wechatUser->getFieldValue($message->FromUserName, 'openid', 'uid', ['user_type', '<>', 'h5']);
- $userInfo = $userService->get($uid);
- try {
- switch (strtolower($thirdType[0])) {
- case 'spread':
- if ($spreadUid == $uid) {
- $response = '自己不能推荐自己';
- } else if (!$userInfo) {
- $response = '用户不存在';
- } else if (!$spreadInfo) {
- $response = '上级用户不存在';
- } else if ($userInfo['spread_uid']) {
- $response = '已有推荐人!';
- } else if (!$loginService->updateUserInfo(['code' => $spreadUid], $userInfo, $is_new)) {
- $response = '绑定推荐人失败!';
- }
- $wechatNews['title'] = sys_config('site_name');
- $wechatNews['image'] = sys_config('wap_login_logo');
- $wechatNews['url'] = $baseUrl . '/pages/index/index';
- $loginService->updateUserInfo(['code' => $spreadUid], $userInfo, $is_new);
- $messages = WechatService::newsMessage($wechatNews);
- WechatService::staffService()->message($messages)->to($message->FromUserName)->send();
- break;
- case 'agent':
- if ($spreadUid == $uid) {
- $response = '自己不能推荐自己';
- } else if (!$userInfo) {
- $response = '用户不存在';
- } else if (!$spreadInfo) {
- $response = '上级用户不存在';
- } else if ($userInfo->is_division) {
- $response = '您是事业部,不能绑定成为别人的员工';
- } else if ($userInfo->is_agent) {
- $response = '您是代理商,不能绑定成为别人的员工';
- } else if ($loginService->updateUserInfo(['code' => $spreadUid, 'is_staff' => 1], $userInfo, $is_new)) {
- $response = '绑定店员成功!';
- }
- break;
- case 'wechatqrcode':
- /** @var WechatQrcodeServices $wechatQrcodeService */
- $wechatQrcodeService = app()->make(WechatQrcodeServices::class);
- //wechatqrcode类型的二维码数据中,third_id为渠道码的id
- $qrcodeInfo = $wechatQrcodeService->qrcodeInfo($qrInfo['third_id']);
- $spreadUid = $qrcodeInfo['uid'];
- $spreadInfo = $userService->get($spreadUid);
- $is_new = $wechatUser->saveUser($message->FromUserName);
- $uid = $wechatUser->getFieldValue($message->FromUserName, 'openid', 'uid', ['user_type', '<>', 'h5']);
- $userInfo = $userService->get($uid);
- if ($qrcodeInfo['status'] == 0 || $qrcodeInfo['is_del'] == 1 || ($qrcodeInfo['end_time'] < time() && $qrcodeInfo['end_time'] > 0)) {
- $response = '二维码已失效';
- } else if ($spreadUid == $uid) {
- $response = '自己不能推荐自己';
- } else if (!$userInfo) {
- $response = '用户不存在';
- } else if (!$spreadInfo) {
- $response = '上级用户不存在';
- } else if ($loginService->updateUserInfo(['code' => $spreadUid], $userInfo, $is_new)) {
- //写入扫码记录,返回内容
- $response = $wechatQrcodeService->wechatQrcodeRecord($qrcodeInfo, $userInfo, $spreadInfo);
- }
- break;
- case 'product':
- /** @var StoreProductServices $productService */
- $productService = app()->make(StoreProductServices::class);
- $productInfo = $productService->get($thirdType[1] ?? 0);
- $wechatNews['title'] = $productInfo->store_name;
- $wechatNews['image'] = $productInfo->image;
- $wechatNews['description'] = $productInfo->store_info;
- $wechatNews['url'] = $baseUrl . '/pages/goods_details/index?id=' . $thirdType[1];
- $loginService->updateUserInfo(['code' => $spreadUid], $userInfo, $is_new);
- $messages = WechatService::newsMessage($wechatNews);
- WechatService::staffService()->message($messages)->to($message->FromUserName)->send();
- break;
- case 'combination':
- /** @var StoreCombinationServices $combinationService */
- $combinationService = app()->make(StoreCombinationServices::class);
- $productInfo = $combinationService->get($thirdType[1] ?? 0);
- $wechatNews['title'] = $productInfo->title;
- $wechatNews['image'] = $productInfo->image;
- $wechatNews['description'] = $productInfo->info;
- $wechatNews['url'] = $baseUrl . '/pages/activity/goods_combination_details/index?id=' . $thirdType[1];
- $loginService->updateUserInfo(['code' => $spreadUid], $userInfo, $is_new);
- $messages = WechatService::newsMessage($wechatNews);
- WechatService::staffService()->message($messages)->to($message->FromUserName)->send();
- break;
- case 'seckill':
- /** @var StoreSeckillServices $seckillService */
- $seckillService = app()->make(StoreSeckillServices::class);
- $productInfo = $seckillService->get($thirdType[1] ?? 0);
- $wechatNews['title'] = $productInfo->title;
- $wechatNews['image'] = $productInfo->image;
- $wechatNews['description'] = $productInfo->info;
- $wechatNews['url'] = $baseUrl . '/pages/activity/goods_seckill_details/index?id=' . $thirdType[1];
- $loginService->updateUserInfo(['code' => $spreadUid], $userInfo, $is_new);
- $messages = WechatService::newsMessage($wechatNews);
- WechatService::staffService()->message($messages)->to($message->FromUserName)->send();
- break;
- case 'bargain':
- /** @var StoreBargainServices $bargainService */
- $bargainService = app()->make(StoreBargainServices::class);
- $productInfo = $bargainService->get($thirdType[1] ?? 0);
- $wechatNews['title'] = $productInfo->title;
- $wechatNews['image'] = $productInfo->image;
- $wechatNews['description'] = $productInfo->info;
- $wechatNews['url'] = $baseUrl . '/pages/activity/goods_bargain_details/index?id=' . $thirdType[1] . '&bargain=' . $thirdType[2];
- $loginService->updateUserInfo(['code' => $spreadUid], $userInfo, $is_new);
- $messages = WechatService::newsMessage($wechatNews);
- WechatService::staffService()->message($messages)->to($message->FromUserName)->send();
- break;
- case 'pink':
- /** @var StorePinkServices $pinkService */
- $pinkService = app()->make(StorePinkServices::class);
- /** @var StoreCombinationServices $combinationService */
- $combinationService = app()->make(StoreCombinationServices::class);
- $pinktInfo = $pinkService->get($thirdType[1]);
- $productInfo = $combinationService->get($pinktInfo->cid);
- $wechatNews['title'] = $productInfo->title;
- $wechatNews['image'] = $productInfo->image;
- $wechatNews['description'] = $productInfo->info;
- $wechatNews['url'] = $baseUrl . '/pages/activity/goods_combination_status/index?id=' . $thirdType[1];
- $loginService->updateUserInfo(['code' => $spreadUid], $userInfo, $is_new);
- $messages = WechatService::newsMessage($wechatNews);
- WechatService::staffService()->message($messages)->to($message->FromUserName)->send();
- break;
- }
- } catch (\Throwable $e) {
- $response = $e->getMessage();
- }
- } else {
- //扫码不生成用户流程
- }
- }
- return $response;
- }
- /**
- * 取消关注
- * @param $message
- */
- public function wechatEventUnsubscribe($message)
- {
- /** @var WechatUserServices $wechatUser */
- $wechatUser = app()->make(WechatUserServices::class);
- $wechatUser->unSubscribe($message->FromUserName);
- }
- /**
- * 公众号关注
- * @param $message
- * @return array|\EasyWeChat\Message\Image|\EasyWeChat\Message\News|\EasyWeChat\Message\Text|\EasyWeChat\Message\Transfer|\EasyWeChat\Message\Voice|mixed|string
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author 吴汐
- * @email 442384644@qq.com
- * @date 2023/02/24
- */
- public function wechatEventSubscribe($message)
- {
- /** @var QrcodeServices $qrcodeService */
- $qrcodeService = app()->make(QrcodeServices::class);
- /** @var WechatReplyServices $wechatReplyService */
- $wechatReplyService = app()->make(WechatReplyServices::class);
- /** @var WechatUserServices $wechatUser */
- $wechatUser = app()->make(WechatUserServices::class);
- /** @var LoginServices $loginService */
- $loginService = app()->make(LoginServices::class);
- /** @var UserServices $userService */
- $userService = app()->make(UserServices::class);
- $response = $wechatReplyService->reply('subscribe');
- if ($message->EventKey && ($qrInfo = $qrcodeService->getQrcode($message->Ticket, 'ticket'))) {
- $qrcodeService->scanQrcode($message->Ticket, 'ticket');
- $thirdType = explode('-', $qrInfo['third_type']);
- $baseUrl = sys_config('site_url');
- if (in_array(strtolower($thirdType[0]), ['spread', 'agent', 'wechatqrcode', 'product', 'combination', 'seckill', 'bargain', 'pink'])) {
- //扫码需要生成用户流程
- $spreadUid = $qrInfo['third_id'];
- $spreadInfo = $userService->get($spreadUid);
- $is_new = $wechatUser->saveUser($message->FromUserName);
- $uid = $wechatUser->getFieldValue($message->FromUserName, 'openid', 'uid', ['user_type', '<>', 'h5']);
- $userInfo = $userService->get($uid);
- try {
- switch (strtolower($thirdType[0])) {
- case 'spread':
- if ($spreadUid == $uid) {
- $response = '自己不能推荐自己';
- } else if (!$userInfo) {
- $response = '用户不存在';
- } else if (!$spreadInfo) {
- $response = '上级用户不存在';
- } else if ($userInfo['spread_uid']) {
- $response = '已有推荐人!';
- } else if (!$loginService->updateUserInfo(['code' => $spreadUid], $userInfo, $is_new)) {
- $response = '绑定推荐人失败!';
- }
- $wechatNews['title'] = sys_config('site_name');
- $wechatNews['image'] = sys_config('wap_login_logo');
- $wechatNews['url'] = $baseUrl . '/pages/index/index';
- $loginService->updateUserInfo(['code' => $spreadUid], $userInfo, $is_new);
- $messages = WechatService::newsMessage($wechatNews);
- WechatService::staffService()->message($messages)->to($message->FromUserName)->send();
- break;
- case 'agent':
- if ($spreadUid == $uid) {
- $response = '自己不能推荐自己';
- } else if (!$userInfo) {
- $response = '用户不存在';
- } else if (!$spreadInfo) {
- $response = '上级用户不存在';
- } else if ($userInfo->is_division) {
- $response = '您是事业部,不能绑定成为别人的员工';
- } else if ($userInfo->is_agent) {
- $response = '您是代理商,不能绑定成为别人的员工';
- } else if ($loginService->updateUserInfo(['code' => $spreadUid, 'is_staff' => 1], $userInfo, $is_new)) {
- $response = '绑定店员成功!';
- }
- break;
- case 'wechatqrcode':
- /** @var WechatQrcodeServices $wechatQrcodeService */
- $wechatQrcodeService = app()->make(WechatQrcodeServices::class);
- //wechatqrcode类型的二维码数据中,third_id为渠道码的id
- $qrcodeInfo = $wechatQrcodeService->qrcodeInfo($qrInfo['third_id']);
- $spreadUid = $qrcodeInfo['uid'];
- $spreadInfo = $userService->get($spreadUid);
- $is_new = $wechatUser->saveUser($message->FromUserName);
- $uid = $wechatUser->getFieldValue($message->FromUserName, 'openid', 'uid', ['user_type', '<>', 'h5']);
- $userInfo = $userService->get($uid);
- if ($qrcodeInfo['status'] == 0 || $qrcodeInfo['is_del'] == 1 || ($qrcodeInfo['end_time'] < time() && $qrcodeInfo['end_time'] > 0)) {
- $response = '二维码已失效';
- } else if ($spreadUid == $uid) {
- $response = '自己不能推荐自己';
- } else if (!$userInfo) {
- $response = '用户不存在';
- } else if (!$spreadInfo) {
- $response = '上级用户不存在';
- } else if ($loginService->updateUserInfo(['code' => $spreadUid], $userInfo, $is_new)) {
- //写入扫码记录,返回内容
- $response = $wechatQrcodeService->wechatQrcodeRecord($qrcodeInfo, $userInfo, $spreadInfo);
- }
- break;
- case 'product':
- /** @var StoreProductServices $productService */
- $productService = app()->make(StoreProductServices::class);
- $productInfo = $productService->get($thirdType[1] ?? 0);
- $wechatNews['title'] = $productInfo->store_name;
- $wechatNews['image'] = $productInfo->image;
- $wechatNews['description'] = $productInfo->store_info;
- $wechatNews['url'] = $baseUrl . '/pages/goods_details/index?id=' . $thirdType[1];
- $loginService->updateUserInfo(['code' => $spreadUid], $userInfo, $is_new);
- $messages = WechatService::newsMessage($wechatNews);
- WechatService::staffService()->message($messages)->to($message->FromUserName)->send();
- break;
- case 'combination':
- /** @var StoreCombinationServices $combinationService */
- $combinationService = app()->make(StoreCombinationServices::class);
- $productInfo = $combinationService->get($thirdType[1] ?? 0);
- $wechatNews['title'] = $productInfo->title;
- $wechatNews['image'] = $productInfo->image;
- $wechatNews['description'] = $productInfo->info;
- $wechatNews['url'] = $baseUrl . '/pages/activity/goods_combination_details/index?id=' . $thirdType[1];
- $loginService->updateUserInfo(['code' => $spreadUid], $userInfo, $is_new);
- $messages = WechatService::newsMessage($wechatNews);
- WechatService::staffService()->message($messages)->to($message->FromUserName)->send();
- break;
- case 'seckill':
- /** @var StoreSeckillServices $seckillService */
- $seckillService = app()->make(StoreSeckillServices::class);
- $productInfo = $seckillService->get($thirdType[1] ?? 0);
- $wechatNews['title'] = $productInfo->title;
- $wechatNews['image'] = $productInfo->image;
- $wechatNews['description'] = $productInfo->info;
- $wechatNews['url'] = $baseUrl . '/pages/activity/goods_seckill_details/index?id=' . $thirdType[1];
- $loginService->updateUserInfo(['code' => $spreadUid], $userInfo, $is_new);
- $messages = WechatService::newsMessage($wechatNews);
- WechatService::staffService()->message($messages)->to($message->FromUserName)->send();
- break;
- case 'bargain':
- /** @var StoreBargainServices $bargainService */
- $bargainService = app()->make(StoreBargainServices::class);
- $productInfo = $bargainService->get($thirdType[1] ?? 0);
- $wechatNews['title'] = $productInfo->title;
- $wechatNews['image'] = $productInfo->image;
- $wechatNews['description'] = $productInfo->info;
- $wechatNews['url'] = $baseUrl . '/pages/activity/goods_bargain_details/index?id=' . $thirdType[1] . '&bargain=' . $thirdType[2];
- $loginService->updateUserInfo(['code' => $spreadUid], $userInfo, $is_new);
- $messages = WechatService::newsMessage($wechatNews);
- WechatService::staffService()->message($messages)->to($message->FromUserName)->send();
- break;
- case 'pink':
- /** @var StorePinkServices $pinkService */
- $pinkService = app()->make(StorePinkServices::class);
- /** @var StoreCombinationServices $combinationService */
- $combinationService = app()->make(StoreCombinationServices::class);
- $pinktInfo = $pinkService->get($thirdType[1]);
- $productInfo = $combinationService->get($pinktInfo->cid);
- $wechatNews['title'] = $productInfo->title;
- $wechatNews['image'] = $productInfo->image;
- $wechatNews['description'] = $productInfo->info;
- $wechatNews['url'] = $baseUrl . '/pages/activity/goods_combination_status/index?id=' . $thirdType[1];
- $loginService->updateUserInfo(['code' => $spreadUid], $userInfo, $is_new);
- $messages = WechatService::newsMessage($wechatNews);
- WechatService::staffService()->message($messages)->to($message->FromUserName)->send();
- break;
- }
- } catch (\Throwable $e) {
- $response = $e->getMessage();
- }
- } else {
- //扫码不生成用户流程
- }
- }
- // 更新关注标识
- if (!is_string($response)) {
- $wechatUser->subscribe($message->FromUserName);
- }
- return $response;
- }
- /**
- * 位置 事件
- * @param $message
- * @return string
- */
- public function wechatEventLocation($message)
- {
- //return 'location';
- }
- /**
- * 跳转URL 事件
- * @param $message
- * @return string
- */
- public function wechatEventView($message)
- {
- //return 'view';
- }
- /**
- * 图片 消息
- * @param $message
- * @return string
- */
- public function wechatMessageImage($message)
- {
- //return 'image';
- }
- /**
- * 语音 消息
- * @param $message
- * @return string
- */
- public function wechatMessageVoice($message)
- {
- //return 'voice';
- }
- /**
- * 视频 消息
- * @param $message
- * @return string
- */
- public function wechatMessageVideo($message)
- {
- //return 'video';
- }
- /**
- * 位置 消息
- */
- public function wechatMessageLocation($message)
- {
- //return 'location';
- }
- /**
- * 链接 消息
- * @param $message
- * @return string
- */
- public function wechatMessageLink($message)
- {
- //return 'link';
- }
- /**
- * 其它消息 消息
- */
- public function wechatMessageOther($message)
- {
- //return 'other';
- }
- }
|