kuaifan 5 yıl önce
ebeveyn
işleme
adca197ea8

+ 5 - 0
.env.docker

@@ -45,6 +45,11 @@ PUSHER_APP_KEY=
 PUSHER_APP_SECRET=
 PUSHER_APP_CLUSTER=mt1
 
+UMENG_PUSH_IOS_APPKEY=
+UMENG_PUSH_IOS_APPMASTERSECRET=
+UMENG_PUSH_ANDROID_APPKEY=
+UMENG_PUSH_ANDROID_APPMASTERSECRET=
+
 MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
 MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
 

+ 5 - 0
.env.example

@@ -45,6 +45,11 @@ PUSHER_APP_KEY=
 PUSHER_APP_SECRET=
 PUSHER_APP_CLUSTER=mt1
 
+UMENG_PUSH_IOS_APPKEY=
+UMENG_PUSH_IOS_APPMASTERSECRET=
+UMENG_PUSH_ANDROID_APPKEY=
+UMENG_PUSH_ANDROID_APPMASTERSECRET=
+
 MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
 MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
 

+ 36 - 0
app/Http/Controllers/Api/UsersController.php

@@ -513,4 +513,40 @@ class UsersController extends Controller
             'identity' => $identity
         ]);
     }
+
+    /**
+     * 设置、删除友盟token
+     *
+     * @apiParam {String} act           操作
+     * - set: 设置token
+     * - del: 删除token
+     * @apiParam {String} token         友盟token
+     * @apiParam {String} platform      ios|android
+     */
+    public function umeng__token()
+    {
+        $user = Users::authE();
+        if (Base::isError($user)) {
+            return $user;
+        } else {
+            $user = $user['data'];
+        }
+        //
+        $act = trim(Request::input('act'));
+        $token = trim(Request::input('token'));
+        if (empty($token)) {
+            return Base::retError('token empty');
+        }
+        $platform = strtolower(trim(Request::input('platform')));
+        DB::table('umeng')->where('token', $token)->delete();
+        if ($act != 'del') {
+            DB::table('umeng')->insert([
+                'token' => $token,
+                'username' => $user['username'],
+                'platform' => $platform,
+                'update' => Base::time(),
+            ]);
+        }
+        return Base::retSuccess('success');
+    }
 }

+ 34 - 23
app/Module/Chat.php

@@ -112,34 +112,12 @@ class Chat
             'indate' => $indate
         ];
         //
-        switch ($message['type']) {
-            case 'text':
-                $lastText = $message['text'];
-                break;
-            case 'image':
-                $lastText = '[图片]';
-                break;
-            case 'taskB':
-                $lastText = $message['text'] . " [来自关注任务]";
-                break;
-            case 'report':
-                $lastText = $message['text'] . " [来自工作报告]";
-                break;
-            case 'video':
-                $lastText = '[视频通话]';
-                break;
-            case 'voice':
-                $lastText = '[语音通话]';
-                break;
-            default:
-                $lastText = '[未知类型]';
-                break;
-        }
         if (mb_strlen($message['text']) > 20000) {
             return Base::retError("发送内容长度已超出最大限制!");
         }
         $field = ($dialog['recField'] == 1 ? 'unread1' : 'unread2');
         $unread = intval(DB::table('chat_dialog')->where('id', $dialog['id'])->value($field));
+        $lastText = self::messageDesc($message);
         if ($lastText) {
             $upArray = [];
             if ($username != $receive) {
@@ -227,4 +205,37 @@ class Chat
             ['update', '>', time() - 600],
         ])->whereIn('username', array_values(array_unique($tmpLists)))->get());
     }
+
+    /**
+     * 获取消息简述
+     * @param array $message
+     * @return mixed|string
+     */
+    public static function messageDesc($message)
+    {
+        switch ($message['type']) {
+            case 'text':
+                $lastText = $message['text'];
+                break;
+            case 'image':
+                $lastText = '[图片]';
+                break;
+            case 'taskB':
+                $lastText = $message['text'] . " [来自关注任务]";
+                break;
+            case 'report':
+                $lastText = $message['text'] . " [来自工作报告]";
+                break;
+            case 'video':
+                $lastText = '[视频通话]';
+                break;
+            case 'voice':
+                $lastText = '[语音通话]';
+                break;
+            default:
+                $lastText = '[未知类型]';
+                break;
+        }
+        return $lastText;
+    }
 }

+ 104 - 0
app/Module/Umeng.php

