Wang Chen 4 năm trước cách đây
mục cha
commit
8e97523771

+ 3 - 0
app/Cache/CacheKeys.php

@@ -21,6 +21,9 @@ class CacheKeys
     public static $all = [
         'lock' => [
             'token' => 'Lock:%s', // 锁,token
+        ],
+        'push' => [
+            'user' => 'Push:%s'
         ]
     ];
 }

+ 34 - 0
app/Cache/PushCache.php

@@ -0,0 +1,34 @@
+<?php
+
+
+namespace App\Cache;
+
+
+use App\Libs\Utils;
+use Illuminate\Support\Facades\Redis;
+
+class PushCache
+{
+    /**
+     * 获取用户reg id
+     * @param $uid
+     * @return mixed
+     */
+    public static function getUserPushRegId($uid)
+    {
+        $cacheKey = Utils::getCacheKey('push.user', [$uid]);
+        return Redis::get($cacheKey);
+    }
+
+    /**
+     * 设置用户reg id
+     * @param $uid
+     * @param $regId
+     * @return mixed
+     */
+    public static function setUserPushRegId($uid, $regId)
+    {
+        $cacheKey = Utils::getCacheKey('push.user', [$uid]);
+        return Redis::set($cacheKey, $regId);
+    }
+}

+ 72 - 0
app/Console/Commands/Push/HwPushTest.php

@@ -0,0 +1,72 @@
+<?php
+
+
+namespace App\Console\Commands\Push;
+
+
+use App\Consts\PushConst;
+use App\Libs\Push\HuaWei\Admin\Constants;
+use App\Libs\Push\HuaWei\HwPushCommon;
+use App\Libs\Push\HuaWei\PushMsgCommon;
+use Illuminate\Console\Command;
+
+class HwPushTest extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'push:hw:test';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'test push';
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $appId     = '102612543';
+        $appSecret = '07c896089f432163421923bdcab83a8da9d386274be43e61d510c27f56c23269';
+        $pushToken = 'AN0R08PKJjTJvCTRCL8hATh5ZxIVHQQOhpQ1JkpyRBjG25pRKbBckpUntQYBLpXNb2SFj8_bGb18uMeCT7KY5ehosiWZlopKUIphPYqeWKJeQcHGrn2fPKUsFEM-72X5Zg';
+
+        $fastAppPushCommon = new HwPushCommon($appId, $appSecret);
+        $fastAppPushCommon->setToken([$pushToken]);
+//        $fastAppPushCommon->subscribeTopic(PushConst::TOPIC_ALL, [$pushToken]);
+//        $fastAppPushCommon->unSubscribeTopic(PushConst::TOPIC_ALL, [$pushToken]);
+//        $result = $fastAppPushCommon->subscribeList($pushToken);
+//        $result            = $fastAppPushCommon->queryPushToken($pushToken);
+        $result = $fastAppPushCommon->sendPushMessage('我是标题', '我是内容', '/');
+        dd($result);
+
+//        $testPushMsgSample = new PushMsgCommon();
+//        $testPushMsgSample->sendPushMsgMessageByMsgType(Constants::PUSHMSG_FASTAPP_MSG_TYPE);
+
+//        $message = '{
+//	"data": "{\"pushtype\":1,\"pushbody\":{\"messageId\":\"111110001\",\"data\":\"test pass-through msg\"}}",
+//	"ssss":"{k1:v1}",
+//	"android": {
+//		"collapse_key": -1,
+//		"urgency": "HIGH",
+//		"ttl": "1448s",
+//		"bi_tag": "Trump",
+//		"fast_app_target": 1
+//	},
+//	"token": [
+//		*push_token*
+//		]
+//}';
+//
+//
+//        $message = str_ireplace("*push_token*", '"' . $testPushMsgSample->fast_push_token . '"', $message);
+//        $testPushMsgSample->sendPushMsgRealMessage(json_decode($message), Constants::PUSHMSG_FASTAPP_MSG_TYPE);
+    }
+
+}

+ 100 - 0
app/Console/Commands/Push/MiPushTest.php

