JPushService.php 7.8 KB

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