NotifyService.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace services\common;
  3. use Yii;
  4. use common\enums\MemberTypeEnum;
  5. use common\components\Service;
  6. use common\enums\NotifyTypeEnum;
  7. use common\enums\StatusEnum;
  8. use common\models\common\Notify;
  9. use common\models\common\NotifyMember;
  10. use common\helpers\ArrayHelper;
  11. use common\enums\SubscriptionActionEnum;
  12. use common\enums\NotifyConfigTypeEnum;
  13. /**
  14. * Class NotifyService
  15. * @package services\common
  16. * @author jianyan74 <751393839@qq.com>
  17. */
  18. class NotifyService extends Service
  19. {
  20. /**
  21. * 解析数据
  22. *
  23. * @param $data
  24. * @return array|array[]|object|object[]|string|string[]
  25. * @throws \yii\base\InvalidConfigException
  26. */
  27. public function analysisData($data)
  28. {
  29. $data = ArrayHelper::toArray($data);
  30. $data['time'] = Yii::$app->formatter->asDatetime(time());
  31. $data['ip'] = Yii::$app->services->base->getUserIp();
  32. return $data;
  33. }
  34. /**
  35. * 发送提醒
  36. *
  37. * @param $target_id
  38. * @param $targetType
  39. * @param int $merchantId
  40. * @param array $data
  41. * @throws \GuzzleHttp\Exception\GuzzleException
  42. * @throws \yii\base\InvalidConfigException
  43. */
  44. public function sendRemind($target_id, $targetType, $merchantId = 0, $data = [], $link = '')
  45. {
  46. $data = $this->analysisData($data);
  47. $addonName = Yii::$app->params['addon']['name'] ?? '';
  48. $config = Yii::$app->services->notifyConfig->findByName($targetType, $merchantId, $addonName);
  49. $sys = [];
  50. foreach ($config as $value) {
  51. if (empty($value->content)) {
  52. $value->attributes = SubscriptionActionEnum::default($targetType);
  53. }
  54. if (NotifyConfigTypeEnum::SYS == $value->type) {
  55. $sys = $value;
  56. $sys->content = ArrayHelper::recursionGetVal($value->content, $data);
  57. }
  58. }
  59. // 用户授权
  60. $auth = Yii::$app->services->memberAuth->findByMerchantId($merchantId, $merchantId > 0 ? MemberTypeEnum::MERCHANT : MemberTypeEnum::MANAGER);
  61. if (!empty($auth)) {
  62. Yii::$app->services->notifyConfig->send($config, $auth, $target_id, $targetType, $data);
  63. Yii::$app->services->merchant->setId($merchantId);
  64. }
  65. !empty($sys) && $this->createRemind($sys->content, $target_id, $targetType, $merchantId, $link);
  66. }
  67. /**
  68. * 创建一条提醒
  69. *
  70. * @param $content
  71. * @param $target_id
  72. * @param $target_type
  73. * @return false
  74. */
  75. public function createRemind($content, $target_id, $target_type, $merchant_id, $link = '')
  76. {
  77. $model = new Notify();
  78. $model->target_id = $target_id;
  79. $model->target_type = $target_type;
  80. $model->merchant_id = $merchant_id;
  81. $model->action = $target_type;
  82. $model->sender_id = 0;
  83. $model->link = $link;
  84. $model->type = NotifyTypeEnum::REMIND;
  85. $model->title = SubscriptionActionEnum::getValue($target_type);
  86. $model->content = $content;
  87. if ($model->save()) {
  88. $notifyMember = new NotifyMember();
  89. $notifyMember->notify_id = $model->id;
  90. $notifyMember->merchant_id = $merchant_id;
  91. $notifyMember->member_id = 0;
  92. $notifyMember->type = NotifyTypeEnum::REMIND;
  93. $notifyMember->save();
  94. }
  95. return false;
  96. }
  97. /**
  98. * 创建一条信息(私信)
  99. *
  100. * @param int $sender_id 触发id
  101. * @param string $content 内容
  102. * @param int $receiver 接收id
  103. */
  104. public function createMessage($content, $sender_id, $receiver)
  105. {
  106. $model = new Notify();
  107. $model->content = $content;
  108. $model->sender_id = $sender_id;
  109. $model->type = NotifyTypeEnum::MESSAGE;
  110. if ($model->save()) {
  111. $NotifyMember = new NotifyMember();
  112. $NotifyMember->notify_id = $model->id;
  113. $NotifyMember->member_id = $receiver;
  114. $NotifyMember->type = NotifyTypeEnum::MESSAGE;
  115. return $NotifyMember->save();
  116. }
  117. return false;
  118. }
  119. /**
  120. * 创建公告
  121. *
  122. * @param $title
  123. * @param $status
  124. * @param $target_id
  125. * @return bool
  126. */
  127. public function createAnnounce($title, $status, $target_id)
  128. {
  129. $model = Notify::find()
  130. ->where([
  131. 'target_id' => $target_id,
  132. 'type' => NotifyTypeEnum::ANNOUNCE,
  133. ])
  134. ->one();
  135. if (empty($model)) {
  136. $model = new Notify();
  137. $model = $model->loadDefaultValues();
  138. $model->type = NotifyTypeEnum::ANNOUNCE;
  139. $model->target_id = $target_id;
  140. }
  141. $model->title = $title;
  142. $model->status = $status;
  143. return $model->save();
  144. }
  145. /**
  146. * 拉取公告
  147. *
  148. * @param int $merchant_id 商户ID
  149. * @throws \yii\db\Exception
  150. */
  151. public function pullAnnounce($merchant_id, $created_at)
  152. {
  153. // 从 UserNotify 中获取最近的一条公告信息的创建时间: lastTime
  154. $model = NotifyMember::find()
  155. ->where(['merchant_id' => $merchant_id, 'type' => NotifyTypeEnum::ANNOUNCE])
  156. ->orderBy('id desc')
  157. ->asArray()
  158. ->one();
  159. // 用 lastTime 作为过滤条件,查询 Notify 的公告信息
  160. $lastTime = $model ? $model['created_at'] : $created_at;
  161. $notifies = Notify::find()
  162. ->where(['type' => NotifyTypeEnum::ANNOUNCE, 'status' => StatusEnum::ENABLED])
  163. ->andWhere(['>', 'created_at', $lastTime])
  164. ->asArray()
  165. ->all();
  166. // 新建 UserNotify 并关联查询出来的公告信息
  167. $rows = [];
  168. $fields = ['notify_id', 'merchant_id', 'app_id', 'type', 'created_at', 'updated_at'];
  169. $appId = Yii::$app->id;
  170. foreach ($notifies as $notify) {
  171. $rows[] = [$notify['id'], $merchant_id, $appId, NotifyTypeEnum::ANNOUNCE, $notify['created_at'], time()];
  172. }
  173. !empty($rows) && Yii::$app->db->createCommand()->batchInsert(NotifyMember::tableName(), $fields, $rows)->execute();
  174. }
  175. }
粤ICP备19079148号