JPushService.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Log;
  4. use JPush\Client;
  5. class JPushService
  6. {
  7. protected static $client = null;
  8. //推送类型
  9. const PUSH_TYPE_ALL = 1;
  10. const PUSH_TYPE_TAG = 2;
  11. const PUSH_TYPE_ALIAS = 3;
  12. const PUSH_TYPE_REG_ID = 4;
  13. private function __construct()
  14. {
  15. }
  16. private function __clone()
  17. {
  18. }
  19. /**
  20. * 获取实例
  21. */
  22. public static function getInstance()
  23. {
  24. if (!self::$client) {
  25. self::$client = new Client(config('jpush.app_key'), config('jpush.master_secret'), null);
  26. }
  27. return self::$client;
  28. }
  29. /**
  30. * 给android或ios推送消息
  31. */
  32. public static function pushNotify($params)
  33. {
  34. //推送平台
  35. $platform = $params['platform'] ?? 'all';
  36. //推送标题
  37. $title = $params['title'] ?? '';
  38. //推送内容
  39. $content = $params['content'] ?? '';
  40. //通知栏样式ID
  41. $builder_id = $params['builder_id'] ?? 0;
  42. //附加字段
  43. $extras = $params['extras'] ?? '';
  44. //推送类型
  45. $type = $params['type'] ?? '';
  46. //推送目标(注册ID)
  47. $reg_id = $params['reg_id'] ?? '';
  48. //推送目标(标签)
  49. $tag = $params['tag'] ?? '';
  50. //推送目标(别名)
  51. $alias = $params['alias'] ?? '';
  52. try {
  53. $push = self::getInstance()->push();
  54. //设置平台
  55. $push->setPlatform($platform);
  56. switch ($type) {
  57. case self::PUSH_TYPE_ALL:
  58. $push->addAllAudience();
  59. break;
  60. case self::PUSH_TYPE_TAG:
  61. $push->addTag($tag);
  62. break;
  63. case self::PUSH_TYPE_ALIAS:
  64. $push->addAlias($alias);
  65. break;
  66. case self::PUSH_TYPE_REG_ID:
  67. $push->addRegistrationId($reg_id);
  68. break;
  69. }
  70. $push->androidNotification($content, [
  71. 'title' => $title,
  72. 'builder_id' => $builder_id,
  73. 'extras' => $extras,
  74. ])->iosNotification($content, [
  75. 'sound' => 'sound',
  76. 'badge' => '+1',
  77. 'extras' => $extras
  78. ])->options([
  79. 'apns_production' => config('jpush.apns_production', true),
  80. //表示离线消息保留时长(秒)
  81. 'time_to_live' => 86400,
  82. ]);
  83. $response = $push->send();
  84. if ($response['http_code'] != 200) {
  85. Log::channel('jpush')->error(json_encode($response, JSON_UNESCAPED_UNICODE));
  86. }
  87. return $response;
  88. } catch (\Throwable $e) {
  89. Log::channel('jpush')->error(json_encode([
  90. 'file' => $e->getFile(),
  91. 'line' => $e->getLine(),
  92. 'message' => $e->getMessage(),
  93. 'params' => $params,
  94. ], JSON_UNESCAPED_UNICODE));
  95. }
  96. }
  97. /**
  98. * 获取指定设备的别名和标签
  99. */
  100. public static function getDevices($reg_id)
  101. {
  102. $response = self::getInstance()->device()->getDevices($reg_id);
  103. if ($response['http_code'] == 200) {
  104. return $response['body'];
  105. }
  106. return [];
  107. }
  108. /**
  109. * 给指定设备添加标签
  110. */
  111. public static function addTags($reg_id, $tags = [])
  112. {
  113. $response = self::getInstance()->device()->addTags($reg_id, $tags);
  114. if ($response['http_code'] == 200) {
  115. return true;
  116. }
  117. return false;
  118. }
  119. /**
  120. * 清空指定设备的标签
  121. */
  122. public static function clearTags($reg_id)
  123. {
  124. $response = self::getInstance()->device()->clearTags($reg_id);
  125. if ($response['http_code'] == 200) {
  126. return true;
  127. }
  128. return false;
  129. }
  130. /**
  131. * 清空指定设备的标签
  132. */
  133. public static function removeTags($reg_id, $tags = [])
  134. {
  135. $response = self::getInstance()->device()->removeTags($reg_id, $tags);
  136. if ($response['http_code'] == 200) {
  137. return true;
  138. }
  139. return false;
  140. }
  141. /**
  142. * 更新指定设备的别名
  143. */
  144. public static function updateAlias($reg_id, $alias)
  145. {
  146. $response = self::getInstance()->device()->updateAlias($reg_id, $alias);
  147. if ($response['http_code'] == 200) {
  148. return true;
  149. }
  150. return false;
  151. }
  152. /**
  153. * jgOpensslPrivateDecrypt($encrypted) (加密) 手机号码解密获取手机号
  154. * @param $encrypted string (加密) 手机号码
  155. * @param
  156. *
  157. * @return string 手机号|false
  158. * err:错误信息
  159. *
  160. */
  161. public function jgOpensslPrivateDecrypt($encrypted){
  162. $prefix = '-----BEGIN RSA PRIVATE KEY-----';
  163. $suffix = '-----END RSA PRIVATE KEY-----';
  164. $result = '';
  165. $prikey = JG_YJDL_PRIVATE_RSA;
  166. $key = $prefix . "\n" . $prikey . "\n" . $suffix;//拼接换行符
  167. $r = openssl_private_decrypt(base64_decode($encrypted), $result, openssl_pkey_get_private($key));
  168. if($r){
  169. return $result;
  170. }else{
  171. return false;
  172. }
  173. }
  174. /**
  175. * jgLoginTokenVerify($token) 提交loginToken,验证后返回手机号码(加密)
  176. * @param $token string 认证SDK获取到的loginToken
  177. * @param $exId 开发者自定义的id,非必填
  178. *
  179. * @return array 手机号
  180. * err:错误信息
  181. *
  182. */
  183. public function jgLoginTokenVerify($token,$exId)
  184. {
  185. $host = JG_YJDL_POST_URL;
  186. $method = "POST";
  187. $appcode = VERIFY_APPCODE;
  188. $headers = array();
  189. array_push($headers, "Authorization: Basic " .base64_encode(JG_APPKEY.':'.JG_MASTERSECRET));
  190. //根据API的要求,定义相对应的Content-Type
  191. array_push($headers, "Content-Type".":"."application/json");
  192. $data = json_encode(["loginToken"=>$token,"exId"=>$exId]);
  193. $curl = curl_init();
  194. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  195. curl_setopt($curl, CURLOPT_URL, $host);
  196. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  197. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  198. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  199. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  200. //设定返回信息中是否包含响应信息头,启用时会将头文件的信息作为数据流输出,true 表示输出信息头, false表示不输出信息头
  201. //如果需要将字符串转成json,请将 CURLOPT_HEADER 设置成 false
  202. curl_setopt($curl, CURLOPT_HEADER, false);
  203. $ret = curl_exec($curl);
  204. $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  205. // file_put_contents(PATH_USER_JG_LOGIN_LOG.date('Y-m-d').'.log', date('Y-m-d H:i:s').$ret.PHP_EOL,FILE_APPEND);//写入文件 常量需要自己在定义
  206. $ret = json_decode($ret,true);
  207. if ($ret['code'] == 8000) {
  208. if($ret['exID']!=$exId){
  209. $ret['err'] = 'exID 返回与发送不一致';
  210. }
  211. } else {
  212. $ret['err'] = $ret['code'].':'.$ret['content'];
  213. }
  214. return $ret;
  215. // $ret = jgLoginTokenVerify($token,$exId);
  216. // $res = jgOpensslPrivateDecrypt($ret['phone']);
  217. }
  218. }