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; if (!empty($errors)) { return reValue(4002, null, $errors); } $appId = $request->get('app_id'); $info = Push::selectInfoByAppId($appId); if (!isset($info)) { return reValue(4002, null, ['appId有误']); } $asynchronous = $request->get('asynchronous', 1); if ($asynchronous == 1) { $all = $request->all(); $all['push_type'] = 'mi'; $all['asynchronous'] = 0; $all = json_encode($all); //初始化队列 $connect = new Stomp(env('ACTIVEMQ_TCP')); $connect->connect(); //将上传数据写入队列 $result = $connect->send(config('queue.push_name'), $all, array('persistent' => 'true')); if ($result) { return reValue(0, null, ['数据人队列成功']); } return reValue(4003, null, ['入队列失败']); } //推送所用数据 $sign = $request->get('sign'); //客户端累次哪个1:ios2:安卓 $clientType = $request->get('client_type'); //推送用的方法 $type = $request->get('type'); //通知提示 $notifyType = $request->get('notify_type', null); //回传app信息 $payload = $request->get('payload', null); //标题 $title = $request->get('title'); //内容 $content = $request->get('content'); //定时时间 // $timeToSend = $request->get('time_to_send', null); $timeToSend = $request->get('time_to_send')?$request->get('time_to_send'):null; $data = []; if (isset($notifyType)) { $data['notify_type'] = $notifyType; } // $appSecret = 'UYKlSebkDzgHlhhavkHC8w=='; $appSecret = $info->app_secret; //判断是测试环境还是正式环境 $test = $request->get('test', false); if($test){ $pushUrl = config('push.test_mi_push_url'); } else { $pushUrl = config('push.mi_push_url'); } $headers = array('Authorization: key=' . $appSecret, 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'); switch ($type) { case 1: $data['registration_id'] = $request->get('sign'); $url = $pushUrl . config('push.mi_push_regid'); break; case 2: $data['alias'] = $request->get('sign'); $url = $pushUrl . config('push.mi_push_alias'); break; case 3: $data['user_account'] = $request->get('sign'); $url = $pushUrl . config('push.mi_push_user_account'); break; case 4: $url = $pushUrl . config('push.mi_push_topic'); break; case 5: $url = $pushUrl . config('push.mi_push_multi_topic'); case 6: $url = $pushUrl . config('push.mi_push_all'); } //定时 if (isset($timeToSend)) { $data['time_to_send'] = $timeToSend; } //ios和安卓数据差异化处理 /*if ($clientType == 2) { $data['title'] = $title; $data['description'] = $content; if (isset($payload)) { $data['payload'] = $payload; } } else { $subtitle = $request->get('subtitle', null); if (isset($subtitle)) { $data['aps_proper_fields.subtitle'] = $subtitle; } $data['aps_proper_fields.title'] = $title; $data['aps_proper_fields.body'] = $content; $data['description'] = $content; $payload = json_decode($payload, true); if (isset($payload)) { $data['payload'] = $payload; foreach ($payload as $k => $v) { $data['extra.' . $k] = $v; } } }*/ if ($clientType == 2) { $data['title'] = $title; $data['description'] = $content; if (isset($payload)) { $data['payload'] = $payload; } }elseif ($clientType == 1) { $data['title'] = $title; $data['description'] = $content; $payload = json_decode($payload, true); if (isset($payload)) { $data['payload'] = $payload; foreach ($payload as $k => $v) { $data['extra.' . $k] = $v; } } }else { $subtitle = $request->get('subtitle', null); if (isset($subtitle)) { $data['aps_proper_fields.subtitle'] = $subtitle; } $data['aps_proper_fields.title'] = $title; $data['aps_proper_fields.body'] = $content; $data['description'] = $content; $payload = json_decode($payload, true); if (isset($payload)) { $data['payload'] = $payload; foreach ($payload as $k => $v) { $data['extra.' . $k] = $v; } } } $re = curlInit($url, $data, 'Post', $headers); if ($re === false) { return reValue(4003, null, ['网络错误或超时']); } $re = json_decode($re, true); if ($re['code'] == 0) { return reValue(0, $re); } return reValue(4002, $re); } /** * [deleteScheduleJob 删除小米推送的定时任务] * @Author mzb * @DateTime 2018-11-13T14:24:32+0800 * @param Request $request [description] * @return [type] [description] */ public function deleteScheduleJob(Request $request) { $errors = app('check')->must('job_id|推送id', true)->errors; if (!empty($errors)) { return reValue(4002, null, $errors); } //消息id $data['job_id'] = $request->get('job_id'); $appSecret = 'UYKlSebkDzgHlhhavkHC8w=='; // $appSecret = 'GXdQE1z72KUoG6Rnv1cd9g=='; //请求头 $headers = array('Authorization: key=' . $appSecret, 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'); $url = config('push.mi_push_url') . config('push.mi_push_delete'); $re = curlInit($url, $data, 'Post', $headers); if ($re === false) { return reValue(4003, null, ['网络错误或超时']); } $re = json_decode($re, true); if ($re['code'] == 0) { return reValue(0, $re); } return reValue(4002, $re); } /** * [huaweiPush description] * @Author mzb * @DateTime 2018-11-15T10:18:56+0800 * @return [type] [description] */ public function huaweiPush(Request $request) { $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; if (!empty($errors)) { return reValue(4002, null, $errors); } $appId = $request->get('app_id'); $info = Push::selectInfoByAppId($appId); if (!isset($info)) { return reValue(4002, null, ['appId有误']); } $asynchronous = $request->get('asynchronous', 1); if ($asynchronous == 1) { $all = $request->all(); $all['push_type'] = 'huawei'; $all['asynchronous'] = 0; $all = json_encode($all); //初始化队列 $connect = new Stomp(env('ACTIVEMQ_TCP')); $connect->connect(); //将上传数据写入队列 $result = $connect->send(config('queue.push_name'), $all, array('persistent' => 'true')); if ($result) { return reValue(0, null, ['数据人队列成功']); } return reValue(4003, null, ['入队列失败']); } $appSecret = $info->app_secret; $url = config('push.hua_wei_push_url'); $postfix['ver'] = config('push.hua_wei_push_version'); $postfix['appId'] = $appId; $postfix = json_encode($postfix); $postfix = urlencode($postfix); $url = $url . '?nsp_ctx=' . $postfix; $accessToken = $this->getHuaweiToken($appId, $appSecret); if ($accessToken === false) { return reValue(4002, null, ['access_token获取失败']); } //推送所用数据 $sign = $request->get('sign'); $sign = explode(',', $sign); $data['access_token'] = $accessToken; $data['nsp_ts'] = time(); $data['nsp_svc'] = 'openpush.message.api.send'; $data['device_token_list'] = json_encode($sign); // $data['expire_time'] = time() + 86400 * 7; //行为类型 $actionType = $request->get('action_type', 3); //消息类型 $msgType = $request->get('msg_type', 3); //biTag标签 $biTag = $request->get('biTag', null); //推送icon $icon = $request->get('icon', null); //json数组 $customize = $request->get('customize', null); if ($msgType == 3) { $errors = app('check')->must('title|标题')->must('content|通知描述')->errors; if (!empty($errors)) { return reValue(4002, null, $errors); } //仅通知栏消息需要设置标题和内容,透传消息key和value为用户自定义 //消息标题 $body['title'] = $request->get('title', ""); //消息标题 $body['content'] = $request->get('content', ""); //类型3为打开APP,其他行为请参考接口文档设置 $action['type'] = $actionType; if ($actionType == 3) { $param['appPkgName'] = $info->app_name; } else if ($actionType == 2) { $param['url'] = $request->get('url'); } else { $param['intent'] = $request->get('intent'); } //消息点击动作参数 $action['param'] = $param; //扩展信息,含BI消息统计,特定展示风格,消息折叠。 //设置消息标签,如果带了这个标签,会在回执中推送给CP用于检测某种类型消息的到达率和状态 if (isset($biTag)) { $ext['biTag'] = $biTag; } //自定义推送消息在通知栏的图标,value为一个公网可以访问的URL if (isset($icon)) { $ext['icon'] = $icon; } //用户自定义 if (isset($customize)) { $ext['customize'] = [json_encode(json_decode($customize))]; } //华为PUSH消息总结构体 if (isset($ext)) { $hps['ext'] = $ext; } //消息点击动作 $msg['action'] = $action; //3: 通知栏消息,异步透传消息请根据接口文档设置 $msg['type'] = $request->get('type',3); //通知栏消息body内容 $msg['body'] = $body; } else { $errors = app('check')->must('body|消息体')->errors; if (!empty($errors)) { return reValue(4002, null, $errors); } $msg['type'] = 1; //通知栏消息body内容 $msg['body'] = $request->get('body', ""); } $hps['msg'] = $msg; $payload['hps'] = $hps; $payload = json_encode($payload); $data['payload'] = $payload; // dd($data['payload']); $headers = array( 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'); $re = curlInit($url, $data, 'Post', $headers); $re = json_decode($re, true); if (!isset($re['code'])) { return reValue(4003); } if ($re['code'] == 80000000) { return reValue(0, $re); } return reValue(4003, $re); } /** * [getHuaweiToken 华为获取token] * @Author mzb * @DateTime 2018-11-15T13:59:12+0800 * @param [type] $id [description] * @param [type] $appSecret [description] * @return [type] [description] */ public function getHuaweiToken($appId, $appSecret) { // print_r($appId); // print_r($appSecret);exit; //api的url $url = config('push.hua_wei_token_url'); $data['grant_type'] = 'client_credentials'; $data['client_id'] = $appId; // $data['client_id'] = '100228763'; $data['client_secret'] = $appSecret; // $data['client_secret'] = '6170edadd53e02e02139d5b532c84cd7'; $headers = array( 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'); $re = curlInit($url, $data, 'Post', $headers); $re = json_decode($re, true); if (isset($re['access_token'])) { return $re['access_token']; } return false; } /** * [oppoPush oppo推送] * @Author mzb * @DateTime 2018-11-16T16:35:50+0800 * @param Request $request [description] * @return [type] [description] */ public function oppoPush(Request $request) { // ->must('sign|设备标识') $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; if (!empty($errors)) { return reValue(4002, null, $errors); } $appId = $request->get('app_id'); $info = Push::selectInfoByAppId($appId); if (!isset($info)) { return reValue(4002, null, ['appId有误']); } $asynchronous = $request->get('asynchronous', 1); if ($asynchronous == 1) { $all = $request->all(); $all['push_type'] = 'oppo'; $all['asynchronous'] = 0; $all = json_encode($all); //初始化队列 $connect = new Stomp(env('ACTIVEMQ_TCP')); $connect->connect(); //将上传数据写入队列 $result = $connect->send(config('queue.push_name'), $all, array('persistent' => 'true')); if ($result) { return reValue(0, null, ['数据人队列成功']); } return reValue(4003, null, ['入队列失败']); } $appSecret = $info->app_secret; $appExt = $info->app_ext; $appKey = $info->app_key; $re = $this->oppoGetAuth($appKey, $appExt); if ($re === false) { return reValue(4002, null, ['网络请求超时']); } if ($re['code'] != 0) { return reValue(4002, $re, ['网络请求超时']); } if (!isset($re['data']['auth_token'])) { return reValue(4004, $re, ['未知错误']); } $token = $re['data']['auth_token']; //推送类型 $push['target_type'] = $request->get('target_type'); //判断设备标识是否存在 if ($push['target_type'] != 1) { $errors = app('check')->must('sign|设备标识')->errors; if (!empty($errors)) { return reValue(4002, null, $errors); } if ($push['target_type'] == 2) { $push['target_value'] = $request->get('sign'); } else { $data['target_value'] = $request->get('sign'); } } //设置头 $headers = array( 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'); if ($push['target_type'] == 1 || $push['target_type'] == 2) { $url = config('push.oppo_push_url') . config('push.save_message_broadcast'); $messageId = $request->get('message_id'); if (empty($messageId)) { //获取消息体数据 $data = $this->oppoFormatData($request); //消息id $data['app_message_id'] = $appId . time() . randomStr(); //添加token $data['auth_token'] = $token; //请求oppo保存消息体 $saveMessageUrl = config('push.oppo_push_url') . config('push.save_message_content'); $re = curlInit($saveMessageUrl, $data, 'Post', $headers); $re = json_decode($re, true); if ($re == false) { return reValue(4002, null, ['网络请求超时']); } if ($re['code'] != 0) { return reValue(4003, $re); } $push['message_id'] = $re['data']['message_id']; } else { $push['message_id'] = $messageId; } } else if ($push['target_type'] == 3) { $url = config('push.oppo_push_url') . config('push.save_message_unicast'); //获取消息体数据 $add = $this->oppoFormatData($request); //消息id $data['app_message_id'] = $appId . time() . randomStr(); $data['target_type'] = 2; $data['notification'] = $add; $push['message'] = json_encode($data); $headers = array_merge($headers, ['auth_token:' . $token]); } else { $message = $request->get('message'); if (empty($message)) { return reValue(4002, null, ['单点多推时必有message参数']); } $url = config('push.oppo_push_url') . config('push.save_message_unicast_batch'); $push['target_type'] = 2; $push['message'] = $message; } $push['auth_token'] = $token; $push['channel_id'] = 1001; // print_r($token);exit; // print_r($push);exit; $pushRe = curlInit($url, $push, 'Post', $headers); $pushRe = json_decode($pushRe, true); if ($pushRe == false) { return reValue(4002, null, ['网络请求超时']); } if ($pushRe['code'] != 0) { return reValue(4003, $pushRe); } return reValue(0, $pushRe); } /** * [oppoFormatData oppo格式化数据] * @Author mzb * @DateTime 2018-11-16T16:47:26+0800 * @param Request $request [description] * @return [type] [description] */ public function oppoFormatData(Request $request) { //推送标题 $data['title'] = $request->get('title'); //推送子标题 $data['sub_title'] = $request->get('sub_title'); //推送内容 $data['content'] = $request->get('content'); //推送点击类型 $data['click_action_type'] = $request->get('click_action_type', 0); if ($data['click_action_type'] == 1 || $data['click_action_type'] == 4) { $data['click_action_activity'] = $request->get('click_action_activity'); } if ($data['click_action_type'] == 2) { $data['click_action_url'] = $request->get('click_action_url'); } //获取动作参数 $actionParameters = $request->get('action_parameters'); if (!empty($actionParameters)) { $data['action_parameters'] = $actionParameters; } //展示类型 $data['show_time_type'] = $request->get('show_time_type', 0); if ($data['show_time_type'] == 1) { //定时开始时间 $data['show_start_time'] = $request->get('show_start_time', 0); //定时结束时间 $data['show_end_time'] = $request->get('show_end_time', 0); } //是否离线 $data['off_line'] = $request->get('off_line', true); //消息存活时间 $data['off_line_ttl'] = $request->get('off_line_ttl', 86400 * 3); //定时 $data['push_time_type'] = $request->get('push_time_type', 0); if ($data['push_time_type']) { //定时开始时间 $data['push_start_time'] = $request->get('push_start_time', 0); } $timeZone = $request->get('time_zone'); if (!empty($timeZone)) { //设置市区 $data['time_zone'] = $request->get('time_zone'); } //是否定速推 $data['fix_speed'] = $request->get('fix_speed', false); //定速速率 $data['fix_speed_rate'] = $request->get('fix_speed_rate', 0); //网络限制情况 $data['network_type'] = $request->get('network_type', 0); //回执url $callBackUrl = $request->get('call_back_url'); if (!empty($callBackUrl)) { $data['call_back_url'] = $callBackUrl; } //回执参数 $callBackParamete = $request->get('call_back_parameter'); if (!empty($callBackParamete)) { $data['call_back_parameter'] = $callBackParamete; } return $data; } /** * [oppoGetAuth oppo获取去权限token] * @Author mzb * @DateTime 2018-11-15T18:30:40+0800 * @return [type] [description] */ public function oppoGetAuth($appKey, $masterSecret) { $url = config('push.oppo_push_url') . config('push.oppo_get_auth'); // $appKey = '9S93BT1f4zk00gw8848sWC88'; // $masterSecret = "8A9c7dcbe536a8570BE44Dc5e715EF0B"; $time = getMillisecond(); $data['app_key'] = $appKey; $str = $appKey . $time . $masterSecret; $data['sign'] = hash('sha256', $str); $data['timestamp'] = $time; $headers = array( 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'); $re = curlInit($url, $data, 'Post', $headers); if ($re === false) { return $re; } $re = json_decode($re, true); // if (isset($re['data']['auth_token'])) { // return $re['data']['auth_token']; // } return $re; } /** * [vivoGetAuth vivo获取去权限token] * @Author mzb * @DateTime 2018-11-16T10:32:23+0800 * @return [type] [description] */ public function vivoGetAuth($appId, $appKey, $appSecret) { $url = config('push.vivo_push_url') . config('push.vivo_get_auth'); $time = getMillisecond(); $data['appKey'] = $appKey; $data['timestamp'] = $time; $str = $appId.$appKey.$time.$appSecret; $data['sign'] = md5($str); $data['appId'] = $appId; dd(json_encode($data)); $headers = array( 'Content-Type:application/json;charset=UTF-8'); $re = curlInit($url, $data, 'Post', $headers); if ($re === false) { return $re; } $re = json_decode($re, true); // if (isset($re['data']['auth_token'])) { // return $re['data']['auth_token']; // } return $re; } public function vivoPush(Request $request) { // $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; // if (!empty($errors)) { // return reValue(4002, null, $errors); // } $appId = $request->get('app_id'); $info = Push::selectInfoByAppId($appId); if (!isset($info)) { return reValue(4002, null, ['appId有误']); } // $asynchronous = $request->get('asynchronous', 1); // if ($asynchronous == 1) { // $all = $request->all(); // $all['push_type'] = 'huawei'; // $all['asynchronous'] = 0; // $all = json_encode($all); // //初始化队列 // $connect = new Stomp(env('ACTIVEMQ_TCP')); // $connect->connect(); // //将上传数据写入队列 // $result = $connect->send(config('queue.push_name'), $all, array('persistent' => 'true')); // if ($result) { // return reValue(0, null, ['数据人队列成功']); // } // return reValue(4003, null, ['入队列失败']); // } $appSecret = $info->app_secret; $appKey = $info->app_key; $url = config('push.vivo_push_url'); $re = $this->vivoGetAuth($appId, $appKey ,$appSecret); print_r($re);die; if ($re === false) { return reValue(4002, null, ['网络请求超时']); } if ($re['code'] != 0) { return reValue(4002, $re, ['网络请求超时']); } if (!isset($re['data']['auth_token'])) { return reValue(4004, $re, ['未知错误']); } } }