Browse Source

系统公告推送

lizhen_gitee 6 months ago
parent
commit
1ec4fdc845

+ 23 - 1
application/admin/controller/Messagesys.php

@@ -3,7 +3,8 @@
 namespace app\admin\controller;
 
 use app\common\controller\Backend;
-
+use think\Db;
+use app\common\library\Getui;
 /**
  * 系统公告
  *
@@ -24,6 +25,7 @@ class Messagesys extends Backend
         $this->model = new \app\admin\model\Messagesys;
         $this->view->assign("isIndexList", $this->model->getIsIndexList());
         $this->view->assign("isShowList", $this->model->getIsShowList());
+        $this->view->assign("pushStatusList", $this->model->getPushStatusList());
     }
 
 
@@ -34,5 +36,25 @@ class Messagesys extends Backend
      * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
      */
 
+    /**
+     * 推送到APP
+     */
+    public function tuisong(){
+
+        $id = input('id',0);
+        $info = Db::name('message_sys')->where('id',$id)->find();
+
+        Db::name('message_sys')->where('id',$id)->update(['push_status' => 1]);
 
+
+        $getui = new Getui();
+
+        $title = $info['title_en'];
+        $body = $info['info_en'];
+        $rs = $getui->sendtoall($title,  $body,  1, 'android');
+        $rs = $getui->sendtoall($title,  $body,  1, 'ios');
+
+        $this->success('推送完成');
+
+    }
 }

+ 4 - 1
application/admin/lang/en/messagesys.php

@@ -13,5 +13,8 @@ return [
     'Is_show'    => 'Is show',
     'Is_show 1'  => 'On',
     'Is_show 0'  => 'Off',
-    'Weigh'      => 'Weigh'
+    'Weigh'      => 'Weigh',
+    'Push_status'   => 'Push status',
+    'Push_status 0' => 'NO',
+    'Push_status 1' => 'YES'
 ];

+ 18 - 15
application/admin/lang/zh-cn/messagesys.php

@@ -1,19 +1,22 @@
 <?php
 
 return [
-    'Id'         => 'ID',
-    'Author'     => '作者',
-    'Avatar'     => '头像',
-    'Title'      => '消息标题',
-    'Title_en'   => '消息标题(en)',
-    'Info'       => '消息内容',
-    'Info_en'    => '消息内容(en)',
-    'Sendtime'   => '发布时间',
-    'Is_index'   => '放到首页',
-    'Is_index 0' => '否',
-    'Is_index 1' => '是',
-    'Is_show'    => '是否显示',
-    'Is_show 1'  => '上架',
-    'Is_show 0'  => '下架',
-    'Weigh'      => '排序'
+    'Id'            => 'ID',
+    'Author'        => '作者',
+    'Avatar'        => '头像',
+    'Title'         => '消息标题',
+    'Title_en'      => '消息标题(en)',
+    'Info'          => '消息内容',
+    'Info_en'       => '消息内容(en)',
+    'Sendtime'      => '发布时间',
+    'Is_index'      => '放到首页',
+    'Is_index 0'    => '否',
+    'Is_index 1'    => '是',
+    'Is_show'       => '是否显示',
+    'Is_show 1'     => '上架',
+    'Is_show 0'     => '下架',
+    'Weigh'         => '排序',
+    'Push_status'   => '推送状态',
+    'Push_status 0' => '未推送',
+    'Push_status 1' => '已推送'
 ];

+ 15 - 1
application/admin/model/Messagesys.php

@@ -27,7 +27,8 @@ class Messagesys extends Model
     protected $append = [
         'sendtime_text',
         'is_index_text',
-        'is_show_text'
+        'is_show_text',
+        'push_status_text'
     ];
     
 
@@ -50,6 +51,11 @@ class Messagesys extends Model
         return ['1' => __('Is_show 1'), '0' => __('Is_show 0')];
     }
 
+    public function getPushStatusList()
+    {
+        return ['0' => __('Push_status 0'), '1' => __('Push_status 1')];
+    }
+
 
     public function getSendtimeTextAttr($value, $data)
     {
@@ -73,6 +79,14 @@ class Messagesys extends Model
         return isset($list[$value]) ? $list[$value] : '';
     }
 
+
+    public function getPushStatusTextAttr($value, $data)
+    {
+        $value = $value ? $value : (isset($data['push_status']) ? $data['push_status'] : '');
+        $list = $this->getPushStatusList();
+        return isset($list[$value]) ? $list[$value] : '';
+    }
+
     protected function setSendtimeAttr($value)
     {
         return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);

+ 6 - 5
application/api/controller/Demo.php

@@ -83,13 +83,14 @@ class Demo extends Api
         $getui->push($cid,  $title,  $body, $ring_name, $type,  $platform);
 
     }
