| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- namespace services\common;
- use Yii;
- use yii\helpers\Json;
- use common\enums\NotifyConfigTypeEnum;
- use common\helpers\ArrayHelper;
- use common\enums\StatusEnum;
- use common\models\common\NotifyConfig;
- use common\enums\AccessTokenGroupEnum;
- /**
- * Class NotifyConfigService
- * @package services\common
- * @author jianyan74 <751393839@qq.com>
- */
- class NotifyConfigService
- {
- /**
- * 发送消息
- *
- * @param $notifyConfigs
- * @param $auths
- * @param $targetId
- * @param $targetType
- * @param $data
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function send($notifyConfigs, $auths, $targetId, $targetType, $data)
- {
- try {
- foreach ($auths as $auth) {
- /** @var NotifyConfig $notifyConfig */
- foreach ($notifyConfigs as $notifyConfig) {
- // ios 推送
- if ($notifyConfig->type == NotifyConfigTypeEnum::APP_PUSH && $auth['oauth_client'] == AccessTokenGroupEnum::IOS) {
- $this->appIosRemind(
- $notifyConfig,
- $auth['oauth_client_user_id'],
- $targetId,
- $targetType,
- $data
- );
- }
- // 安卓 推送
- if ($notifyConfig->type == NotifyConfigTypeEnum::APP_PUSH && $auth['oauth_client'] == AccessTokenGroupEnum::ANDROID) {
- $this->appAndroidRemind(
- $notifyConfig,
- $auth['oauth_client_user_id'],
- $targetId,
- $targetType,
- $data
- );
- }
- // 微信推送
- if ($notifyConfig->type == NotifyConfigTypeEnum::WECHAT_MP && $auth['oauth_client'] == AccessTokenGroupEnum::WECHAT_MP) {
- $this->wechatRemind(
- $notifyConfig,
- $auth['oauth_client_user_id'],
- $data
- );
- }
- // 微信小程序推送
- if ($notifyConfig->type == NotifyConfigTypeEnum::WECHAT_MINI && $auth['oauth_client'] == AccessTokenGroupEnum::WECHAT_MINI) {
- $this->wechatMiniProgramRemind(
- $notifyConfig,
- $auth['oauth_client_user_id'],
- $data
- );
- }
- }
- }
- } catch (\Exception $e) {
- Yii::error($e->getMessage());
- // 记录行为日志
- Yii::$app->services->log->push(500, 'notifyConfig', Yii::$app->services->base->getErrorInfo($e));
- }
- }
- /**
- * @param NotifyConfig $config
- * @param $oauth_client_user_id
- * @param $targetId
- * @param $targetType
- * @param $data
- */
- public function appIosRemind(NotifyConfig $config, $oauth_client_user_id, $targetId, $targetType, $data)
- {
- $config->title = ArrayHelper::recursionGetVal($config->title, $data);
- $config->content = ArrayHelper::recursionGetVal($config->content, $data);
- Yii::$app->services->geTui->ios($config->title, $config->content, $oauth_client_user_id, [
- 'target_id' => $targetId,
- 'target_type' => $targetType,
- ]);
- }
- /**
- * @param NotifyConfig $config
- * @param $oauth_client_user_id
- * @param $targetId
- * @param $targetType
- * @param $data
- */
- public function appAndroidRemind(NotifyConfig $config, $oauth_client_user_id, $targetId, $targetType, $data)
- {
- $config->title = ArrayHelper::recursionGetVal($config->title, $data);
- $config->content = ArrayHelper::recursionGetVal($config->content, $data);
- Yii::$app->services->geTui->android($config->title, $config->content, $oauth_client_user_id, [
- 'target_id' => $targetId,
- 'target_type' => $targetType,
- ]);
- }
- /**
- * 微信消息
- *
- * @param NotifyConfig $config
- * @param $openid
- * @param $data
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function wechatRemind(NotifyConfig $config, $openid, $data)
- {
- $templateData = [];
- $content = Json::decode($config->content);
- foreach ($content as $item) {
- $templateData[$item['key']] = [
- 'value' => ArrayHelper::recursionGetVal($item['value'], $data),
- 'color' => !empty($item['color']) ? $item['color'] : '#000000'
- ];
- }
- $url = ArrayHelper::recursionGetVal($config->url, $data);
- $result = Yii::$app->wechat->app->template_message->send([
- 'touser' => $openid,
- 'template_id' => $config->template_id,
- 'url' => $url,
- 'data' => $templateData,
- ]);
- // 报错调试
- if (YII_DEBUG && isset($result['errcode']) && $result['errcode'] != 0) {
- Yii::$app->services->log->push(500, 'notifyConfig', $result);
- }
- }
- /**
- * 小程序消息
- *
- * @param NotifyConfig $config
- * @param $openid
- * @param $data
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function wechatMiniProgramRemind(NotifyConfig $config, $openid, $data)
- {
- $templateData = [];
- $content = Json::decode($config->content);
- foreach ($content as $item) {
- $templateData[$item['key']] = [
- 'value' => ArrayHelper::recursionGetVal($item['value'], $data),
- 'color' => !empty($item['color']) ? $item['color'] : '#000000'
- ];
- }
- $url = ArrayHelper::recursionGetVal($config->url, $data);
- $result = Yii::$app->wechat->miniProgram->subscribe_message->send([
- 'template_id' => $config->template_id, // 所需下发的订阅模板id
- 'touser' => $openid, // 接收者(用户)的 openid
- 'page' => $url, // 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
- 'data' => $templateData,
- ]);
- // 报错调试
- if (YII_DEBUG && isset($result['errcode']) && $result['errcode'] != 0) {
- Yii::$app->services->log->push(500, 'notifyConfig', $result);
- }
- }
- /**
- * @param $name
- * @param $merchant_id
- * @param $addon_name
- * @return array|\yii\db\ActiveRecord[]|NotifyConfig
- */
- public function findByName($name, $merchant_id, $addon_name = '')
- {
- return NotifyConfig::find()
- ->where([
- 'name' => $name,
- 'status' => StatusEnum::ENABLED,
- 'merchant_id' => $merchant_id
- ])
- ->andFilterWhere(['addon_name' => $addon_name])
- ->all();
- }
- /**
- * @param $addon_name
- * @param $type
- * @param $merchant_id
- * @return array|\yii\db\ActiveRecord[]
- */
- public function findByAddonName($addon_name, $type = AccessTokenGroupEnum::WECHAT_MINI, $merchant_id = 0)
- {
- return NotifyConfig::find()
- ->select(['name', 'title', 'template_id'])
- ->where([
- 'type' => $type,
- 'status' => StatusEnum::ENABLED,
- 'merchant_id' => $merchant_id,
- 'addon_name' => $addon_name
- ])
- ->asArray()
- ->all();
- }
- /**
- * @param $name
- * @param $merchant_id
- * @param $addon_name
- * @return array|\yii\db\ActiveRecord[]|NotifyConfig
- */
- public function findSysByName($name, $merchant_id, $addon_name = '')
- {
- $data = NotifyConfig::find()
- ->where([
- 'name' => $name,
- 'type' => NotifyConfigTypeEnum::SYS,
- 'status' => StatusEnum::ENABLED,
- 'merchant_id' => $merchant_id
- ])
- ->andFilterWhere(['addon_name' => $addon_name])
- ->one();
- if (!$data) {
- $data = new NotifyConfig();
- $data = $data->loadDefaultValues();
- }
- return $data;
- }
- }
|