Sin descripción

PushController.php 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. <?php
  2. namespace App\Http\Controllers\Push\V1;
  3. use App\Http\Controllers\Controller;
  4. use App\Model\Push\V1\push;
  5. use FuseSource\Stomp\Stomp;
  6. use Illuminate\Http\Request;
  7. class PushController extends Controller
  8. {
  9. // db5a66d2f7fcc1da1f28c5c7a1d9a50911855559,
  10. public function miPush(Request $request)
  11. {
  12. $errors = app('check')->must('app_id|应用id')->contain([1, 2], 'client_type|机型', true)->contain([1, 2, 3, 4, 5, 6], 'type|标志类型', true)->must('title|标题')->must('content|通知描述')->contain([-1, 1, 2, 4], 'notify_type|机型')->contain([0, 1], 'asynchronous')->errors;
  13. if (!empty($errors)) {
  14. return reValue(4002, null, $errors);
  15. }
  16. $appId = $request->get('app_id');
  17. $info = Push::selectInfoByAppId($appId);
  18. if (!isset($info)) {
  19. return reValue(4002, null, ['appId有误']);
  20. }
  21. $asynchronous = $request->get('asynchronous', 1);
  22. if ($asynchronous == 1) {
  23. $all = $request->all();
  24. $all['push_type'] = 'mi';
  25. $all['asynchronous'] = 0;
  26. $all = json_encode($all);
  27. //初始化队列
  28. $connect = new Stomp(env('ACTIVEMQ_TCP'));
  29. $connect->connect();
  30. //将上传数据写入队列
  31. $result = $connect->send(config('queue.push_name'), $all, array('persistent' => 'true'));
  32. if ($result) {
  33. return reValue(0, null, ['数据人队列成功']);
  34. }
  35. return reValue(4003, null, ['入队列失败']);
  36. }
  37. //推送所用数据
  38. $sign = $request->get('sign');
  39. //客户端累次哪个1:ios2:安卓
  40. $clientType = $request->get('client_type');
  41. //推送用的方法
  42. $type = $request->get('type');
  43. //通知提示
  44. $notifyType = $request->get('notify_type', null);
  45. //回传app信息
  46. $payload = $request->get('payload', null);
  47. //标题
  48. $title = $request->get('title');
  49. //内容
  50. $content = $request->get('content');
  51. //定时时间
  52. // $timeToSend = $request->get('time_to_send', null);
  53. $timeToSend = $request->get('time_to_send')?$request->get('time_to_send'):null;
  54. $data = [];
  55. if (isset($notifyType)) {
  56. $data['notify_type'] = $notifyType;
  57. }
  58. // $appSecret = 'UYKlSebkDzgHlhhavkHC8w==';
  59. $appSecret = $info->app_secret;
  60. //判断是测试环境还是正式环境
  61. $test = $request->get('test', false);
  62. if($test){
  63. $pushUrl = config('push.test_mi_push_url');
  64. } else {
  65. $pushUrl = config('push.mi_push_url');
  66. }
  67. $headers = array('Authorization: key=' . $appSecret,
  68. 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8');
  69. switch ($type) {
  70. case 1:
  71. $data['registration_id'] = $request->get('sign');
  72. $url = $pushUrl . config('push.mi_push_regid');
  73. break;
  74. case 2:
  75. $data['alias'] = $request->get('sign');
  76. $url = $pushUrl . config('push.mi_push_alias');
  77. break;
  78. case 3:
  79. $data['user_account'] = $request->get('sign');
  80. $url = $pushUrl . config('push.mi_push_user_account');
  81. break;
  82. case 4:
  83. $url = $pushUrl . config('push.mi_push_topic');
  84. break;
  85. case 5:
  86. $url = $pushUrl . config('push.mi_push_multi_topic');
  87. case 6:
  88. $url = $pushUrl . config('push.mi_push_all');
  89. }
  90. //定时
  91. if (isset($timeToSend)) {
  92. $data['time_to_send'] = $timeToSend;
  93. }
  94. //ios和安卓数据差异化处理
  95. /*if ($clientType == 2) {
  96. $data['title'] = $title;
  97. $data['description'] = $content;
  98. if (isset($payload)) {
  99. $data['payload'] = $payload;
  100. }
  101. } else {
  102. $subtitle = $request->get('subtitle', null);
  103. if (isset($subtitle)) {
  104. $data['aps_proper_fields.subtitle'] = $subtitle;
  105. }
  106. $data['aps_proper_fields.title'] = $title;
  107. $data['aps_proper_fields.body'] = $content;
  108. $data['description'] = $content;
  109. $payload = json_decode($payload, true);
  110. if (isset($payload)) {
  111. $data['payload'] = $payload;
  112. foreach ($payload as $k => $v) {
  113. $data['extra.' . $k] = $v;
  114. }
  115. }
  116. }*/
  117. if ($clientType == 2) {
  118. $data['title'] = $title;
  119. $data['description'] = $content;
  120. if (isset($payload)) {
  121. $data['payload'] = $payload;
  122. }
  123. }elseif ($clientType == 1) {
  124. $data['title'] = $title;
  125. $data['description'] = $content;
  126. $payload = json_decode($payload, true);
  127. if (isset($payload)) {
  128. $data['payload'] = $payload;
  129. foreach ($payload as $k => $v) {
  130. $data['extra.' . $k] = $v;
  131. }
  132. }
  133. }else {
  134. $subtitle = $request->get('subtitle', null);
  135. if (isset($subtitle)) {
  136. $data['aps_proper_fields.subtitle'] = $subtitle;
  137. }
  138. $data['aps_proper_fields.title'] = $title;
  139. $data['aps_proper_fields.body'] = $content;
  140. $data['description'] = $content;
  141. $payload = json_decode($payload, true);
  142. if (isset($payload)) {
  143. $data['payload'] = $payload;
  144. foreach ($payload as $k => $v) {
  145. $data['extra.' . $k] = $v;
  146. }
  147. }
  148. }
  149. $re = curlInit($url, $data, 'Post', $headers);
  150. if ($re === false) {
  151. return reValue(4003, null, ['网络错误或超时']);
  152. }
  153. $re = json_decode($re, true);
  154. if ($re['code'] == 0) {
  155. return reValue(0, $re);
  156. }
  157. return reValue(4002, $re);
  158. }
  159. /**
  160. * [deleteScheduleJob 删除小米推送的定时任务]
  161. * @Author mzb
  162. * @DateTime 2018-11-13T14:24:32+0800
  163. * @param Request $request [description]
  164. * @return [type] [description]
  165. */
  166. public function deleteScheduleJob(Request $request)
  167. {
  168. $errors = app('check')->must('job_id|推送id', true)->errors;
  169. if (!empty($errors)) {
  170. return reValue(4002, null, $errors);
  171. }
  172. //消息id
  173. $data['job_id'] = $request->get('job_id');
  174. $appSecret = 'UYKlSebkDzgHlhhavkHC8w==';
  175. // $appSecret = 'GXdQE1z72KUoG6Rnv1cd9g==';
  176. //请求头
  177. $headers = array('Authorization: key=' . $appSecret,
  178. 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8');
  179. $url = config('push.mi_push_url') . config('push.mi_push_delete');
  180. $re = curlInit($url, $data, 'Post', $headers);
  181. if ($re === false) {
  182. return reValue(4003, null, ['网络错误或超时']);
  183. }
  184. $re = json_decode($re, true);
  185. if ($re['code'] == 0) {
  186. return reValue(0, $re);
  187. }
  188. return reValue(4002, $re);
  189. }
  190. /**
  191. * [huaweiPush description]
  192. * @Author mzb
  193. * @DateTime 2018-11-15T10:18:56+0800
  194. * @return [type] [description]
  195. */
  196. public function huaweiPush(Request $request)
  197. {
  198. $errors = app('check')->must('sign|设备标识')->contain([1, 2, 3], 'action_type|点击类型')->contain([1, 3], 'msg_type|消息类型', true)->contain([0, 1], 'asynchronous')->must('app_id|应用id')->errors;
  199. if (!empty($errors)) {
  200. return reValue(4002, null, $errors);
  201. }
  202. $appId = $request->get('app_id');
  203. $info = Push::selectInfoByAppId($appId);
  204. if (!isset($info)) {
  205. return reValue(4002, null, ['appId有误']);
  206. }
  207. $asynchronous = $request->get('asynchronous', 1);
  208. if ($asynchronous == 1) {
  209. $all = $request->all();
  210. $all['push_type'] = 'huawei';
  211. $all['asynchronous'] = 0;
  212. $all = json_encode($all);
  213. //初始化队列
  214. $connect = new Stomp(env('ACTIVEMQ_TCP'));
  215. $connect->connect();
  216. //将上传数据写入队列
  217. $result = $connect->send(config('queue.push_name'), $all, array('persistent' => 'true'));
  218. if ($result) {
  219. return reValue(0, null, ['数据人队列成功']);
  220. }
  221. return reValue(4003, null, ['入队列失败']);
  222. }
  223. $appSecret = $info->app_secret;
  224. $url = config('push.hua_wei_push_url');
  225. $postfix['ver'] = config('push.hua_wei_push_version');
  226. $postfix['appId'] = $appId;
  227. $postfix = json_encode($postfix);
  228. $postfix = urlencode($postfix);
  229. $url = $url . '?nsp_ctx=' . $postfix;
  230. $accessToken = $this->getHuaweiToken($appId, $appSecret);
  231. if ($accessToken === false) {
  232. return reValue(4002, null, ['access_token获取失败']);
  233. }
  234. //推送所用数据
  235. $sign = $request->get('sign');
  236. $sign = explode(',', $sign);
  237. $data['access_token'] = $accessToken;
  238. $data['nsp_ts'] = time();
  239. $data['nsp_svc'] = 'openpush.message.api.send';
  240. $data['device_token_list'] = json_encode($sign);
  241. // $data['expire_time'] = time() + 86400 * 7;
  242. //行为类型
  243. $actionType = $request->get('action_type', 3);
  244. //消息类型
  245. $msgType = $request->get('msg_type', 3);
  246. //biTag标签
  247. $biTag = $request->get('biTag', null);
  248. //推送icon
  249. $icon = $request->get('icon', null);
  250. //json数组
  251. $customize = $request->get('customize', null);
  252. if ($msgType == 3) {
  253. $errors = app('check')->must('title|标题')->must('content|通知描述')->errors;
  254. if (!empty($errors)) {
  255. return reValue(4002, null, $errors);
  256. }
  257. //仅通知栏消息需要设置标题和内容,透传消息key和value为用户自定义
  258. //消息标题
  259. $body['title'] = $request->get('title', "");
  260. //消息标题
  261. $body['content'] = $request->get('content', "");
  262. //类型3为打开APP,其他行为请参考接口文档设置
  263. $action['type'] = $actionType;
  264. if ($actionType == 3) {
  265. $param['appPkgName'] = $info->app_name;
  266. } else if ($actionType == 2) {
  267. $param['url'] = $request->get('url');
  268. } else {
  269. $param['intent'] = $request->get('intent');
  270. }
  271. //消息点击动作参数
  272. $action['param'] = $param;
  273. //扩展信息,含BI消息统计,特定展示风格,消息折叠。
  274. //设置消息标签,如果带了这个标签,会在回执中推送给CP用于检测某种类型消息的到达率和状态
  275. if (isset($biTag)) {
  276. $ext['biTag'] = $biTag;
  277. }
  278. //自定义推送消息在通知栏的图标,value为一个公网可以访问的URL
  279. if (isset($icon)) {
  280. $ext['icon'] = $icon;
  281. }
  282. //用户自定义
  283. if (isset($customize)) {
  284. $ext['customize'] = [json_encode(json_decode($customize))];
  285. }
  286. //华为PUSH消息总结构体
  287. if (isset($ext)) {
  288. $hps['ext'] = $ext;
  289. }
  290. //消息点击动作
  291. $msg['action'] = $action;
  292. //3: 通知栏消息,异步透传消息请根据接口文档设置
  293. $msg['type'] = $request->get('type',3);
  294. //通知栏消息body内容
  295. $msg['body'] = $body;
  296. } else {
  297. $errors = app('check')->must('body|消息体')->errors;
  298. if (!empty($errors)) {
  299. return reValue(4002, null, $errors);
  300. }
  301. $msg['type'] = 1;
  302. //通知栏消息body内容
  303. $msg['body'] = $request->get('body', "");
  304. }
  305. $hps['msg'] = $msg;
  306. $payload['hps'] = $hps;
  307. $payload = json_encode($payload);
  308. $data['payload'] = $payload;
  309. // dd($data['payload']);
  310. $headers = array(
  311. 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8');
  312. $re = curlInit($url, $data, 'Post', $headers);
  313. $re = json_decode($re, true);
  314. if (!isset($re['code'])) {
  315. return reValue(4003);
  316. }
  317. if ($re['code'] == 80000000) {
  318. return reValue(0, $re);
  319. }
  320. return reValue(4003, $re);
  321. }
  322. /**
  323. * [getHuaweiToken 华为获取token]
  324. * @Author mzb
  325. * @DateTime 2018-11-15T13:59:12+0800
  326. * @param [type] $id [description]
  327. * @param [type] $appSecret [description]
  328. * @return [type] [description]
  329. */
  330. public function getHuaweiToken($appId, $appSecret)
  331. {
  332. // print_r($appId);
  333. // print_r($appSecret);exit;
  334. //api的url
  335. $url = config('push.hua_wei_token_url');
  336. $data['grant_type'] = 'client_credentials';
  337. $data['client_id'] = $appId;
  338. // $data['client_id'] = '100228763';
  339. $data['client_secret'] = $appSecret;
  340. // $data['client_secret'] = '6170edadd53e02e02139d5b532c84cd7';
  341. $headers = array(
  342. 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8');
  343. $re = curlInit($url, $data, 'Post', $headers);
  344. $re = json_decode($re, true);
  345. if (isset($re['access_token'])) {
  346. return $re['access_token'];
  347. }
  348. return false;
  349. }
  350. /**
  351. * [oppoPush oppo推送]
  352. * @Author mzb
  353. * @DateTime 2018-11-16T16:35:50+0800
  354. * @param Request $request [description]
  355. * @return [type] [description]
  356. */
  357. public function oppoPush(Request $request)
  358. {
  359. // ->must('sign|设备标识')
  360. $errors = app('check')->must('title|标题')->must('content|推送内容')->must('sub_title|子标题')->must('app_id|应用id')->contain([0, 1, 2, 4, 5], 'click_action_type|点击类型')->contain([0, 1], 'show_time_type|展示类型')->contain([0, 1, 2, 4, 5], 'click_action_activity|点击类型')->json('action_parameters|动作参数')->contain([false, true], 'off_line|是否离线')->NonNInt('off_line_ttl|存活时间')->contain([0, 1], 'push_time_type|定时')->NonNInt('push_start_time|定时时间')->NonNInt('show_start_time|展示开始时间')->NonNInt('push_end_time|展示结束时间')->contain([false, true], 'fix_speed|是否是否定速推')->NonNInt('fix_speed_rate|定速速率')->contain([1, 0], 'network_type|限网络情况')->contain([1, 2, 3, 4], 'target_type|目标类型', true)->contain([0, 1], 'asynchronous')->errors;
  361. if (!empty($errors)) {
  362. return reValue(4002, null, $errors);
  363. }
  364. $appId = $request->get('app_id');
  365. $info = Push::selectInfoByAppId($appId);
  366. if (!isset($info)) {
  367. return reValue(4002, null, ['appId有误']);
  368. }
  369. $asynchronous = $request->get('asynchronous', 1);
  370. if ($asynchronous == 1) {
  371. $all = $request->all();
  372. $all['push_type'] = 'oppo';
  373. $all['asynchronous'] = 0;
  374. $all = json_encode($all);
  375. //初始化队列
  376. $connect = new Stomp(env('ACTIVEMQ_TCP'));
  377. $connect->connect();
  378. //将上传数据写入队列
  379. $result = $connect->send(config('queue.push_name'), $all, array('persistent' => 'true'));
  380. if ($result) {
  381. return reValue(0, null, ['数据人队列成功']);
  382. }
  383. return reValue(4003, null, ['入队列失败']);
  384. }
  385. $appSecret = $info->app_secret;
  386. $appExt = $info->app_ext;
  387. $appKey = $info->app_key;
  388. $re = $this->oppoGetAuth($appKey, $appExt);
  389. if ($re === false) {
  390. return reValue(4002, null, ['网络请求超时']);
  391. }
  392. if ($re['code'] != 0) {
  393. return reValue(4002, $re, ['网络请求超时']);
  394. }
  395. if (!isset($re['data']['auth_token'])) {
  396. return reValue(4004, $re, ['未知错误']);
  397. }
  398. $token = $re['data']['auth_token'];
  399. //推送类型
  400. $push['target_type'] = $request->get('target_type');
  401. //判断设备标识是否存在
  402. if ($push['target_type'] != 1) {
  403. $errors = app('check')->must('sign|设备标识')->errors;
  404. if (!empty($errors)) {
  405. return reValue(4002, null, $errors);
  406. }
  407. if ($push['target_type'] == 2) {
  408. $push['target_value'] = $request->get('sign');
  409. } else {
  410. $data['target_value'] = $request->get('sign');
  411. }
  412. }
  413. //设置头
  414. $headers = array(
  415. 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8');
  416. if ($push['target_type'] == 1 || $push['target_type'] == 2) {
  417. $url = config('push.oppo_push_url') . config('push.save_message_broadcast');
  418. $messageId = $request->get('message_id');
  419. if (empty($messageId)) {
  420. //获取消息体数据
  421. $data = $this->oppoFormatData($request);
  422. //消息id
  423. $data['app_message_id'] = $appId . time() . randomStr();
  424. //添加token
  425. $data['auth_token'] = $token;
  426. //请求oppo保存消息体
  427. $saveMessageUrl = config('push.oppo_push_url') . config('push.save_message_content');
  428. $re = curlInit($saveMessageUrl, $data, 'Post', $headers);
  429. $re = json_decode($re, true);
  430. if ($re == false) {
  431. return reValue(4002, null, ['网络请求超时']);
  432. }
  433. if ($re['code'] != 0) {
  434. return reValue(4003, $re);
  435. }
  436. $push['message_id'] = $re['data']['message_id'];
  437. } else {
  438. $push['message_id'] = $messageId;
  439. }
  440. } else if ($push['target_type'] == 3) {
  441. $url = config('push.oppo_push_url') . config('push.save_message_unicast');
  442. //获取消息体数据
  443. $add = $this->oppoFormatData($request);
  444. //消息id
  445. $data['app_message_id'] = $appId . time() . randomStr();
  446. $data['target_type'] = 2;
  447. $data['notification'] = $add;
  448. $push['message'] = json_encode($data);
  449. $headers = array_merge($headers, ['auth_token:' . $token]);
  450. } else {
  451. $message = $request->get('message');
  452. if (empty($message)) {
  453. return reValue(4002, null, ['单点多推时必有message参数']);
  454. }
  455. $url = config('push.oppo_push_url') . config('push.save_message_unicast_batch');
  456. $push['target_type'] = 2;
  457. $push['message'] = $message;
  458. }
  459. $push['auth_token'] = $token;
  460. $push['channel_id'] = 1001;
  461. // print_r($token);exit;
  462. // print_r($push);exit;
  463. $pushRe = curlInit($url, $push, 'Post', $headers);
  464. $pushRe = json_decode($pushRe, true);
  465. if ($pushRe == false) {
  466. return reValue(4002, null, ['网络请求超时']);
  467. }
  468. if ($pushRe['code'] != 0) {
  469. return reValue(4003, $pushRe);
  470. }
  471. return reValue(0, $pushRe);
  472. }
  473. /**
  474. * [oppoFormatData oppo格式化数据]
  475. * @Author mzb
  476. * @DateTime 2018-11-16T16:47:26+0800
  477. * @param Request $request [description]
  478. * @return [type] [description]
  479. */
  480. public function oppoFormatData(Request $request)
  481. {
  482. //推送标题
  483. $data['title'] = $request->get('title');
  484. //推送子标题
  485. $data['sub_title'] = $request->get('sub_title');
  486. //推送内容
  487. $data['content'] = $request->get('content');
  488. //推送点击类型
  489. $data['click_action_type'] = $request->get('click_action_type', 0);
  490. if ($data['click_action_type'] == 1 || $data['click_action_type'] == 4) {
  491. $data['click_action_activity'] = $request->get('click_action_activity');
  492. }
  493. if ($data['click_action_type'] == 2) {
  494. $data['click_action_url'] = $request->get('click_action_url');
  495. }
  496. //获取动作参数
  497. $actionParameters = $request->get('action_parameters');
  498. if (!empty($actionParameters)) {
  499. $data['action_parameters'] = $actionParameters;
  500. }
  501. //展示类型
  502. $data['show_time_type'] = $request->get('show_time_type', 0);
  503. if ($data['show_time_type'] == 1) {
  504. //定时开始时间
  505. $data['show_start_time'] = $request->get('show_start_time', 0);
  506. //定时结束时间
  507. $data['show_end_time'] = $request->get('show_end_time', 0);
  508. }
  509. //是否离线
  510. $data['off_line'] = $request->get('off_line', true);
  511. //消息存活时间
  512. $data['off_line_ttl'] = $request->get('off_line_ttl', 86400 * 3);
  513. //定时
  514. $data['push_time_type'] = $request->get('push_time_type', 0);
  515. if ($data['push_time_type']) {
  516. //定时开始时间
  517. $data['push_start_time'] = $request->get('push_start_time', 0);
  518. }
  519. $timeZone = $request->get('time_zone');
  520. if (!empty($timeZone)) {
  521. //设置市区
  522. $data['time_zone'] = $request->get('time_zone');
  523. }
  524. //是否定速推
  525. $data['fix_speed'] = $request->get('fix_speed', false);
  526. //定速速率
  527. $data['fix_speed_rate'] = $request->get('fix_speed_rate', 0);
  528. //网络限制情况
  529. $data['network_type'] = $request->get('network_type', 0);
  530. //回执url
  531. $callBackUrl = $request->get('call_back_url');
  532. if (!empty($callBackUrl)) {
  533. $data['call_back_url'] = $callBackUrl;
  534. }
  535. //回执参数
  536. $callBackParamete = $request->get('call_back_parameter');
  537. if (!empty($callBackParamete)) {
  538. $data['call_back_parameter'] = $callBackParamete;
  539. }
  540. return $data;
  541. }
  542. /**
  543. * [oppoGetAuth oppo获取去权限token]
  544. * @Author mzb
  545. * @DateTime 2018-11-15T18:30:40+0800
  546. * @return [type] [description]
  547. */
  548. public function oppoGetAuth($appKey, $masterSecret)
  549. {
  550. $url = config('push.oppo_push_url') . config('push.oppo_get_auth');
  551. // $appKey = '9S93BT1f4zk00gw8848sWC88';
  552. // $masterSecret = "8A9c7dcbe536a8570BE44Dc5e715EF0B";
  553. $time = getMillisecond();
  554. $data['app_key'] = $appKey;
  555. $str = $appKey . $time . $masterSecret;
  556. $data['sign'] = hash('sha256', $str);
  557. $data['timestamp'] = $time;
  558. $headers = array(
  559. 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8');
  560. $re = curlInit($url, $data, 'Post', $headers);
  561. if ($re === false) {
  562. return $re;
  563. }
  564. $re = json_decode($re, true);
  565. // if (isset($re['data']['auth_token'])) {
  566. // return $re['data']['auth_token'];
  567. // }
  568. return $re;
  569. }
  570. /**
  571. * [vivoGetAuth vivo获取去权限token]
  572. * @Author mzb
  573. * @DateTime 2018-11-16T10:32:23+0800
  574. * @return [type] [description]
  575. */
  576. public function vivoGetAuth($appId, $appKey, $appSecret)
  577. {
  578. $url = config('push.vivo_push_url') . config('push.vivo_get_auth');
  579. $time = getMillisecond();
  580. $data['appKey'] = $appKey;
  581. $data['timestamp'] = $time;
  582. $str = $appId.$appKey.$time.$appSecret;
  583. $data['sign'] = md5($str);
  584. $data['appId'] = $appId;
  585. dd(json_encode($data));
  586. $headers = array(
  587. 'Content-Type:application/json;charset=UTF-8');
  588. $re = curlInit($url, $data, 'Post', $headers);
  589. if ($re === false) {
  590. return $re;
  591. }
  592. $re = json_decode($re, true);
  593. // if (isset($re['data']['auth_token'])) {
  594. // return $re['data']['auth_token'];
  595. // }
  596. return $re;
  597. }
  598. public function vivoPush(Request $request)
  599. {
  600. // $errors = app('check')->must('sign|设备标识')->contain([1, 2, 3], 'action_type|点击类型')->contain([1, 3], 'msg_type|消息类型', true)->contain([0, 1], 'asynchronous')->must('app_id|应用id')->errors;
  601. // if (!empty($errors)) {
  602. // return reValue(4002, null, $errors);
  603. // }
  604. $appId = $request->get('app_id');
  605. $info = Push::selectInfoByAppId($appId);
  606. if (!isset($info)) {
  607. return reValue(4002, null, ['appId有误']);
  608. }
  609. // $asynchronous = $request->get('asynchronous', 1);
  610. // if ($asynchronous == 1) {
  611. // $all = $request->all();
  612. // $all['push_type'] = 'huawei';
  613. // $all['asynchronous'] = 0;
  614. // $all = json_encode($all);
  615. // //初始化队列
  616. // $connect = new Stomp(env('ACTIVEMQ_TCP'));
  617. // $connect->connect();
  618. // //将上传数据写入队列
  619. // $result = $connect->send(config('queue.push_name'), $all, array('persistent' => 'true'));
  620. // if ($result) {
  621. // return reValue(0, null, ['数据人队列成功']);
  622. // }
  623. // return reValue(4003, null, ['入队列失败']);
  624. // }
  625. $appSecret = $info->app_secret;
  626. $appKey = $info->app_key;
  627. $url = config('push.vivo_push_url');
  628. $re = $this->vivoGetAuth($appId, $appKey ,$appSecret);
  629. print_r($re);die;
  630. if ($re === false) {
  631. return reValue(4002, null, ['网络请求超时']);
  632. }
  633. if ($re['code'] != 0) {
  634. return reValue(4002, $re, ['网络请求超时']);
  635. }
  636. if (!isset($re['data']['auth_token'])) {
  637. return reValue(4004, $re, ['未知错误']);
  638. }
  639. }
  640. }