JPushService.php 7.6 KB

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