| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- namespace addons\BusinessCard\common\logic;
- use addons\BusinessCard\common\enums\CardEnums;
- use addons\BusinessCard\common\models\BusinessCard;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- use common\helpers\wechat\WechatTemplate;
- use common\models\common\PlatformSetting;
- use common\models\mall\Mall;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\models\user\User;
- use common\models\user\UserInfo;
- use Exception;
- use GuzzleHttp\Exception\GuzzleException;
- class WechatNotice
- {
- /**
- * @param array $data item_id,user_id,mall_id,log_type
- * @return void
- * @throws Exception
- */
- public static function handler(array $data) {
- if (!isset($data['log_type'])) return;
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, $data,
- self::class, 'async_handle');
- }
- public static function async_handle($data)
- {
- $setting = \Yii::$app->services->setting->addons->get($data['mall_id'], CardEnums::ADDONS_NAME, 'basic');
- if (empty($setting["wechat_notice_{$data['log_type']}"])) {
- return;
- }
- $limit_key = "wechat_notice_limit_{$data['log_type']}";
- $limit = intval($setting[$limit_key] ?? 0);
- $cache_key = "{$limit_key}:{$data['to_user_id']}";
- if ($limit > 0 && \Yii::$app->cache->get($cache_key)) { // 之前已经发送过一次
- return;
- }
- $user = User::getOne([
- ['mall_id' => $data['mall_id']],
- ['id' => $data['user_id']], // 执行动作人
- ['status' => StatusEnum::ENABLED]
- ]);
- if (empty($user)) {
- return;
- }
- $openid = UserInfo::find()
- ->where(['mall_id' => $data['mall_id']])
- ->andWhere(['user_id' => $data['to_user_id']]) // 接受通知人
- ->andWhere(['platform' => 'wechat'])
- ->select('openid')
- ->scalar();
- if (!empty($openid)) {
- $config = [];
- switch ($data['log_type']) {
- case CardEnums::LOG_TYPE_MATEREAL_DETAIL: // 查看素材
- $config = self::material($user);
- break;
- case CardEnums::LOG_TYPE_CARD: // 查看名片 - 短时间内不可发送第二条
- $config = self::view($user);
- break;
- case CardEnums::LOG_TYPE_ORDER: // 下单
- $order = Order::getOne([
- 'where' => [
- 'id' => $data['order_id']
- ]
- ], true, ['detail']);
- $config = self::order($user, $order);
- break;
- }
- if (!empty($config)) {
- try {
- $Platform_config = PlatformSetting::getConfByGroup( 'system', true);
- $list['web_url'] = $Platform_config['h5_url']['value'];
- $mall = Mall::getOne([
- ['id' => $data['mall_id']]
- ], true, [], null, 'mall_sign');
- if (empty($mall)) throw new Exception("商城 {$data['mall_id']} 不存在");
- $card = BusinessCard::getOne([
- ['mall_id' => $data['mall_id']],
- ['user_id' => $data['user_id']],
- ['status' => StatusEnum::ENABLED]
- ], true);
- // /plugins2/BusinessCard/index?sign=ada0cf84124e914e11694614d57b925b&scene=f679acfb9506
- // /plugins2/BusinessCard/my/customDetail?user_id=1505
- $url = "{$list['web_url']}/#/plugins2/BusinessCard/log?menu_id=2&sign={$mall['mall_sign']}";
- // if (!empty($card)) {
- // $url = "$url&card_id={$card['id']}";
- // } else {
- // $url = "{$list['web_url']}/#/plugins2/BusinessCard/my/customDetail?sign={$mall['mall_sign']}&user_id={$data['user_id']}";
- // }
-
- $wechat = new WechatTemplate($data['mall_id']);
- $res = $wechat->sendTemplate($data['to_user_id'], '', $url, $config, null, $setting["wechat_notice_{$data['log_type']}"]);
- if ($res && $limit > 0) {
- \Yii::$app->cache->set($cache_key, 1, intval($limit * 3600));
- }
- } catch (Exception $e) {
- \Yii::error(__METHOD__ . " - " .FormatHelper::exception($e, __METHOD__));
- } catch (GuzzleException $e) {
- \Yii::error(__METHOD__ . " - " .FormatHelper::exception($e, __METHOD__));
- }
- }
- }
- }
- /**
- * @param User $user
- * @return array
- */
- protected static function material(User $user)
- {
- return [
- 'first' => "用户 {$user->nickname} (ID:{$user->id}) 查看了您的素材",
- 'thing1' => $user->nickname,
- 'time2' => date("Y-m-d H:i:s"),
- 'const3' => '查看了素材',
- 'remark' => ''
- ];
- }
- /**
- * @param User $user
- * @return array
- */
- protected static function view(User $user)
- {
- return [
- 'first' => "用户 {$user->nickname} (ID:{$user->id}) 访问了您的名片",
- 'thing1' => $user->nickname,
- 'time2' => date("Y-m-d H:i:s"),
- 'phone_number4' => $user->mobile,
- 'const3' => '访问了名片',
- 'remark' => ''
- ];
- }
- /**
- * @param User $user
- * @param array $order
- * @return array
- */
- protected static function order(User $user, array $order)
- {
- $goods_names = OrderDetail::find()->where(['order_id' => $order['id']])
- ->select('goods_name')->column();
- $total = count($goods_names);
- return [
- 'first' => "用户 {$user->nickname} (ID:{$user->id}) 通过您的名片购买了商品",
- 'thing2' => $user->nickname,
- 'character_string9' => $order['order_no'],
- // 'thing7' => bcadd($order['goods_price'], $order['shipping_money'], 2),
- 'thing7' => mb_substr($goods_names[0] ?? '未知商品', 0, 10) . "...,等共{$total}款商品", // 商品名称
- 'time4' => date('Y-m-d H:i:s', $order['created_at']),
- // 'keyword4' => $order['order_no'],
- 'remark' => '查看详情'
- ];
- }
- }
|