@@ -0,0 +1,100 @@
+<?php
+
+namespace App\Console\Commands\Push;
+
+use App\Libs\Push\XMPush\Builder;
+use App\Libs\Push\XMPush\Constants;
+use App\Libs\Push\XMPush\Message;
+use App\Libs\Push\XMPush\Sender;
+use App\Libs\Push\XMPush\Stats;
+use App\Libs\Push\XMPush\TargetedMessage;
+use App\Libs\Push\XMPush\Tracer;
+use Illuminate\Console\Command;
+
+class MiPushTest extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'push:mi:test';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $secret  = 'W0OBbzwRFYE2qfqqzdxV7A==';
+        $package = 'com.beidao.kuaiying.zsy';
+
+        // 常量设置必须在new Sender()方法之前调用
+        Constants::setPackage($package);
+        Constants::setSecret($secret);
+
+        $aliasList = array('alias1', 'alias2');
+        $title     = '测试标题22';
+        $desc      = '这是一条mipush推送消息22';
+        $payload   = '{"test":1,"ok":"It\'s a string"}';
+
+        $sender = new Sender();
+
+        // message1 演示自定义的点击行为
+        $message1 = new Builder();
+        $message1->title($title);  // 通知栏的title
+        $message1->description($desc); // 通知栏的descption
+        $message1->passThrough(0);  // 这是一条通知栏消息,如果需要透传,把这个参数设置成1,同时去掉title和descption两个参数
+        $message1->payload($payload); // 携带的数据,点击后将会通过客户端的receiver中的onReceiveMessage方法传入。
+        $message1->extra(Builder::notifyForeground, 1); // 应用在前台是否展示通知,如果不希望应用在前台时候弹出通知,则设置这个参数为0
+        $message1->notifyId(2); // 通知类型。最多支持0-4 5个取值范围,同样的类型的通知会互相覆盖,不同类型可以在通知栏并存
+        $message1->build();
+        $targetMessage = new TargetedMessage();
+        $targetMessage->setTarget('alias1', TargetedMessage::TARGET_TYPE_ALIAS); // 设置发送目标。可通过regID,alias和topic三种方式发送
+        $targetMessage->setMessage($message1);
+
+        // message2 演示预定义点击行为中的点击直接打开app行为
+        $message2 = new Builder();
+        $message2->title($title);
+        $message2->description($desc);
+        $message2->passThrough(0);
+        $message2->payload($payload); // 对于预定义点击行为,payload会通过点击进入的界面的intent中的extra字段获取,而不会调用到onReceiveMessage方法。
+        $message2->extra(Builder::notifyEffect, 1); // 此处设置预定义点击行为,1为打开app
+        $message2->extra(Builder::notifyForeground, 1);
+        $message2->notifyId(0);
+        $message2->build();
+        $targetMessage2 = new TargetedMessage();
+        $targetMessage2->setTarget('alias2', TargetedMessage::TARGET_TYPE_ALIAS);
+        $targetMessage2->setMessage($message2);
+
+        $targetMessageList = array($targetMessage, $targetMessage2);
+        print_r($sender->send($message1, 'yeCXNzRSIiqLPfeoejM4gaLlpz3Tm8zyOctN+wFx0v6U73mTud4cYEJhEDyCEQrC')->getRaw());
+        //print_r($sender->multiSend($targetMessageList, TargetedMessage::TARGET_TYPE_ALIAS)->getRaw());
+
+        // print_r($sender->sendToAliases($message1, $aliasList)->getRaw());
+        $stats     = new Stats();
+        $startDate = '20200727';
+        $endDate   = '20200728';
+        // print_r($stats->getStats($startDate, $endDate)->getRaw());
+        //$tracer = new Tracer();
+        //print_r($tracer->getMessageStatusById('sdm58749595907489079Yl')->getRaw());
+    }
+}

+ 67 - 0
app/Console/Commands/Push/OppoPushTest.php

@@ -0,0 +1,67 @@
+<?php
+
+
+namespace App\Console\Commands\Push;
+
+
+use App\Consts\PushConst;
+use App\Libs\Push\OPPOPush\OPPOPushCommon;
+use Illuminate\Console\Command;
+use GuzzleHttp\Exception\GuzzleException;
+
+class OppoPushTest extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'push:oppo:test';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * @throws GuzzleException
+     */
+    public function handle()
+    {
+        // 配置
+        $regId = 'CN_83427bab875a0a40e4da3eeb2055f331';
+        [$appKey, $masterSecret] = ['354f2fba4c834e94a206fca247f132a0', 'ba2d9968c45b489da75e6bde7147b32a'];
+
+        // 初始化
+        $instance = new OPPOPushCommon($appKey, $masterSecret);
+
+        // 查询APP当天发送量的状态
+        $res = $instance->fetchPushPermit();
+        dd($res);
+
+        // 标签相关
+        $tag  = PushConst::TOPIC_ALL;
+        $res1 = $instance->addTags($tag, '全部用户');
+        $res2 = $instance->subscribeTags($regId, $tag);
+        $res3 = $instance->getAllTags($regId);
+        dd($res1, $res2, $res3);
+
+        // 发送消息
+        $messageId = $instance->getMessageId('我是标题11', '我是内容11', '/');
+        $res4      = $instance->broadCastRegIds($messageId, [$regId]);
+        dd($messageId, $res4);
+
+    }
+}

