JPushService.php 8.2 KB

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