Msg.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace addons\notice\library\server;
  3. use addons\notice\library\GatewayTool;
  4. use addons\notice\library\ToData;
  5. use GatewayClient\Gateway;
  6. class Msg
  7. {
  8. // 获取通知数据
  9. public function getNoticeData($event, $template, $params)
  10. {
  11. $toData = ToData::get($event, $template, $params);
  12. $params = array_merge($params, $toData['default_field']);
  13. $content = self::formatParams($template['content'], $params);
  14. if ($template['platform'] == 'admin' || $template['platform'] == 'user') {
  15. foreach ($toData['to_id'] as $id) {
  16. try{
  17. $num = \app\admin\model\notice\Notice::where('to_id', $id)
  18. ->where('platform', $template['platform'])
  19. ->where('type','msg')
  20. ->order('id', 'desc')
  21. ->whereNull('readtime')
  22. ->count();
  23. // 判断是否开启socket通知
  24. $config = get_addon_config('notice');
  25. if ($config['admin_real'] == 2 && $template['platform'] == 'admin') {
  26. GatewayTool::sendByUid($template['platform'].'_'.$id, 'new_notice', ['msg' => $content, 'num' => $num+1, 'time' => time()]);
  27. }
  28. if ($config['user_real'] == 2 && $template['platform'] == 'user') {
  29. GatewayTool::sendByUid($template['platform'].'_'.$id, 'new_notice', ['msg' => $content, 'num' => $num+1, 'time' => time()]);
  30. }
  31. }catch (\Exception $e) {
  32. }
  33. }
  34. }
  35. // url
  36. $url = Msg::formatParams($template['url'], $params);
  37. $urlTitle = Msg::formatParams($template['url_title'], $params);
  38. $urlExt = [
  39. 'url' => $url,
  40. 'url_type' => $template['url_type'],
  41. 'url_title' => $urlTitle
  42. ];
  43. $ext = json_encode($urlExt, true);
  44. return [
  45. 'to_id' => $toData['to_id'],
  46. 'content' => $content,
  47. 'ext' => $ext
  48. ];
  49. }
  50. // 格式化模板数据
  51. public static function formatParams($content, $params) {
  52. if (preg_match_all("/(?<=({{)).+?(?=}})/", $content, $matches)) {
  53. foreach ($matches[0] as $k => $field) {
  54. $fieldVal = $params[$field] ?? null;
  55. if ($fieldVal !== null) {
  56. $content = str_replace("{{" . $field . "}}", $fieldVal, $content);
  57. }
  58. }
  59. }
  60. return $content;
  61. }
  62. }