Przeglądaj źródła

add:后端调用接口;

Wang Chen 4 lat temu
rodzic
commit
bcd0857052

+ 2 - 0
app/Consts/ErrorConst.php

@@ -25,6 +25,7 @@ class ErrorConst
     const VERIFY_CODE_ERROR                 = '6004:验证码有误';
     const SYS_EXCEPTION                     = '6005:系统错误';
     const FREQUENT_OPERATION                = '6006:操作频繁,请稍后操作';
+    const SIGN_NOT_ACCESS                   = '6007:签名验证失败';
 
     // PUSH相关
     const PUSH_APP_ID_INVALID               = '10001:无效appid';
@@ -34,4 +35,5 @@ class ErrorConst
     const PUSH_TASK_NO_USERS                = '10005:推送任务无有效用户';
     const PUSH_TASK_LOGS_NOT_FOUND          = '10006:无有效子任务';
     const PUSH_APP_NOT_SET                  = '10007:未设置推送应用';
+    const PUSH_FAIELD                       = '10008:推送失败,请联系管理员';
 }

+ 15 - 28
app/Http/Controllers/QuickApp/Push/PushController.php

@@ -8,7 +8,7 @@ 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 App\Modules\Push\Services\PushMessageService;
 use Illuminate\Http\Request;
 use App\Exceptions\ApiException;
 
@@ -17,44 +17,31 @@ class PushController extends BaseController
     use ApiResponse;
 
     /**
-     * 设置用户推送reg_id
      * @param Request $request
      * @return mixed
      * @throws ApiException
+     * @throws \GuzzleHttp\Exception\GuzzleException
      */
-    public function setUserRegId(Request $request)
+    public function pushToUser(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)) {
+        $all     = $request->all();
+        $uid     = (int)getProp($all, 'uid');
+        $title   = trim(getProp($all, 'title'));
+        $content = trim(getProp($all, 'content'));
+        $url     = trim(getProp($all, 'url'));
+        $sign    = trim(getProp($all, 'sign'));
+        if (empty($uid) || empty($title) || empty($content) || empty($url)) {
             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);
+        // 签名校验
+        if ($sign !== 'dqu7nsNZY&A8AEzwNQ*WpbjHMd6bUt@V') {
+            Utils::throwError(ErrorConst::SIGN_NOT_ACCESS);
         }
 
         // 更新用户reg_id
-        $result = PushService::sendMessage($taskId);
-        if (!$result) {
-            Utils::throwError(ErrorConst::SYS_EXCEPTION);
-        }
+        $result = PushMessageService::pushMessageToUser($uid, $title, $content, $url);
 
-        return $this->success();
+        return $this->success($result);
     }
 }

+ 5 - 0
app/Http/Routes/QuickApp/QuickAppRoutes.php

@@ -88,6 +88,11 @@ Route::group(['domain' => env('QUICKAPP_DOMAIN'), 'namespace' => 'App\Http\Contr
 
         Route::get('customer_img', 'WelcomeController@getCustomerServiceImg');
     });
+
+    // 推送
+    Route::group(['prefix' => 'api/push'], function () {
+        Route::post('pushToUser', 'Push\PushController@pushToUser');
+    });
 });
 
 //快应用派单推广

+ 90 - 0
app/Modules/Push/Services/PushMessageService.php

@@ -0,0 +1,90 @@
+<?php
+
+
+namespace App\Modules\Push\Services;
+
+
+use App\Consts\ErrorConst;
+use App\Consts\PushConst;
+use App\Libs\Push\HuaWei\HwPushCommon;
+use App\Libs\Push\OPPOPush\OPPOPushCommon;
+use App\Libs\Push\XMPush\MiPushCommon;
+use App\Libs\Utils;
+use App\Modules\Push\Models\QappPushApp;
+use App\Modules\Push\Models\QappPushUser;
+use Exception;
+use GuzzleHttp\Exception\GuzzleException;
+use App\Exceptions\ApiException;
+
+class PushMessageService
+{
+    /**
+     * @param $uid
+     * @param $title
+     * @param $content
+     * @param $url
+     * @return bool
+     * @throws ApiException
+     * @throws GuzzleException
+     */
+    public static function pushMessageToUser($uid, $title, $content, $url): bool
+    {
+        // 获取用户push信息
+        $pushUser  = QappPushUser::getPushUserByUid($uid);
+        $regId     = getProp($pushUser, 'reg_id');
+        $regIdList = [$regId];
+        if (empty($regId)) {
+            Utils::throwError(ErrorConst::PUSH_TOKEN_INVALID);
+        }
+
+        // 获取push app信息
+        $pushAppId    = getProp($pushUser, 'app_id');
+        $pushApp      = QappPushApp::getPushAppByAppId($pushAppId);
+        $package      = getProp($pushApp, 'package');
+        $appId        = getProp($pushApp, 'app_id');
+        $provider     = getProp($pushApp, 'provider');
+        $appSecret    = getProp($pushApp, 'app_secret');
+        $appKey       = getProp($pushApp, 'app_key');
+        $masterSecret = getProp($pushApp, 'master_secret');
+
+        $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) {
+            $message = $e->getMessage();
+            myLog('push')->info('Exception', compact('result', 'message'));
+            Utils::throwError(ErrorConst::PUSH_FAIELD);
+        }
+
+        return $result;
+    }
+}