WechatNotice.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace addons\BusinessCard\common\logic;
  3. use addons\BusinessCard\common\enums\CardEnums;
  4. use addons\BusinessCard\common\models\BusinessCard;
  5. use common\enums\RabbitMqEnum;
  6. use common\enums\StatusEnum;
  7. use common\helpers\FormatHelper;
  8. use common\helpers\wechat\WechatTemplate;
  9. use common\models\common\PlatformSetting;
  10. use common\models\mall\Mall;
  11. use common\models\order\Order;
  12. use common\models\order\OrderDetail;
  13. use common\models\user\User;
  14. use common\models\user\UserInfo;
  15. use Exception;
  16. use GuzzleHttp\Exception\GuzzleException;
  17. class WechatNotice
  18. {
  19. /**
  20. * @param array $data item_id,user_id,mall_id,log_type
  21. * @return void
  22. * @throws Exception
  23. */
  24. public static function handler(array $data) {
  25. if (!isset($data['log_type'])) return;
  26. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, $data,
  27. self::class, 'async_handle');
  28. }
  29. public static function async_handle($data)
  30. {
  31. $setting = \Yii::$app->services->setting->addons->get($data['mall_id'], CardEnums::ADDONS_NAME, 'basic');
  32. if (empty($setting["wechat_notice_{$data['log_type']}"])) {
  33. return;
  34. }
  35. $limit_key = "wechat_notice_limit_{$data['log_type']}";
  36. $limit = intval($setting[$limit_key] ?? 0);
  37. $cache_key = "{$limit_key}:{$data['to_user_id']}";
  38. if ($limit > 0 && \Yii::$app->cache->get($cache_key)) { // 之前已经发送过一次
  39. return;
  40. }
  41. $user = User::getOne([
  42. ['mall_id' => $data['mall_id']],
  43. ['id' => $data['user_id']], // 执行动作人
  44. ['status' => StatusEnum::ENABLED]
  45. ]);
  46. if (empty($user)) {
  47. return;
  48. }
  49. $openid = UserInfo::find()
  50. ->where(['mall_id' => $data['mall_id']])
  51. ->andWhere(['user_id' => $data['to_user_id']]) // 接受通知人
  52. ->andWhere(['platform' => 'wechat'])
  53. ->select('openid')
  54. ->scalar();
  55. if (!empty($openid)) {
  56. $config = [];
  57. switch ($data['log_type']) {
  58. case CardEnums::LOG_TYPE_MATEREAL_DETAIL: // 查看素材
  59. $config = self::material($user);
  60. break;
  61. case CardEnums::LOG_TYPE_CARD: // 查看名片 - 短时间内不可发送第二条
  62. $config = self::view($user);
  63. break;
  64. case CardEnums::LOG_TYPE_ORDER: // 下单
  65. $order = Order::getOne([
  66. 'where' => [
  67. 'id' => $data['order_id']
  68. ]
  69. ], true, ['detail']);
  70. $config = self::order($user, $order);
  71. break;
  72. }
  73. if (!empty($config)) {
  74. try {
  75. $Platform_config = PlatformSetting::getConfByGroup( 'system', true);
  76. $list['web_url'] = $Platform_config['h5_url']['value'];
  77. $mall = Mall::getOne([
  78. ['id' => $data['mall_id']]
  79. ], true, [], null, 'mall_sign');
  80. if (empty($mall)) throw new Exception("商城 {$data['mall_id']} 不存在");
  81. $card = BusinessCard::getOne([
  82. ['mall_id' => $data['mall_id']],
  83. ['user_id' => $data['user_id']],
  84. ['status' => StatusEnum::ENABLED]
  85. ], true);
  86. // /plugins2/BusinessCard/index?sign=ada0cf84124e914e11694614d57b925b&scene=f679acfb9506
  87. // /plugins2/BusinessCard/my/customDetail?user_id=1505
  88. $url = "{$list['web_url']}/#/plugins2/BusinessCard/log?menu_id=2&sign={$mall['mall_sign']}";
  89. // if (!empty($card)) {
  90. // $url = "$url&card_id={$card['id']}";
  91. // } else {
  92. // $url = "{$list['web_url']}/#/plugins2/BusinessCard/my/customDetail?sign={$mall['mall_sign']}&user_id={$data['user_id']}";
  93. // }
  94. $wechat = new WechatTemplate($data['mall_id']);
  95. $res = $wechat->sendTemplate($data['to_user_id'], '', $url, $config, null, $setting["wechat_notice_{$data['log_type']}"]);
  96. if ($res && $limit > 0) {
  97. \Yii::$app->cache->set($cache_key, 1, intval($limit * 3600));
  98. }
  99. } catch (Exception $e) {
  100. \Yii::error(__METHOD__ . " - " .FormatHelper::exception($e, __METHOD__));
  101. } catch (GuzzleException $e) {
  102. \Yii::error(__METHOD__ . " - " .FormatHelper::exception($e, __METHOD__));
  103. }
  104. }
  105. }
  106. }
  107. /**
  108. * @param User $user
  109. * @return array
  110. */
  111. protected static function material(User $user)
  112. {
  113. return [
  114. 'first' => "用户 {$user->nickname} (ID:{$user->id}) 查看了您的素材",
  115. 'thing1' => $user->nickname,
  116. 'time2' => date("Y-m-d H:i:s"),
  117. 'const3' => '查看了素材',
  118. 'remark' => ''
  119. ];
  120. }
  121. /**
  122. * @param User $user
  123. * @return array
  124. */
  125. protected static function view(User $user)
  126. {
  127. return [
  128. 'first' => "用户 {$user->nickname} (ID:{$user->id}) 访问了您的名片",
  129. 'thing1' => $user->nickname,
  130. 'time2' => date("Y-m-d H:i:s"),
  131. 'phone_number4' => $user->mobile,
  132. 'const3' => '访问了名片',
  133. 'remark' => ''
  134. ];
  135. }
  136. /**
  137. * @param User $user
  138. * @param array $order
  139. * @return array
  140. */
  141. protected static function order(User $user, array $order)
  142. {
  143. $goods_names = OrderDetail::find()->where(['order_id' => $order['id']])
  144. ->select('goods_name')->column();
  145. $total = count($goods_names);
  146. return [
  147. 'first' => "用户 {$user->nickname} (ID:{$user->id}) 通过您的名片购买了商品",
  148. 'thing2' => $user->nickname,
  149. 'character_string9' => $order['order_no'],
  150. // 'thing7' => bcadd($order['goods_price'], $order['shipping_money'], 2),
  151. 'thing7' => mb_substr($goods_names[0] ?? '未知商品', 0, 10) . "...,等共{$total}款商品", // 商品名称
  152. 'time4' => date('Y-m-d H:i:s', $order['created_at']),
  153. // 'keyword4' => $order['order_no'],
  154. 'remark' => '查看详情'
  155. ];
  156. }
  157. }