push.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace common\components;
  3. class push
  4. {
  5. // 消息类型
  6. const MSG_TYPE_ORDER = 'order'; // 订单消息
  7. const MSG_TYPE_LOGISTICS = 'logistics'; // 物流消息
  8. const MSG_TYPE_ACCOUNT = 'account'; // 账户消息
  9. const MSG_TYPE_WORK = 'work'; // 工作事项提醒
  10. const MSG_TYPE_SUBSCRIPTION = 'subscription'; // 订阅消息
  11. const SECRET = 'RIVATE_KEY_S^@VJDy7'; // 👈 与云函数中保持一致
  12. const GHS_URL = 'https://fc-mp-42d1a66f-2aec-4fc5-9d92-4d176bf3d337.next.bspapp.com/ghs/push';
  13. const HD_URL = 'https://fc-mp-027cce01-d39a-4ed2-8b09-1f4ff43ec945.next.bspapp.com/hd/push';
  14. // 私有属性
  15. private $url = '';
  16. private $msgType = '';
  17. public function __construct($client, $msgType) {
  18. if ($client == 'ghs') {
  19. $this->url = self::GHS_URL;
  20. } else if ($client == 'hd') {
  21. $this->url = self::HD_URL;
  22. } else {
  23. throw new \Exception('client not found');
  24. }
  25. $this->msgType = $msgType;
  26. }
  27. /**
  28. * 实现部分厂商特定功能,包括仅部分厂商支持、不常用或厂商临时新增的功能(不依赖 uni-push,厂商文档支持的参数可直接使用)。
  29. * 例如:推送渠道 ID、消息分类(部分厂商未配置时可能被限量推送或静默推送,即静音且需下拉系统通知栏才可见通知内容)、通知栏富文本
  30. */
  31. public function initOptions() {
  32. $optionsArr = [];
  33. switch ($this->msgType) {
  34. case self::MSG_TYPE_ORDER:
  35. $optionsArr = [ "android" =>
  36. [
  37. "XM" => [
  38. "/extra.channel_id" => "142743" //小米后台申请的通知类别id -- https://admin.xmpush.xiaomi.com/zh_CN/channel/list?appId=2882303761520146676
  39. ],
  40. "HW" => [
  41. "/message/android/category" => "EXPRESS"
  42. ],
  43. "VV" => [
  44. "/category" => "ORDER"
  45. ],
  46. ]
  47. ];
  48. break;
  49. case self::MSG_TYPE_LOGISTICS:
  50. $optionsArr = [ "android" =>
  51. [
  52. "XM" => [
  53. "/extra.channel_id" => "142744"
  54. ],
  55. "HW" => [
  56. "/message/android/category" => "EXPRESS"
  57. ],
  58. "VV" => [
  59. "/category" => "ORDER"
  60. ],
  61. ]
  62. ];
  63. break;
  64. case self::MSG_TYPE_ACCOUNT:
  65. $optionsArr = [ "android" =>
  66. [
  67. "XM" => [
  68. "/extra.channel_id" => "142895"
  69. ],
  70. "HW" => [
  71. "/message/android/category" => "ACCOUNT"
  72. ],
  73. "VV" => [
  74. "/category" => "ACCOUNT"
  75. ],
  76. ]
  77. ];
  78. break;
  79. case self::MSG_TYPE_WORK:
  80. $optionsArr = [ "android" =>
  81. [
  82. "XM" => [
  83. "/extra.channel_id" => "142892"
  84. ],
  85. "HW" => [
  86. "/message/android/category" => "WORK"
  87. ],
  88. "VV" => [
  89. "/category" => "TODO"
  90. ],
  91. ]
  92. ];
  93. break;
  94. case self::MSG_TYPE_SUBSCRIPTION:
  95. $optionsArr = [ "android" =>
  96. [
  97. "XM" => [
  98. "/extra.channel_id" => "0" // TODO 暂无
  99. ],
  100. "HW" => [
  101. "/message/android/category" => "SUBSCRIPTION"
  102. ],
  103. "VV" => [
  104. "/category" => "SUBSCRIPTION"
  105. ],
  106. ]
  107. ];
  108. break;
  109. }
  110. return $optionsArr;
  111. }
  112. public function pushByCloud($cids, $title, $content, $payload = [])
  113. {
  114. $sign = md5($title . $content . self::SECRET);
  115. $optionsArr = $this->initOptions();
  116. // 测试环境的特殊处理
  117. if ( getenv('YII_ENV') == 'dev') {
  118. $optionsArr["android"]["HW"]["/message/android/target_user_type"] = 1; //华为 -- 值为int 类型。1 表示华为测试消息,华为每个应用每日可发送该测试消息500条,此target_user_type 参数请勿发布至线上。
  119. $optionsArr["android"]["VV"]["/pushMode"] = 1; //VIVO -- 值为int 类型。0 表示正式推送;1 表示测试推送,不填默认为0。线上请勿使用测试推送
  120. $optionsArr["android"]["HO"]["/android/targetUserType"] = 1; //荣耀 -- 值为int 类型。1 表示测试推送,不填默认为0。荣耀每个应用每日可发送该测试消息1000条。此测试参数请勿发布至线上。
  121. }
  122. $data = [
  123. 'cid' => is_array($cids) ? $cids : [$cids],
  124. 'title' => $title,
  125. 'content' => $content,
  126. 'payload' => $payload,
  127. 'force_notification' => true, // true: 无论是离线推送还是在线推送,都自创建通知栏消息
  128. 'sign' => $sign,
  129. 'options' => $optionsArr
  130. ];
  131. $ch = curl_init($this->url);
  132. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  133. curl_setopt($ch, CURLOPT_POST, true);
  134. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  135. 'Content-Type: application/json'
  136. ]);
  137. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  138. $res = curl_exec($ch);
  139. curl_close($ch);
  140. return json_decode($res, true);
  141. }
  142. }