JPushService.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. 'large_icon' => 'https://zhengda.oss-accelerate.aliyuncs.com/chengluApp/image_cropper_1628595761196_compressed122286944406054090.jpg',
  77. ])->iosNotification($content, [
  78. 'sound' => 'sound',
  79. 'badge' => '+1',
  80. 'extras' => $extras
  81. ])->options([
  82. 'apns_production' => config('jpush.apns_production', true),
  83. //表示离线消息保留时长(秒)
  84. 'time_to_live' => 86400,
  85. ]);
  86. $response = $push->send();
  87. if ($response['http_code'] != 200) {
  88. Log::channel('jpush')->error(json_encode($response, JSON_UNESCAPED_UNICODE));
  89. }
  90. return $response;
  91. } catch (\Throwable $e) {
  92. Log::channel('jpush')->error(json_encode([
  93. 'file' => $e->getFile(),
  94. 'line' => $e->getLine(),
  95. 'message' => $e->getMessage(),
  96. 'params' => $params,
  97. ], JSON_UNESCAPED_UNICODE));
  98. }
  99. }
  100. /**
  101. * 获取指定设备的别名和标签
  102. */
  103. public static function getDevices($reg_id)
  104. {
  105. $response = self::getInstance()->device()->getDevices($reg_id);
  106. if ($response['http_code'] == 200) {
  107. return $response['body'];
  108. }
  109. return [];
  110. }
  111. /**
  112. * 给指定设备添加标签
  113. */
  114. public static function addTags($reg_id, $tags = [])
  115. {
  116. $response = self::getInstance()->device()->addTags($reg_id, $tags);
  117. if ($response['http_code'] == 200) {
  118. return true;
  119. }
  120. return false;
  121. }
  122. /**
  123. * 清空指定设备的标签
  124. */
  125. public static function clearTags($reg_id)
  126. {
  127. $response = self::getInstance()->device()->clearTags($reg_id);
  128. if ($response['http_code'] == 200) {
  129. return true;
  130. }
  131. return false;
  132. }
  133. /**
  134. * 清空指定设备的标签
  135. */
  136. public static function removeTags($reg_id, $tags = [])
  137. {
  138. $response = self::getInstance()->device()->removeTags($reg_id, $tags);
  139. if ($response['http_code'] == 200) {
  140. return true;
  141. }
  142. return false;
  143. }
  144. /**
  145. * 更新指定设备的别名
  146. */
  147. public static function updateAlias($reg_id, $alias)
  148. {
  149. $response = self::getInstance()->device()->updateAlias($reg_id, $alias);
  150. if ($response['http_code'] == 200) {
  151. return true;
  152. }
  153. return false;
  154. }
  155. /**
  156. * jgOpensslPrivateDecrypt($encrypted) (加密) 手机号码解密获取手机号
  157. * @param $encrypted string (加密) 手机号码
  158. * @param
  159. *
  160. * @return string 手机号|false
  161. * err:错误信息
  162. *
  163. */
  164. public static function jgOpensslPrivateDecrypt($encrypted){
  165. $prefix = '-----BEGIN RSA PRIVATE KEY-----';
  166. $suffix = '-----END RSA PRIVATE KEY-----';
  167. $result = '';
  168. $prikey = config('jpush.jg_yjdl_private_rsa');
  169. $key = $prefix . "\n" . $prikey . "\n" . $suffix;//拼接换行符
  170. $r = openssl_private_decrypt(base64_decode($encrypted), $result, openssl_pkey_get_private($key));
  171. if($r){
  172. return $result;
  173. }else{
  174. return false;
  175. throw new Exception("解密失败");
  176. }
  177. }
  178. /**
  179. * jgLoginTokenVerify($token) 提交loginToken,验证后返回手机号码(加密)
  180. * @param $token string 认证SDK获取到的loginToken
  181. * @param $exId 开发者自定义的id,非必填
  182. *
  183. * @return array 手机号
  184. * err:错误信息
  185. *
  186. */
  187. public static function jgLoginTokenVerify($token,$exId)
  188. {
  189. $host = self::JG_YJDL_POST_URL;
  190. $method = "POST";
  191. // $appcode = VERIFY_APPCODE;
  192. $headers = array();
  193. array_push($headers, "Authorization: Basic " .base64_encode(config('jpush.app_key').':'.config('jpush.master_secret')));
  194. //根据API的要求,定义相对应的Content-Type
  195. array_push($headers, "Content-Type".":"."application/json");
  196. $data = json_encode(["loginToken"=>$token,"exId"=>$exId]);
  197. $curl = curl_init();
  198. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  199. curl_setopt($curl, CURLOPT_URL, $host);
  200. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  201. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  202. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  203. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  204. //设定返回信息中是否包含响应信息头,启用时会将头文件的信息作为数据流输出,true 表示输出信息头, false表示不输出信息头
  205. //如果需要将字符串转成json,请将 CURLOPT_HEADER 设置成 false
  206. curl_setopt($curl, CURLOPT_HEADER, false);
  207. $ret = curl_exec($curl);
  208. $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  209. // 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);//写入文件 常量需要自己在定义
  210. $ret = json_decode($ret,true);
  211. if ($ret['code'] == 8000) {
  212. // if($ret['exID']!=$exId){
  213. // throw new Exception("exID 返回与发送不一致");
  214. // }
  215. } else {
  216. throw new Exception($ret['code'].":请重试");
  217. throw new Exception($ret['code'].':'.$ret['content']);
  218. }
  219. return $ret;
  220. }
  221. }