NoticeLogic.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\logic;
  15. use app\common\enum\notice\NoticeEnum;
  16. use app\common\enum\YesNoEnum;
  17. use app\common\model\notice\NoticeRecord;
  18. use app\common\model\notice\NoticeSetting;
  19. use app\common\model\user\User;
  20. use app\common\service\sms\SmsMessageService;
  21. /**
  22. * 通知逻辑层
  23. * Class NoticeLogic
  24. * @package app\common\logic
  25. */
  26. class NoticeLogic extends BaseLogic
  27. {
  28. /**
  29. * @notes 根据场景发送短信
  30. * @param $params
  31. * @return bool
  32. * @author 段誉
  33. * @date 2022/9/15 15:28
  34. */
  35. public static function noticeByScene($params)
  36. {
  37. try {
  38. $noticeSetting = NoticeSetting::where('scene_id', $params['scene_id'])->findOrEmpty()->toArray();
  39. if (empty($noticeSetting)) {
  40. throw new \Exception('找不到对应场景的配置');
  41. }
  42. // 合并额外参数
  43. $params = self::mergeParams($params);
  44. $res = false;
  45. self::setError('发送通知失败');
  46. // 短信通知
  47. if (isset($noticeSetting['sms_notice']['status']) && $noticeSetting['sms_notice']['status'] == YesNoEnum::YES) {
  48. $res = (new SmsMessageService())->send($params);
  49. }
  50. return $res;
  51. } catch (\Exception $e) {
  52. self::setError($e->getMessage());
  53. return false;
  54. }
  55. }
  56. /**
  57. * @notes 整理参数
  58. * @param $params
  59. * @return array
  60. * @author 段誉
  61. * @date 2022/9/15 15:28
  62. */
  63. public static function mergeParams($params)
  64. {
  65. // 用户相关
  66. if (!empty($params['params']['user_id'])) {
  67. $user = User::findOrEmpty($params['params']['user_id'])->toArray();
  68. $params['params']['nickname'] = $user['nickname'];
  69. $params['params']['user_name'] = $user['nickname'];
  70. $params['params']['user_sn'] = $user['sn'];
  71. $params['params']['mobile'] = $params['params']['mobile'] ?? $user['mobile'];
  72. }
  73. // 跳转路径
  74. $jumpPath = self::getPathByScene($params['scene_id'], $params['params']['order_id'] ?? 0);
  75. $params['url'] = $jumpPath['url'];
  76. $params['page'] = $jumpPath['page'];
  77. return $params;
  78. }
  79. /**
  80. * @notes 根据场景获取跳转链接
  81. * @param $sceneId
  82. * @param $extraId
  83. * @return string[]
  84. * @author 段誉
  85. * @date 2022/9/15 15:29
  86. */
  87. public static function getPathByScene($sceneId, $extraId)
  88. {
  89. // 小程序主页路径
  90. $page = '/pages/index/index';
  91. // 公众号主页路径
  92. $url = '/mobile/pages/index/index';
  93. return [
  94. 'url' => $url,
  95. 'page' => $page,
  96. ];
  97. }
  98. /**
  99. * @notes 替换消息内容中的变量占位符
  100. * @param $content
  101. * @param $params
  102. * @return array|mixed|string|string[]
  103. * @author 段誉
  104. * @date 2022/9/15 15:29
  105. */
  106. public static function contentFormat($content, $params)
  107. {
  108. foreach ($params['params'] as $k => $v) {
  109. $search = '{' . $k . '}';
  110. $content = str_replace($search, $v, $content);
  111. }
  112. return $content;
  113. }
  114. /**
  115. * @notes 添加通知记录
  116. * @param $params
  117. * @param $noticeSetting
  118. * @param $sendType
  119. * @param $content
  120. * @param string $extra
  121. * @return NoticeRecord|\think\Model
  122. * @author 段誉
  123. * @date 2022/9/15 15:29
  124. */
  125. public static function addNotice($params, $noticeSetting, $sendType, $content, $extra = '')
  126. {
  127. return NoticeRecord::create([
  128. 'user_id' => $params['params']['user_id'] ?? 0,
  129. 'title' => self::getTitleByScene($sendType, $noticeSetting),
  130. 'content' => $content,
  131. 'scene_id' => $noticeSetting['scene_id'],
  132. 'read' => YesNoEnum::NO,
  133. 'recipient' => $noticeSetting['recipient'],
  134. 'send_type' => $sendType,
  135. 'notice_type' => $noticeSetting['type'],
  136. 'extra' => $extra,
  137. ]);
  138. }
  139. /**
  140. * @notes 通知记录标题
  141. * @param $sendType
  142. * @param $noticeSetting
  143. * @return string
  144. * @author 段誉
  145. * @date 2022/9/15 15:30
  146. */
  147. public static function getTitleByScene($sendType, $noticeSetting)
  148. {
  149. switch ($sendType) {
  150. case NoticeEnum::SMS:
  151. $title = '';
  152. break;
  153. case NoticeEnum::OA:
  154. $title = $noticeSetting['oa_notice']['name'] ?? '';
  155. break;
  156. case NoticeEnum::MNP:
  157. $title = $noticeSetting['mnp_notice']['name'] ?? '';
  158. break;
  159. default:
  160. $title = '';
  161. }
  162. return $title;
  163. }
  164. }