NoticeLogic.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. use app\common\service\wechat\WeChatOaService;
  22. /**
  23. * 通知逻辑层
  24. * Class NoticeLogic
  25. * @package app\common\logic
  26. */
  27. class NoticeLogic extends BaseLogic
  28. {
  29. /**
  30. * @notes 根据场景发送短信
  31. * @param $params
  32. * @return bool
  33. * @author 段誉
  34. * @date 2022/9/15 15:28
  35. */
  36. public static function noticeByScene($params)
  37. {
  38. try {
  39. $noticeSetting = NoticeSetting::where('scene_id', $params['scene_id'])->findOrEmpty()->toArray();
  40. if (empty($noticeSetting)) {
  41. throw new \Exception('找不到对应场景的配置');
  42. }
  43. // 合并额外参数
  44. $params = self::mergeParams($params);
  45. $res = false;
  46. self::setError('发送通知失败');
  47. // 短信验证码通知(具有时效性)
  48. if (isset($noticeSetting['sms_notice']['status']) && $noticeSetting['sms_notice']['status'] == YesNoEnum::YES && $noticeSetting['type'] == NoticeEnum::VERIFICATION_CODE) {
  49. $res = (new SmsMessageService())->send($params);
  50. }
  51. // 短信业务通知
  52. if (isset($noticeSetting['sms_notice']['status']) && $noticeSetting['sms_notice']['status'] == YesNoEnum::YES && $noticeSetting['type'] == NoticeEnum::BUSINESS_NOTIFICATION) {
  53. $res = (new SmsMessageService())->sendBusiness($params);
  54. }
  55. // 微信公众号消息模板业务通知
  56. if (isset($noticeSetting['oa_notice']['status']) && $noticeSetting['oa_notice']['status'] == YesNoEnum::YES
  57. && $noticeSetting['type'] == NoticeEnum::BUSINESS_NOTIFICATION
  58. && isset($params['params']['openid']) && !empty($params['params']['openid'])
  59. ) {
  60. $res = (new WeChatOaService())->sendTemplateMessage($params);
  61. }
  62. return $res;
  63. } catch (\Exception $e) {
  64. self::setError($e->getMessage());
  65. return false;
  66. }
  67. }
  68. /**
  69. * @notes 整理参数
  70. * @param $params
  71. * @return array
  72. * @author 段誉
  73. * @date 2022/9/15 15:28
  74. */
  75. public static function mergeParams($params)
  76. {
  77. // 用户相关
  78. if (!empty($params['params']['user_id'])) {
  79. $user = User::findOrEmpty($params['params']['user_id'])->toArray();
  80. $params['params']['nickname'] = $user['nickname'];
  81. $params['params']['user_name'] = $user['nickname'];
  82. $params['params']['user_sn'] = $user['sn'];
  83. $params['params']['mobile'] = $params['params']['mobile'] ?? $user['mobile'];
  84. }
  85. // 跳转路径
  86. $jumpPath = self::getPathByScene($params['scene_id'], $params['params']['order_id'] ?? 0);
  87. $params['url'] = $jumpPath['url'];
  88. $params['page'] = $jumpPath['page'];
  89. return $params;
  90. }
  91. /**
  92. * @notes 根据场景获取跳转链接
  93. * @param $sceneId
  94. * @param $extraId
  95. * @return string[]
  96. * @author 段誉
  97. * @date 2022/9/15 15:29
  98. */
  99. public static function getPathByScene($sceneId, $extraId)
  100. {
  101. // 小程序主页路径
  102. $page = '/pages/index/index';
  103. // 公众号主页路径
  104. $url = '/mobile/pages/index/index';
  105. return [
  106. 'url' => $url,
  107. 'page' => $page,
  108. ];
  109. }
  110. /**
  111. * @notes 替换消息内容中的变量占位符
  112. * @param $content
  113. * @param $params
  114. * @return array|mixed|string|string[]
  115. * @author 段誉
  116. * @date 2022/9/15 15:29
  117. */
  118. public static function contentFormat($content, $params)
  119. {
  120. foreach ($params['params'] as $k => $v) {
  121. $search = '{' . $k . '}';
  122. $content = str_replace($search, $v, $content);
  123. }
  124. return $content;
  125. }
  126. /**
  127. * @notes 添加通知记录
  128. * @param $params
  129. * @param $noticeSetting
  130. * @param $sendType
  131. * @param $content
  132. * @param string $extra
  133. * @return NoticeRecord|\think\Model
  134. * @author 段誉
  135. * @date 2022/9/15 15:29
  136. */
  137. public static function addNotice($params, $noticeSetting, $sendType, $content, $extra = '')
  138. {
  139. return NoticeRecord::create([
  140. 'user_id' => $params['params']['user_id'] ?? 0,
  141. 'title' => self::getTitleByScene($sendType, $noticeSetting),
  142. 'content' => $content,
  143. 'scene_id' => $noticeSetting['scene_id'],
  144. 'read' => YesNoEnum::NO,
  145. 'recipient' => $noticeSetting['recipient'],
  146. 'send_type' => $sendType,
  147. 'notice_type' => $noticeSetting['type'],
  148. 'extra' => $extra,
  149. ]);
  150. }
  151. /**
  152. * @notes 通知记录标题
  153. * @param $sendType
  154. * @param $noticeSetting
  155. * @return string
  156. * @author 段誉
  157. * @date 2022/9/15 15:30
  158. */
  159. public static function getTitleByScene($sendType, $noticeSetting)
  160. {
  161. switch ($sendType) {
  162. case NoticeEnum::SMS:
  163. $title = '';
  164. break;
  165. case NoticeEnum::OA:
  166. $title = $noticeSetting['oa_notice']['name'] ?? '';
  167. break;
  168. case NoticeEnum::MNP:
  169. $title = $noticeSetting['mnp_notice']['name'] ?? '';
  170. break;
  171. default:
  172. $title = '';
  173. }
  174. return $title;
  175. }
  176. }