NotifyConfigService.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace services\common;
  3. use Yii;
  4. use yii\helpers\Json;
  5. use common\enums\NotifyConfigTypeEnum;
  6. use common\helpers\ArrayHelper;
  7. use common\enums\StatusEnum;
  8. use common\models\common\NotifyConfig;
  9. use common\enums\AccessTokenGroupEnum;
  10. /**
  11. * Class NotifyConfigService
  12. * @package services\common
  13. * @author jianyan74 <751393839@qq.com>
  14. */
  15. class NotifyConfigService
  16. {
  17. /**
  18. * 发送消息
  19. *
  20. * @param $notifyConfigs
  21. * @param $auths
  22. * @param $targetId
  23. * @param $targetType
  24. * @param $data
  25. * @throws \GuzzleHttp\Exception\GuzzleException
  26. */
  27. public function send($notifyConfigs, $auths, $targetId, $targetType, $data)
  28. {
  29. try {
  30. foreach ($auths as $auth) {
  31. /** @var NotifyConfig $notifyConfig */
  32. foreach ($notifyConfigs as $notifyConfig) {
  33. // ios 推送
  34. if ($notifyConfig->type == NotifyConfigTypeEnum::APP_PUSH && $auth['oauth_client'] == AccessTokenGroupEnum::IOS) {
  35. $this->appIosRemind(
  36. $notifyConfig,
  37. $auth['oauth_client_user_id'],
  38. $targetId,
  39. $targetType,
  40. $data
  41. );
  42. }
  43. // 安卓 推送
  44. if ($notifyConfig->type == NotifyConfigTypeEnum::APP_PUSH && $auth['oauth_client'] == AccessTokenGroupEnum::ANDROID) {
  45. $this->appAndroidRemind(
  46. $notifyConfig,
  47. $auth['oauth_client_user_id'],
  48. $targetId,
  49. $targetType,
  50. $data
  51. );
  52. }
  53. // 微信推送
  54. if ($notifyConfig->type == NotifyConfigTypeEnum::WECHAT_MP && $auth['oauth_client'] == AccessTokenGroupEnum::WECHAT_MP) {
  55. $this->wechatRemind(
  56. $notifyConfig,
  57. $auth['oauth_client_user_id'],
  58. $data
  59. );
  60. }
  61. // 微信小程序推送
  62. if ($notifyConfig->type == NotifyConfigTypeEnum::WECHAT_MINI && $auth['oauth_client'] == AccessTokenGroupEnum::WECHAT_MINI) {
  63. $this->wechatMiniProgramRemind(
  64. $notifyConfig,
  65. $auth['oauth_client_user_id'],
  66. $data
  67. );
  68. }
  69. }
  70. }
  71. } catch (\Exception $e) {
  72. Yii::error($e->getMessage());
  73. // 记录行为日志
  74. Yii::$app->services->log->push(500, 'notifyConfig', Yii::$app->services->base->getErrorInfo($e));
  75. }
  76. }
  77. /**
  78. * @param NotifyConfig $config
  79. * @param $oauth_client_user_id
  80. * @param $targetId
  81. * @param $targetType
  82. * @param $data
  83. */
  84. public function appIosRemind(NotifyConfig $config, $oauth_client_user_id, $targetId, $targetType, $data)
  85. {
  86. $config->title = ArrayHelper::recursionGetVal($config->title, $data);
  87. $config->content = ArrayHelper::recursionGetVal($config->content, $data);
  88. Yii::$app->services->geTui->ios($config->title, $config->content, $oauth_client_user_id, [
  89. 'target_id' => $targetId,
  90. 'target_type' => $targetType,
  91. ]);
  92. }
  93. /**
  94. * @param NotifyConfig $config
  95. * @param $oauth_client_user_id
  96. * @param $targetId
  97. * @param $targetType
  98. * @param $data
  99. */
  100. public function appAndroidRemind(NotifyConfig $config, $oauth_client_user_id, $targetId, $targetType, $data)
  101. {
  102. $config->title = ArrayHelper::recursionGetVal($config->title, $data);
  103. $config->content = ArrayHelper::recursionGetVal($config->content, $data);
  104. Yii::$app->services->geTui->android($config->title, $config->content, $oauth_client_user_id, [
  105. 'target_id' => $targetId,
  106. 'target_type' => $targetType,
  107. ]);
  108. }
  109. /**
  110. * 微信消息
  111. *
  112. * @param NotifyConfig $config
  113. * @param $openid
  114. * @param $data
  115. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  116. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  117. * @throws \GuzzleHttp\Exception\GuzzleException
  118. */
  119. public function wechatRemind(NotifyConfig $config, $openid, $data)
  120. {
  121. $templateData = [];
  122. $content = Json::decode($config->content);
  123. foreach ($content as $item) {
  124. $templateData[$item['key']] = [
  125. 'value' => ArrayHelper::recursionGetVal($item['value'], $data),
  126. 'color' => !empty($item['color']) ? $item['color'] : '#000000'
  127. ];
  128. }
  129. $url = ArrayHelper::recursionGetVal($config->url, $data);
  130. $result = Yii::$app->wechat->app->template_message->send([
  131. 'touser' => $openid,
  132. 'template_id' => $config->template_id,
  133. 'url' => $url,
  134. 'data' => $templateData,
  135. ]);
  136. // 报错调试
  137. if (YII_DEBUG && isset($result['errcode']) && $result['errcode'] != 0) {
  138. Yii::$app->services->log->push(500, 'notifyConfig', $result);
  139. }
  140. }
  141. /**
  142. * 小程序消息
  143. *
  144. * @param NotifyConfig $config
  145. * @param $openid
  146. * @param $data
  147. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  148. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  149. * @throws \GuzzleHttp\Exception\GuzzleException
  150. */
  151. public function wechatMiniProgramRemind(NotifyConfig $config, $openid, $data)
  152. {
  153. $templateData = [];
  154. $content = Json::decode($config->content);
  155. foreach ($content as $item) {
  156. $templateData[$item['key']] = [
  157. 'value' => ArrayHelper::recursionGetVal($item['value'], $data),
  158. 'color' => !empty($item['color']) ? $item['color'] : '#000000'
  159. ];
  160. }
  161. $url = ArrayHelper::recursionGetVal($config->url, $data);
  162. $result = Yii::$app->wechat->miniProgram->subscribe_message->send([
  163. 'template_id' => $config->template_id, // 所需下发的订阅模板id
  164. 'touser' => $openid, // 接收者(用户)的 openid
  165. 'page' => $url, // 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
  166. 'data' => $templateData,
  167. ]);
  168. // 报错调试
  169. if (YII_DEBUG && isset($result['errcode']) && $result['errcode'] != 0) {
  170. Yii::$app->services->log->push(500, 'notifyConfig', $result);
  171. }
  172. }
  173. /**
  174. * @param $name
  175. * @param $merchant_id
  176. * @param $addon_name
  177. * @return array|\yii\db\ActiveRecord[]|NotifyConfig
  178. */
  179. public function findByName($name, $merchant_id, $addon_name = '')
  180. {
  181. return NotifyConfig::find()
  182. ->where([
  183. 'name' => $name,
  184. 'status' => StatusEnum::ENABLED,
  185. 'merchant_id' => $merchant_id
  186. ])
  187. ->andFilterWhere(['addon_name' => $addon_name])
  188. ->all();
  189. }
  190. /**
  191. * @param $addon_name
  192. * @param $type
  193. * @param $merchant_id
  194. * @return array|\yii\db\ActiveRecord[]
  195. */
  196. public function findByAddonName($addon_name, $type = AccessTokenGroupEnum::WECHAT_MINI, $merchant_id = 0)
  197. {
  198. return NotifyConfig::find()
  199. ->select(['name', 'title', 'template_id'])
  200. ->where([
  201. 'type' => $type,
  202. 'status' => StatusEnum::ENABLED,
  203. 'merchant_id' => $merchant_id,
  204. 'addon_name' => $addon_name
  205. ])
  206. ->asArray()
  207. ->all();
  208. }
  209. /**
  210. * @param $name
  211. * @param $merchant_id
  212. * @param $addon_name
  213. * @return array|\yii\db\ActiveRecord[]|NotifyConfig
  214. */
  215. public function findSysByName($name, $merchant_id, $addon_name = '')
  216. {
  217. $data = NotifyConfig::find()
  218. ->where([
  219. 'name' => $name,
  220. 'type' => NotifyConfigTypeEnum::SYS,
  221. 'status' => StatusEnum::ENABLED,
  222. 'merchant_id' => $merchant_id
  223. ])
  224. ->andFilterWhere(['addon_name' => $addon_name])
  225. ->one();
  226. if (!$data) {
  227. $data = new NotifyConfig();
  228. $data = $data->loadDefaultValues();
  229. }
  230. return $data;
  231. }
  232. }
粤ICP备19079148号