+ 113 - 0
app/Console/Commands/Push/PushTest.php

@@ -0,0 +1,113 @@
+<?php
+
+
+namespace App\Console\Commands\Push;
+
+
+use App\Consts\PushConst;
+use App\Libs\Push\HuaWei\HwPushCommon;
+use App\Libs\Push\OPPOPush\OPPOPushCommon;
+use App\Libs\Push\XMPush\MiPushCommon;
+use App\Modules\Push\Models\QappPushApp;
+use App\Modules\Push\Models\QappPushUser;
+use Exception;
+use Illuminate\Console\Command;
+
+class PushTest extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'push:test {uid}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'test push';
+
+    /**
+     * @throws \GuzzleHttp\Exception\GuzzleException
+     */
+    public function handle()
+    {
+        // 用户Uid
+        $uid = $this->argument('uid');
+
+        // 获取用户push信息
+        $pushUser  = QappPushUser::getPushUserByUid($uid);
+        $regId     = getProp($pushUser, 'reg_id');
+        $regIdList = [$regId];
+
+        // 获取push app信息
+        $pushAppId    = getProp($pushUser, 'app_id');
+        $pushApp      = QappPushApp::getPushAppByAppId($pushAppId);
+        $package      = getProp($pushApp, 'package');
+        $appId        = getProp($pushApp, 'app_id');
+        $name         = getProp($pushApp, 'name');
+        $provider     = getProp($pushApp, 'provider');
+        $appSecret    = getProp($pushApp, 'app_secret');
+        $appKey       = getProp($pushApp, 'app_key');
+        $masterSecret = getProp($pushApp, 'master_secret');
+
+        // 打印
+        var_dump('name:' . $name);
+        var_dump('package:' . $package);
+        var_dump('provider:' . $provider);
+        var_dump('app_id:' . $appId);
+        var_dump('app_secret:' . $appSecret);
+        var_dump('app_key:' . $appKey);
+        var_dump('master_secret:' . $masterSecret);
+
+        $title   = '这是' . $name . '标题';
+        $content = '这是' . $name . '内容,创建于' . date('Y-m-d H:i:s');
+        $url     = 'hap://app/com.app.kyy.xnyd/views/Reader?send_order_id=1643289&bid=vdqY7p15xnZQzK4VzzgmMD6wG2yr8BNX&chapter_id=4774851';
+        var_dump('uid:' . $uid);
+        var_dump('title:' . $title);
+        var_dump('content:' . $content);
+        var_dump('url:' . $url);
+        var_dump('reg id:' . $regId);
+
+        $result = [];
+        try {
+            switch ($provider) {
+                // 华为
+                case PushConst::PROVIDER_HW:
+                    // 初始化huawei推送
+                    $client = new HwPushCommon($appId, $appSecret);
+
+                    // 循环推送
+                    $client->setToken($regIdList);
+                    $result = $client->sendPushMessage($title, $content, $url);
+                    break;
+                // 小米
+                case PushConst::PROVIDER_MI:
+                    // 初始化小米推送
+                    $client = new MiPushCommon($package, $appSecret);
+
+                    // 循环推送
+                    $client->setRegArr($regIdList);
+                    $result = $client->sendMessage($title, $content, $url);
+                    break;
+                // OPPO
+                case PushConst::PROVIDER_OPPO:
+                    // 初始化oppo推送
+                    $client    = new OPPOPushCommon($appKey, $masterSecret);
+                    $messageId = $client->getMessageId($title, $content, $url);
+
+                    // 循环推送
+                    $client->setRegArr($regIdList);
+                    $result = $client->broadCastRegIds($messageId);
+                    break;
+            }
+        } catch (Exception $e) {
+            var_dump($e->getMessage());
+        }
+
+        dd($result);
+    }
+
+}

+ 37 - 0
app/Consts/ErrorConst.php