-    public function createmsg(){
+    public function sendtoall(){
         $getui = new Getui();
 
-        $title = '标题';
-        $body = '内容';
-        $type = 1;
-        $rs = $getui->sendtoall($title,  $body,  $type);
+        $title = '我是群发标题11222';
+        $body = '接下来两周的课程将于今天下午 6.45 发布22333';
+
+        $rs = $getui->sendtoall($title,  $body,  1, 'android');
+        $rs = $getui->sendtoall($title,  $body,  1, 'ios');
 
     }
 

+ 33 - 5
application/common/library/Getui.php

@@ -21,7 +21,9 @@ class Getui
     }
 
     //创建消息
-    public function sendtoall($title,  $body, $type = 1){
+    //type = 1 安卓
+    //type = 2 苹果
+    public function sendtoall($title,  $body, $type = 1,$platform = 'android'){
         if (!$access_token = $this->auth()) {
             return $this->error('ge tui token error');
         }
@@ -30,7 +32,7 @@ class Getui
         $notification = [
             "notification"=> [
                 "title"=> $title,
-                "body"=> $body,
+                "body" => $body,
                 "click_type"=> "url",
                 "url"=> "https//:xxx"
             ],
@@ -40,7 +42,7 @@ class Getui
         $transmission = [
             'transmission' => json_encode([
                 "title"=> $title,
-                "body"=> $body,
+                "body" => $body,
                 "t"     => time(),
             ]),
         ];
@@ -52,6 +54,30 @@ class Getui
             $push_message = $transmission;
         }
 
+        $push_channel = [];
+        if ($platform == 'ios') {
+            $push_channel = [
+                "ios" => [
+                    "type"       => "notify",
+                    "payload"    => json_encode([
+                        "title" => $title,
+                        "body"  => $body,
+                    ], JSON_UNESCAPED_UNICODE),
+                    "aps"        => [
+                        "alert"             => [
+                            "title" => $title,
+                            "body"  => $body,
+                        ],
+                        "content-available" => 0,
+                        "sound"             => "default",
+                        "category"          => "ACTIONABLE",
+                    ],
+                    "auto_badge" => "+1",
+                ]
+            ];
+            $push_message = $transmission;
+        }
+
         $data = [
             "request_id"=> createUniqueNo('PA'),
             "group_name"=> "",
@@ -61,6 +87,10 @@ class Getui
             "audience"=> "all",
             "push_message"=> $push_message,
         ];
+        if ($platform == 'ios') {
+            $data['push_channel'] = $push_channel;
+        }
+
 
         $response = $this->post('/push/all', $data, [
             'token:'.$access_token
@@ -86,7 +116,6 @@ class Getui
         if (!$access_token = $this->auth()) {
             return $this->error('ge tui token error');
         }
-        dump($access_token);
         // 通知
         $notification = [
             "notification" => [
@@ -209,7 +238,6 @@ class Getui
         $common_header = ["Content-Type:application/json;charset=UTF-8","Connection: Keep-Alive"];
         $header = array_merge($header,$common_header);
 
-        dump($header);
         $url = $this->base_uri.$this->appId.$uri;
         return curl_post($url,json_encode($params),$header);
     }

+ 23 - 1
public/assets/js/backend/messagesys.js

@@ -22,6 +22,8 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                 url: $.fn.bootstrapTable.defaults.extend.index_url,
                 pk: 'id',
                 sortName: 'weigh',
+                fixedColumns: true,
+                fixedRightNumber: 1,
                 columns: [
                     [
                         {checkbox: true},
@@ -36,7 +38,27 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                         {field: 'is_index', title: __('Is_index'), searchList: {"0":__('Is_index 0'),"1":__('Is_index 1')}, formatter: Table.api.formatter.normal},
                         {field: 'is_show', title: __('Is_show'), searchList: {"1":__('Is_show 1'),"0":__('Is_show 0')}, formatter: Table.api.formatter.normal},
                         {field: 'weigh', title: __('Weigh'), operate: false},
-                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
+                        {field: 'push_status', title: __('Push_status'), searchList: {"0":__('Push_status 0'),"1":__('Push_status 1')}, formatter: Table.api.formatter.status},
+                        {field: 'operate', title: __('Operate'), table: table,
+                            buttons:[
+                                {
+                                    name:'tuisong',
+                                    text:__('推送'),
+                                    title:__('推送'),
+                                    icon:'fa fa-exclamation-circle',
+                                    classname:'btn btn-xs btn-info btn-ajax',
+                                    url:'messagesys/tuisong/id/{ids}?dialog=1',
+                                    target:'_self',
+                                    hidden:function(row){
+                                        if(row.push_status == 0){
+                                            return false
+                                        }
+                                        return true;
+                                    },
+                                    refresh:{silent:true},
+                                }
+                            ],
+                            events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                     ]
                 ]
             });