Notification.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace addons\shopro\notification\traits;
  3. use app\admin\model\shopro\Config;
  4. /**
  5. * 消息通知,额外方法
  6. */
  7. trait Notification
  8. {
  9. public function __construct($data = [])
  10. {
  11. $this->data = $data;
  12. $this->initConfig();
  13. }
  14. /**
  15. * 组合发送参数
  16. *
  17. * @param \think\Model $notifiable
  18. * @param string $type
  19. * @param \Closure|null $callback
  20. * @return array
  21. */
  22. protected function getParams($notifiable, $type, \Closure $callback = null)
  23. {
  24. $params = [];
  25. $params['data'] = $this->getData($notifiable);
  26. if ($callback) {
  27. $params = $callback($params);
  28. }
  29. $params = $this->formatParams($params, $type);
  30. return $params;
  31. }
  32. /**
  33. * 数据库通知数据
  34. *
  35. * @param \think\Model $notifiable
  36. * @return array
  37. */
  38. public function toDatabase($notifiable)
  39. {
  40. $type = str_replace('to', '', __FUNCTION__);
  41. $params = $this->getParams($notifiable, $type);
  42. $params['message_type'] = 'notification';
  43. $params['class_name'] = static::class;
  44. $params['message_text'] = $this->strReplace($this->template['MessageDefaultContent'], $params['data']);
  45. $params['message_title'] = $this->returnField['name'];
  46. return $params;
  47. }
  48. /**
  49. * 短信通知数据
  50. *
  51. * @param \think\Model $notifiable
  52. * @return array
  53. */
  54. public function toSms($notifiable)
  55. {
  56. $type = str_replace('to', '', __FUNCTION__);
  57. $params = $this->getParams($notifiable, $type, function ($params) use ($notifiable) {
  58. $params['mobile'] = $notifiable['mobile'] ? $notifiable['mobile'] : '';
  59. $this->template['MessageDefaultContent'] = $this->strReplace($this->template['MessageDefaultContent'], $params['data']);
  60. return $params;
  61. });
  62. return $params;
  63. }
  64. /**
  65. * 邮件通知数据
  66. *
  67. * @param \think\Model $notifiable
  68. * @return array
  69. */
  70. public function toEmail($notifiable)
  71. {
  72. $type = str_replace('to', '', __FUNCTION__);
  73. $params = $this->getParams($notifiable, $type);
  74. return $params;
  75. }
  76. /**
  77. * socket 通知数据
  78. *
  79. * @param \think\Model $notifiable
  80. * @return array
  81. */
  82. public function toWebsocket($notifiable)
  83. {
  84. $type = str_replace('to', '', __FUNCTION__);
  85. $params = $this->getParams($notifiable, $type);
  86. $params['message_type'] = 'notification';
  87. $params['class_name'] = static::class;
  88. $params['message_text'] = $this->strReplace($this->template['MessageDefaultContent'], $params['data']);
  89. $params['message_title'] = $this->returnField['name'];
  90. return $params;
  91. }
  92. /**
  93. * 微信公众号通知数据
  94. *
  95. * @param \think\Model $notifiable
  96. * @return array
  97. */
  98. public function toWechatOfficialAccount($notifiable)
  99. {
  100. $type = str_replace('to', '', __FUNCTION__);
  101. $params = $this->getParams($notifiable, $type, function ($params) use ($notifiable) {
  102. if ($oauth = $this->getWxOauth($notifiable, 'WechatOfficialAccount')) {
  103. // 公众号跳转地址,订单详情
  104. $path = $params['data']['h5_url'] ?? ($params['data']['jump_url'] ?? '');
  105. // 获取 h5 域名
  106. $url = $this->getH5DomainUrl($path);
  107. $params['openid'] = $oauth->openid;
  108. $params['url'] = $url;
  109. }
  110. return $params;
  111. });
  112. return $params;
  113. }
  114. /**
  115. * 微信小程序通知数据
  116. *
  117. * @param \think\Model $notifiable
  118. * @return array
  119. */
  120. public function toWechatMiniProgram($notifiable)
  121. {
  122. $type = str_replace('to', '', __FUNCTION__);
  123. $params = $this->getParams($notifiable, $type, function ($params) use ($notifiable) {
  124. if ($oauth = $this->getWxOauth($notifiable, 'WechatMiniProgram')) {
  125. // 小程序跳转地址,订单详情
  126. $path = $params['data']['mini_url'] ?? ($params['data']['jump_url'] ?? '');
  127. // 获取小程序完整路径
  128. $page = $this->getMiniDomainPage($path);
  129. $params['openid'] = $oauth->openid;
  130. $params['page'] = $page;
  131. }
  132. return $params;
  133. });
  134. return $params;
  135. }
  136. /**
  137. * 替换字符串中的标识
  138. *
  139. * @param [type] $content
  140. * @param [type] $data
  141. * @return void
  142. */
  143. protected function strReplace($content, $data)
  144. {
  145. foreach ($data as $key => $value) {
  146. $content = str_replace('{' . $key . '}', (string)$value, $content);
  147. }
  148. return $content;
  149. }
  150. /**
  151. * 获取微信授权 oauth
  152. */
  153. protected function getWxOauth($notifiable, $platform)
  154. {
  155. if ($this->receiver_type == 'admin') { // 后台管理员绑定的都是公众号,但是在 thirdOauth 中存的是 admin
  156. $platform = 'admin';
  157. } else {
  158. $platform = lcfirst(str_replace('Wechat', '', $platform));
  159. }
  160. $oauth = \app\admin\model\shopro\ThirdOauth::where($this->receiver_type . '_id', $notifiable['id'])
  161. ->where('provider', 'Wechat')
  162. ->where('platform', $platform)->find();
  163. if ($oauth && $oauth->openid) {
  164. return $oauth;
  165. }
  166. return null;
  167. }
  168. /**
  169. * 获取拼接域名的地址
  170. */
  171. protected function getH5DomainUrl($path)
  172. {
  173. $url = $path;
  174. $domain = Config::getConfigField('shop.basic.domain');
  175. if ($domain) {
  176. $domain = rtrim($domain, '/');
  177. $url = $domain . "/?page=" . urlencode($path);
  178. }
  179. return $url;
  180. }
  181. /**
  182. * 获取拼接的小程序地址
  183. */
  184. protected function getMiniDomainPage($path)
  185. {
  186. return "pages/index/index?page=" . urlencode($path);
  187. }
  188. }