@@ -0,0 +1,37 @@
+<?php
+
+/**
+ * Created by PhpStorm.
+ * User: wangchen
+ * Date: 2019-05-06
+ * Time: 17:50
+ */
+
+namespace App\Consts;
+
+
+class ErrorConst
+{
+    // 基础错误
+    const RECORD_NOT_EXIST                  = '1001:记录不存在';
+    const PARAM_IS_EMPTY                    = '1002:参数不能为空';
+    const PARAM_ERROR_CODE                  = '1003:参数错误';
+    const TASK_DOING                        = '1004:任务执行中';
+    const NOT_AUTHORIZED                    = '1005:没有权限';
+    const BALANCE_NOT_ENOUGH                = '2001:账户余额不足';
+    const NOT_CHECKED                       = '6001:还没审核通过';
+    const ALREADY_CHECKED                   = '6002:已审核通过';
+    const ALREADY_PUSHED                    = '6003:已推送';
+    const VERIFY_CODE_ERROR                 = '6004:验证码有误';
+    const SYS_EXCEPTION                     = '6005:系统错误';
+    const FREQUENT_OPERATION                = '6006:操作频繁,请稍后操作';
+
+    // PUSH相关
+    const PUSH_APP_ID_INVALID               = '10001:无效appid';
+    const PUSH_TOKEN_INVALID                = '10002:无效reg_id';
+    const PUSH_TASK_INVALID                 = '10003:无效的推送任务';
+    const PUSH_TASK_NOT_START               = '10004:推送任务还未开始';
+    const PUSH_TASK_NO_USERS                = '10005:推送任务无有效用户';
+    const PUSH_TASK_LOGS_NOT_FOUND          = '10006:无有效子任务';
+    const PUSH_APP_NOT_SET                  = '10007:未设置推送应用';
+}

+ 26 - 0
app/Consts/PushConst.php

@@ -0,0 +1,26 @@
+<?php
+
+
+namespace App\Consts;
+
+
+class PushConst
+{
+    const TOPIC_ALL = 'all';
+
+    const PROVIDER_HW = 'huawei';
+
+    const PROVIDER_MI = 'xiaomi';
+
+    const PROVIDER_OPPO = 'oppo';
+
+    const PUSH_STATUS_TODO = 1;
+
+    const PUSH_STATUS_DOING = 2;
+
+    const PUSH_STATUS_SUCCESS = 3;
+
+    const PUSH_STATUS_FAIL = 4;
+
+    const PUSH_STATUS_STOP = 5;
+}

+ 60 - 0
app/Http/Controllers/QuickApp/Push/PushController.php

@@ -0,0 +1,60 @@
+<?php
+
+
+namespace App\Http\Controllers\QuickApp\Push;
+
+
+use App\Consts\ErrorConst;
+use App\Http\Controllers\QuickApp\BaseController;
+use App\Libs\ApiResponse;
+use App\Libs\Utils;
+use App\Modules\Push\Services\PushService;
+use Illuminate\Http\Request;
+use App\Exceptions\ApiException;
+
+class PushController extends BaseController
+{
+    use ApiResponse;
+
+    /**
+     * 设置用户推送reg_id
+     * @param Request $request
+     * @return mixed
+     * @throws ApiException
+     */
+    public function setUserRegId(Request $request)
+    {
+        $all   = $request->all();
+        $uid   = $this->uid;
+        $regId = trim(getProp($all, 'reg_id'));
+        $appId = trim(getProp($all, 'app_id'));
+        if (empty($regId) || empty($appId)) {
+            Utils::throwError(ErrorConst::PARAM_ERROR_CODE);
+        }
+
+        // 更新用户reg_id
+        $result = PushService::setUserRegId($uid, $regId, $appId);
+        if (!$result) {
+            Utils::throwError(ErrorConst::SYS_EXCEPTION);
+        }
+
+        return $this->success(compact('uid', 'regId'));
+    }
+
+    public function sendMessage(Request $request)
+    {
+        $all    = $request->all();
+        $taskId = (int)getProp($all, 'task_id');
+        if (empty($taskId)) {
+            Utils::throwError(ErrorConst::PARAM_ERROR_CODE);
+        }
+
+        // 更新用户reg_id
+        $result = PushService::sendMessage($taskId);
+        if (!$result) {
+            Utils::throwError(ErrorConst::SYS_EXCEPTION);
+        }
+
+        return $this->success();
+    }
+}