@@ -0,0 +1,104 @@
+<?php
+
+namespace App\Module;
+
+/**
+ * Class Umeng
+ * @package App\Module
+ */
+class Umeng
+{
+
+    /**
+     * 推送通知
+     * @param string $platform     ios|android
+     * @param string $token        umeng token
+     * @param string $title
+     * @param string $desc
+     * @param array $extra
+     * @return array
+     */
+    public static function notification($platform, $token, $title, $desc, $extra = [])
+    {
+        if ($platform == 'ios') {
+            $body = [
+                'appkey' => env('UMENG_PUSH_IOS_APPKEY'),
+                'timestamp' => Base::time(),
+                'type' => 'unicast',
+                'device_tokens' => $token,
+                'payload' => array_merge([
+                    'aps' => [
+                        'alert' => [
+                            'title' => $title,
+                            'subtitle' => '',
+                            'body' => $desc,
+                        ]
+                    ],
+                ], $extra),
+            ];
+        } else {
+            $body = [
+                'appkey' => env('UMENG_PUSH_ANDROID_APPKEY'),
+                'timestamp' => Base::time(),
+                'type' => 'unicast',
+                'device_tokens' => $token,
+                'payload' => [
+                    'display_type' => 'notification',
+                    'body' => [
+                        'ticker' => $title,
+                        'title' => $title,
+                        'text' => $desc,
+                    ],
+                    'extra' => $extra,
+                ],
+            ];
+        }
+        //
+        $res = self::curl($platform, 'https://msgapi.umeng.com/api/send', $body);
+        if (Base::isError($res)) {
+            return $res;
+        } else {
+            return Base::retSuccess('success');
+        }
+    }
+
+    /**
+     * 发送请求
+     * @param $platform
+     * @param $url
+     * @param $body
+     * @param string $method
+     * @return array
+     */
+    private static function curl($platform, $url, $body, $method = 'POST')
+    {
+        if ($platform == 'ios') {
+            $appkey = env('UMENG_PUSH_IOS_APPKEY');
+            $secret = env('UMENG_PUSH_IOS_APPMASTERSECRET');
+        } else {
+            $appkey = env('UMENG_PUSH_ANDROID_APPKEY');
+            $secret = env('UMENG_PUSH_ANDROID_APPMASTERSECRET');
+        }
+        if (empty($appkey)) {
+            return Base::retError('no appkey');
+        }
+        if (empty($secret)) {
+            return Base::retError('no secret');
+        }
+        //
+        $postBody = json_encode($body);
+        $mysign = md5($method . $url . $postBody . $secret);
+        $url.= "?sign=" . $mysign;
+        //
+        $res = Ihttp::ihttp_request($url, $postBody);
+        if (Base::isError($res)) {
+            return $res;
+        }
+        $array = json_decode($res['data'], true);
+        if ($array['ret'] == 'SUCCESS') {
+            return Base::retSuccess('success', $array['data']);
+        } else {
+            return Base::retError('error', $array['data']);
+        }
+    }
+}

+ 5 - 0
app/Services/WebSocketService.php

@@ -9,6 +9,7 @@ use App\Module\Chat;
 use App\Module\Project;
 use App\Module\Users;
 use App\Tasks\ChromeExtendTask;
+use App\Tasks\NotificationTask;
 use App\Tasks\PushTask;
 use Cache;
 use DB;
@@ -256,6 +257,10 @@ class WebSocketService implements WebSocketHandlerInterface
                     }
                     $pushTask = new PushTask($pushLists);
                     Task::deliver($pushTask);
+                    //
+                    $notificationTask = new NotificationTask($resData['id']);
+                    $notificationTask->delay(10);
+                    Task::deliver($notificationTask);
                 }
                 break;
 

+ 43 - 0
app/Tasks/NotificationTask.php

@@ -0,0 +1,43 @@
+<?php
+namespace App\Tasks;
+
+use App\Module\Base;
+use App\Module\Chat;
+use App\Module\Umeng;
+use App\Module\Users;
+use DB;
+use Hhxsv5\LaravelS\Swoole\Task\Task;
+
+class NotificationTask extends Task
+{
+    private $contentId;
+
+    /**
+     * NotificationTask constructor.
+     * @param int $contentId
+     */
+    public function __construct($contentId)
+    {
+        $this->contentId = intval($contentId);
+    }
+
+    public function handle()
+    {
+        $row = Base::DBC2A(DB::table('chat_msg')->where('id', $this->contentId)->first());
+        if (empty($row)) {
+            return;
+        }
+        if ($row['roger']) {
+            return;
+        }
+        //
+        $message = Base::string2array($row['message']);
+        $lists = Base::DBC2A(DB::table('umeng')->where('username', $row['username'])->get());
+        foreach ($lists AS $item) {
+            Umeng::notification($item['platform'], $item['token'], Users::nickname($row['username']), Chat::messageDesc($message), [
+                'contentId' => $row['contentId'],
+                'username' => $row['username'],
+            ]);
+        }
+    }
+}

+ 34 - 0
database/migrations/2020_07_25_164508_create_pre_umeng_table.php

@@ -0,0 +1,34 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreatePreUmengTable extends Migration
+{
+	/**
+	 * Run the migrations.
+	 *
+	 * @return void
+	 */
+	public function up()
+	{
+		Schema::create('umeng', function(Blueprint $table)
+		{
+			$table->string('token', 64)->default('')->unique('IDEX_token');
+			$table->string('username', 100)->nullable()->default('')->index('IDEX_username');
+			$table->string('platform', 50)->nullable()->default('');
+			$table->bigInteger('update')->nullable()->default(0);
+		});
+	}
+
+	/**
+	 * Reverse the migrations.
+	 *
+	 * @return void
+	 */
+	public function down()
+	{
+		Schema::drop('umeng');
+	}
+}

+ 1 - 1
package.json

@@ -1,5 +1,5 @@
 {
-    "version": "1.4.11",
+    "version": "1.4.12",
     "name": "wookteam",
     "private": true,
     "scripts": {