rabin 7 horas atrás
pai
commit
4e99b78b02

+ 10 - 0
lib/Manage.php

@@ -16,4 +16,14 @@ class Manage
         $result['content'] = $info['record'];
         return $result;
     }
+
+    # 获取消息模版标题
+    public function getTemplateName($id, $name = '')
+    {
+        if ($id) {
+            $info = Dever::db('template', 'msg')->find($id);
+            return $info['name'];
+        }
+        return $name;
+    }
 }

+ 7 - 3
lib/Method/Email.php

@@ -5,7 +5,7 @@ class Email
     private $config;
 
     # 初始化
-    public function init($template_id, $account)
+    public function init($template_id, $config_id, $account, $project = 'api')
     {
         if (!Dever::project('email')) {
             Dever::error('账户不存在');
@@ -13,7 +13,11 @@ class Email
         if (empty($account['email'])) {
             Dever::error('账户不存在');
         }
-        $this->config = Dever::db('template_email', 'msg')->find(['template_id' => $template_id]);
+        $msg = 'msg';
+        if ($project != 'api') {
+            $msg = $project;
+        }
+        $this->config = Dever::db('account_email', $msg)->find(['account_id' => $config_id]);
         if (!$this->config) {
             Dever::error('邮件配置不存在');
         }
@@ -22,7 +26,7 @@ class Email
     }
 
     # 发送短信
-    public function send($content, $param)
+    public function send($api, $content, $param)
     {
         if (!$this->config) {
             Dever::error('短信配置不存在');

+ 10 - 5
lib/Method/Sms.php

@@ -5,21 +5,26 @@ class Sms
     private $config;
 
     # 初始化
-    public function init($template_id, $account)
+    public function init($template_id, $config_id, $account, $project = 'api')
     {
         if (empty($account['mobile'])) {
-            Dever::error('账户不存在');
+            Dever::error('手机号不存在');
         }
-        $this->config = Dever::db('template_sms', 'msg')->find(['template_id' => $template_id]);
+        $msg = 'msg';
+        if ($project != 'api') {
+            $msg = $project;
+        }
+        $this->config = Dever::db('account_sms', $msg)->find(['account_id' => $config_id, 'template_id' => $template_id]);
         if (!$this->config) {
             Dever::error('短信配置不存在');
         }
+        $this->config['project'] = $project;
         $this->config['account'] = $account['mobile'];
         return $this->config['account'];
     }
 
     # 发送短信
-    public function send($content, $param)
+    public function send($api, $content, $param)
     {
         if (!$this->config) {
             Dever::error('短信配置不存在');
@@ -28,7 +33,7 @@ class Sms
         $send['TemplateCode'] = $this->config['code'];
         $send['TemplateParam'] = $param;
         $send['log'] = true;
-        $result = Dever::load('account', 'api')->run($this->config['account_key'], 'send_sms', $send);
+        $result = Dever::load('account', 'api')->run($api, 'send_sms', $send, 1, 'run', $this->config['project']);
         return Dever::json_encode($result);
     }
 }

+ 44 - 0
lib/Method/Wechat_applet.php

@@ -0,0 +1,44 @@
+<?php namespace Msg\Lib\Method;
+use Dever;
+class Wechat_applet
+{
+    private $config;
+
+    # 初始化
+    public function init($template_id, $config_id, $account, $project = 'api')
+    {
+        if (empty($param['openid'])) {
+            Dever::error('openid不存在');
+        }
+        $msg = 'msg';
+        if ($project != 'api') {
+            $msg = $project;
+        }
+        $this->config = Dever::db('account_wechat', $msg)->find(['account_id' => $config_id, 'template_id' => $template_id]);
+        if (!$this->config) {
+            Dever::error('小程序配置不存在');
+        }
+        $this->config['project'] = $project;
+        $this->config['account'] = $param['openid'];
+        return $this->config['account'];
+    }
+
+    # 发送
+    public function send($api, $content, $param)
+    {
+        if (!$this->config) {
+            Dever::error('小程序配置不存在');
+        }
+        $data = [];
+        foreach ($param as $k => $v) {
+            $data[$k]['value'] = $v;
+        }
+        $send['touser'] = $this->config['account'];
+        $send['template_id'] = $this->config['code'];
+        $send['page'] = $param['page'] ?? '';
+        $send['data'] = $data;
+        $send['log'] = true;
+        $result = Dever::load('account', 'api')->run($api, 'applet_send', $send, 1, 'run', $this->config['project']);
+        return Dever::json_encode($result);
+    }
+}

+ 44 - 0
lib/Method/Wechat_service.php

@@ -0,0 +1,44 @@
+<?php namespace Msg\Lib\Method;
+use Dever;
+class Wechat_service
+{
+    private $config;
+
+    # 初始化
+    public function init($template_id, $config_id, $account, $project = 'api')
+    {
+        if (empty($param['openid'])) {
+            Dever::error('openid不存在');
+        }
+        $msg = 'msg';
+        if ($project != 'api') {
+            $msg = $project;
+        }
+        $this->config = Dever::db('account_wechat', $msg)->find(['account_id' => $config_id, 'template_id' => $template_id]);
+        if (!$this->config) {
+            Dever::error('公众号配置不存在');
+        }
+        $this->config['project'] = $project;
+        $this->config['account'] = $param['openid'];
+        return $this->config['account'];
+    }
+
+    # 发送
+    public function send($api, $content, $param)
+    {
+        if (!$this->config) {
+            Dever::error('公众号配置不存在');
+        }
+        $data = [];
+        foreach ($param as $k => $v) {
+            $data[$k]['value'] = $v;
+        }
+        $send['touser'] = $this->config['account'];
+        $send['template_id'] = $this->config['code'];
+        $send['url'] = $param['url'] ?? '';
+        $send['data'] = $data;
+        $send['log'] = true;
+        $result = Dever::load('account', 'api')->run($api, 'service_send', $send, 1, 'run', $this->config['project']);
+        return Dever::json_encode($result);
+    }
+}

+ 19 - 4
lib/Template.php

@@ -2,10 +2,25 @@
 use Dever;
 class Template
 {
+    # 获取通知方式
+    public function method()
+    {
+        $value = [
+            ['id' => 'letter', 'name' => '站内信'],
+            ['id' => 'sms', 'name' => '短信'],
+            ['id' => 'wechat_service', 'name' => '微信公众号模板消息'],
+            ['id' => 'wechat_applet', 'name' => '微信小程序订阅消息'],
+            //['id' => 'app', 'name' => 'APP推送'],
+
+            //['id' => 'email', 'name' => '邮箱'],
+        ];
+        return $value;
+    } 
+
     # 发送消息
-    public function send($template, $account, $param = [])
+    public function send($template, $account = [], $param = [], $project = 'api')
     {
-        $template = Dever::db('template', 'msg')->find(['key' => $template]);
+        $template = Dever::db('template', 'msg')->find(['key' => $template, 'status' => 1]);
         if (!$template) {
             Dever::error('消息模板不存在');
         }
@@ -15,13 +30,13 @@ class Template
             $method = 'code';
         }
         $template['method'] = explode(',', $template['method']);
-        return Dever::load('type/' . $method, 'msg')->send($template, $account, $param);
+        return Dever::load('type/' . $method, 'msg')->send($template, $account, $param, $project);
     }
 
     # 验证码检查
     public function check($template, $account, $code, $update = 1)
     {
-        $template = Dever::db('template', 'msg')->find(['key' => $template]);
+        $template = Dever::db('template', 'msg')->find(['key' => $template, 'status' => 1]);
         if (!$template) {
             Dever::error('消息模板不存在');
         }

+ 25 - 16
lib/Type/Code.php

@@ -4,32 +4,41 @@ class Code
 {
     private $config;
     # 发送验证码
-    public function send($template, $account, $param = [])
+    public function send($template, $account = [], $param = [], $project = 'api')
     {
-        $this->config = Dever::db('template_code', 'msg')->find(['template_id' => $template['id']]);
-        if (!$this->config) {
-            Dever::error('验证码未配置');
-        }
         $data['template_id'] = $template['id'];
         $data['day'] = date('Ymd', DEVER_TIME);
-        $code = $this->createCode();
+        if (empty($param['code'])) {
+            $this->config = Dever::db('template_code', 'msg')->find(['template_id' => $template['id']]);
+            if (!$this->config) {
+                Dever::error('验证码未配置');
+            }
+            $param['code'] = $this->createCode();
+        }
         if ($template['content']) {
             $template['content'] = \Dever\Helper\Str::val($template['content'], $param);
         }
+        $test = 1;
         foreach ($template['method'] as $k => $v) {
-            $handle = Dever::load('method/' . $v, 'msg');
-            $data['account'] = $handle->init($template['id'], $account);
-            $this->valid($data);
-            if ($template['status'] == 1) {
-                $data['record'] = $handle->send($template['content'], ['code' => $code]);
+            $config = Dever::db('account', 'msg')->find(['method' => $v]);
+            if ($config) {
+                $handle = Dever::load('method/' . $config['method'], 'msg');
+                $data['account'] = $handle->init($template['id'], $config['id'], $account, $project);
+                $this->valid($data);
+                if ($config['test'] == 2) {
+                    $test = 2;
+                    $api = Dever::load('account', 'api')->get($config['api_account_id'], $project);
+                    $data['record'] = $handle->send($api, $template['content'], $param);
+                }
+                $data['code'] = $param['code'];
+                $data['status'] = 1;
+                Dever::db('code', 'msg')->insert($data);
+                # 这里以后加入计费机制
             }
-            $data['code'] = $code;
-            $data['status'] = 1;
-            Dever::db('code', 'msg')->insert($data);
         }
         $msg = '验证码发送成功';
-        if ($template['status'] == 2) {
-            $msg .= '::' . $code;
+        if ($test == 1) {
+            $msg .= '::' . $param['code'];
         }
         return $msg;
     }

+ 11 - 7
lib/Type/Notify.php

@@ -3,19 +3,23 @@ use Dever;
 class Notify
 {
     # 发送通知
-    public function send($template, $account, $param = [])
+    public function send($template, $account = [], $param = [], $project = 'api')
     {
         if ($template['content']) {
             $template['content'] = \Dever\Helper\Str::val($template['content'], $param);
         }
         foreach ($template['method'] as $k => $v) {
-            $handle = Dever::load('method/' . $v, 'msg');
-            $data['account'] = $handle->init($template['id'], $account);
-            if ($template['status'] == 1) {
-                $data['record'] = $handle->send($template['content'], $param);
+            $config = Dever::db('account', 'msg')->find(['method' => $v]);
+            if ($config) {
+                $handle = Dever::load('method/' . $config['method'], 'msg');
+                $data['account'] = $handle->init($template['id'], $config['id'], $account, $project);
+                if ($config['test'] == 2) {
+                    $api = Dever::load('account', 'api')->get($config['api_account_id'], $project);
+                    $data['record'] = $handle->send($api, $template['content'], $param);
+                }
+                $data['content'] = $template['content'];
+                Dever::db('notify', 'msg')->insert($data);
             }
-            $data['content'] = $template['content'];
-            Dever::db('notify', 'msg')->insert($data);
         }
         return '通知发送成功';
     }

+ 97 - 0
manage/account.php

@@ -0,0 +1,97 @@
+<?php
+$config = [
+    'list' => [
+        'desc' => '当同一个功能有两个账户时,将按照排序和状态使用最新的一个账户',
+        'field'      => [
+            'name',
+            'method',
+            'sort',
+            'test' => [
+                'tip' => '开启后,将不会实际发送请求',
+                'type' => 'switch',
+                'show'  => '{test}',
+                'active_value' => 1,
+                'inactive_value' => 2,
+            ],
+            'status',
+        ],
+        'button' => [
+            '新增' => ['fastadd', 'name,method,api_account_id,msg/account_email'],
+        ],
+        'data_button' => [
+            '设置' => ['fastedit', 'name,method,api_account_id,msg/account_email'],
+            '短信模板' => ['fastedit', 'msg/account_sms,tip', '', 'method=sms'],
+            '微信模板' => ['fastedit', 'msg/account_wechat,tip', '', 'method=wechat_service,wechat_applet'],
+        ],
+        'search' => [
+            'name',
+            'method',
+            'status',
+        ]
+    ],
+    'update' => [
+        'field'    => [
+            'name',
+            'method' => 'radio',
+            'api_account_id' => 'select',
+            'msg/account_email' => [
+                'name' => '邮件设置',
+                'where'  => ['account_id' => 'id'],
+                'default' => [[]],
+                # 默认使用表格形式展示,可以改成每行展示
+                'type' => 'line',
+            ],
+        ],
+        'control' => [
+            'msg/account_email' => [
+                'method' => ['email'],
+            ],
+            'api_account_id' => [
+                'method' => ['sms', 'wechat_service', 'wechat_applet', 'app'],
+            ],
+        ],
+    ],
+];
+
+$id = Dever::input('id');
+if ($id > 0) {
+    $default = [];
+    $info = Dever::db('account', 'msg')->find($id);
+    $template = Dever::db('template', 'msg')->select(['method' => ['like', $info['method']]]);
+    if ($template) {
+        foreach ($template as $v) {
+            $default[] = [
+                'template_id' => $v['id'],
+                'template_name' => $v['name'],
+            ];
+        }
+        if ($info['method'] == 'sms') {
+            $config['update']['field']['msg/account_sms'] = [
+                'name' => '短信模板',
+                'where'  => ['account_id' => 'id'],
+                'default' => $default,
+            ];
+        } elseif (strstr($info['method'], 'wechat')) {
+            $config['update']['field']['msg/account_wechat'] = [
+                'name' => '微信模板',
+                'where'  => ['account_id' => 'id'],
+                'default' => $default,
+            ];
+        }
+    } else {
+        if ($info['method'] == 'sms') {
+            $config['update']['field']['tip'] = [
+                'name' => '短信模板',
+                'type' => 'show',
+                'default' => '暂无短信模板,请在消息模板中选择',
+            ];
+        } elseif (strstr($info['method'], 'wechat')) {
+            $config['update']['field']['tip'] = [
+                'name' => '微信模板',
+                'type' => 'show',
+                'default' => '暂无微信模板,请在消息模板中选择',
+            ];
+        }
+    }
+}
+return $config;

+ 14 - 0
manage/account_email.php

@@ -0,0 +1,14 @@
+<?php
+return [
+    'update' => [
+        'field'    => [
+            'host' => [
+                'desc' => '主机地址,端口请用:隔开,格式为host:port',
+            ],
+            'user',
+            'username',
+            'pwd',
+            'title',
+        ],
+    ],
+];

+ 18 - 0
manage/account_sms.php

@@ -0,0 +1,18 @@
+<?php
+return [
+    'update' => [
+        'field'    => [
+            'template_id' => [
+                'type' => 'hidden',
+            ],
+            'template_name' => [
+                'type' => 'show',
+                'name' => '消息模板',
+                'default'  => 'Dever::call("msg/manage.getTemplateName", [{template_id}, "{template_name}"])',
+            ],
+            'code' => [
+                'tip' => '填写短信模板编码,阿里云短信需要模板编码',
+            ],
+        ],
+    ],
+];

+ 18 - 0
manage/account_wechat.php

@@ -0,0 +1,18 @@
+<?php
+return [
+    'update' => [
+        'field'    => [
+            'template_id' => [
+                'type' => 'hidden',
+            ],
+            'template_name' => [
+                'type' => 'show',
+                'name' => '消息模板',
+                'default'  => 'Dever::call("msg/manage.getTemplateName", [{template_id}, "{template_name}"])',
+            ],
+            'code' => [
+                'tip' => '填写微信模板ID',
+            ],
+        ],
+    ],
+];

+ 9 - 0
manage/core.php

@@ -15,11 +15,20 @@ return [
             'sort'      => '1',
         ],
 
+        'account' => [
+            'parent'    => 'msg',
+            'name'      => '消息账户',
+            'icon'      => 'codepen-fill',
+            'sort'      => '2',
+        ],
+
+        /*
         'code' => [
             'parent'    => 'msg',
             'name'      => '验证码记录',
             'icon'      => 'codepen-fill',
             'sort'      => '2',
         ],
+        */
     ],
 ];

+ 23 - 16
manage/template.php

@@ -6,13 +6,13 @@ return [
             'key',
             'type',
             'method',
-            'cdate',
             'status' => [
                 'type' => 'switch',
                 'show'  => '{status}',
                 'active_value' => 1,
                 'inactive_value' => 2,
             ],
+            'cdate',
         ],
         'button' => [
             '新增' => 'add',
@@ -27,18 +27,23 @@ return [
         ]
     ],
     'update' => [
-        'tab' => [
-            '基本设置' => 'name,key,content,type,msg/template_code',
-            '功能设置' => 'method,msg/template_sms,msg/template_email',
-        ],
         'field'    => [
-            'name',
-            'key',
+            'name' => [
+                'rules' => true,
+            ],
+            'key' => [
+                'rules' => true,
+            ],
+            'title' => [
+                'tip' => '可为空,一般为站内信或邮箱用到',
+            ],
             'content' => [
+                'rules' => true,
                 'type' => 'textarea',
                 'autosize' => ['minRows' => 2],
                 'desc' => '变量用大括号包含即可,如:{name}',
             ],
+            'method' => 'checkbox',
             'type' => 'radio',
             'msg/template_code' => [
                 'name' => '验证码设置',
@@ -47,33 +52,35 @@ return [
                 # 默认使用表格形式展示,可以改成每行展示
                 'type' => 'line',
             ],
-            
-            'method' => 'checkbox',
+
+            /*
             'msg/template_sms' => [
-                'name' => '短信设置',
+                'field' => 'code',
+                'name' => '短信模板',
                 'where'  => ['template_id' => 'id'],
-                'default' => [[]],
-                # 默认使用表格形式展示,可以改成每行展示
-                'type' => 'line',
+                'type' => 'text',
             ],
             'msg/template_email' => [
-                'name' => '邮件设置',
+                'name' => '邮件标题',
                 'where'  => ['template_id' => 'id'],
+                'type' => 'text',
                 'default' => [[]],
                 # 默认使用表格形式展示,可以改成每行展示
                 'type' => 'line',
-            ],
+                
+            ],*/
         ],
         'control' => [
             'msg/template_code' => [
                 'type' => 2,
             ],
+            /*
             'msg/template_sms' => [
                 'method' => ['sms'],
             ],
             'msg/template_email' => [
                 'method' => ['email'],
-            ],
+            ],*/
         ],
     ],
 ];

+ 0 - 6
manage/template_email.php

@@ -2,12 +2,6 @@
 return [
     'update' => [
         'field'    => [
-            'host' => [
-                'desc' => '主机地址,端口请用:隔开,格式为host:port',
-            ],
-            'user',
-            'username',
-            'pwd',
             'title',
         ],
     ],

+ 0 - 3
manage/template_sms.php

@@ -2,9 +2,6 @@
 return [
     'update' => [
         'field'    => [
-            'account_key' => [
-                'desc' => '',
-            ],
             'code' => [
                 'desc' => '填写短信模板编码,阿里云短信需要模板编码',
             ],

+ 54 - 0
table/account.php

@@ -0,0 +1,54 @@
+<?php
+return [
+    'name' => '消息账户',
+    'order' => 'sort asc,id asc',
+    'struct' => [
+        'name'      => [
+            'type'      => 'varchar(60)',
+            'name'      => '账户名称',
+        ],
+
+        'method'      => [
+            'type'      => 'varchar(100)',
+            'name'      => '账户功能',
+            'default'   => 'sms',
+            'value'    => 'Dever::call("msg/template.method")',
+        ],
+
+        'api_account_id'      => [
+            'type'      => 'int(11)',
+            'name'      => '万接账户',
+            'value'     => 'api/account',
+        ],
+
+        'test' => [
+            'name'      => '测试',
+            'type'      => 'tinyint(1)',
+            'default'   => 2,
+            'value'     => [
+                1 => '开启',
+                2 => '关闭',
+            ],
+        ],
+
+        'sort' => [
+            'name'      => '排序',
+            'type'      => 'int(11)',
+            'default'   => '1',
+        ],
+
+        'status' => [
+            'name'      => '状态',
+            'type'      => 'tinyint(1)',
+            'default'   => 1,
+            'value'     => [
+                1 => '展示',
+                2 => '不展示',
+            ],
+        ],
+    ],
+
+    'index' => [
+        'method' => 'method',
+    ],
+];

+ 5 - 2
table/template_email.php → table/account_email.php

@@ -2,9 +2,9 @@
 return [
     'name' => '邮箱设置',
     'struct' => [
-        'template_id'        => [
+        'account_id'        => [
             'type'      => 'int(11)',
-            'name'      => '模板',
+            'name'      => '账户',
         ],
 
         'host'      => [
@@ -32,4 +32,7 @@ return [
             'name'      => '邮件默认标题',
         ],
     ],
+    'index' => [
+        'search' => 'account_id',
+    ],
 ];

+ 9 - 5
table/template_sms.php → table/account_sms.php

@@ -2,14 +2,15 @@
 return [
     'name' => '短信设置',
     'struct' => [
-        'template_id'        => [
+        'account_id'        => [
             'type'      => 'int(11)',
-            'name'      => '模板',
+            'name'      => '账户',
         ],
 
-        'account_key'      => [
-            'type'      => 'varchar(50)',
-            'name'      => '账户标识',
+        'template_id'        => [
+            'type'      => 'int(11)',
+            'name'      => '消息模板',
+            'value'     => 'msg/template',
         ],
 
         'code'      => [
@@ -17,4 +18,7 @@ return [
             'name'      => '短信模板编码',
         ],
     ],
+    'index' => [
+        'search' => 'account_id,template_id',
+    ],
 ];

+ 24 - 0
table/account_wechat.php

@@ -0,0 +1,24 @@
+<?php
+return [
+    'name' => '模板设置',
+    'struct' => [
+        'account_id'        => [
+            'type'      => 'int(11)',
+            'name'      => '账户',
+        ],
+
+        'template_id'        => [
+            'type'      => 'int(11)',
+            'name'      => '消息模板',
+            'value'     => 'msg/template',
+        ],
+
+        'code'      => [
+            'type'      => 'varchar(50)',
+            'name'      => '微信模板ID',
+        ],
+    ],
+    'index' => [
+        'search' => 'account_id,template_id',
+    ],
+];

+ 4 - 1
table/code.php

@@ -1,7 +1,7 @@
 <?php
 return [
     'name' => '验证码',
-    'store' => 'log',
+    'partition' => 'Dever::call("manage/util.system")',
     'order' => 'cdate desc',
     'struct' => [
         'template_id'        => [
@@ -40,4 +40,7 @@ return [
             'name'      => '返回记录',
         ],
     ],
+    'index' => [
+        'search' => 'template_id,account',
+    ],
 ];

+ 6 - 1
table/notify.php

@@ -1,7 +1,7 @@
 <?php
 return [
     'name' => '通知记录',
-    'store' => 'log',
+    'partition' => 'Dever::call("manage/util.system")',
     'struct' => [
         'template_id'        => [
             'type'      => 'int(11)',
@@ -23,4 +23,9 @@ return [
             'name'      => '发送内容',
         ],
     ],
+
+    'index' => [
+        'account' => 'account',
+        'template_id' => 'template_id',
+    ],
 ];

+ 15 - 12
table/template.php

@@ -1,6 +1,7 @@
 <?php
 return [
     'name' => '消息模板',
+    'order' => 'id asc',
     'struct' => [
         'name'      => [
             'type'      => 'varchar(60)',
@@ -12,9 +13,14 @@ return [
             'name'      => '模板标识',
         ],
 
+        'title'      => [
+            'type'      => 'varchar(60)',
+            'name'      => '通知标题',
+        ],
+
         'type'      => [
             'type'      => 'tinyint(1)',
-            'name'      => '模板类型',
+            'name'      => '通知类型',
             'default'   => '1',
             'value'     => [
                 1 => '通知',
@@ -24,20 +30,14 @@ return [
 
         'method'      => [
             'type'      => 'varchar(100)',
-            'name'      => '模板功能',
+            'name'      => '通知方式',
             'default'   => '',
-            'value'     => [
-                //'letter' => '站内信',
-                'sms' => '短信',
-                'email' => '邮箱',
-                //'wechat' => '微信消息',
-                //'app' => 'APP推送',
-            ],
+            'value'    => 'Dever::call("msg/template.method")',
         ],
 
         'content'      => [
             'type'      => 'varchar(2000)',
-            'name'      => '模板内容',
+            'name'      => '通知内容',
         ],
 
         'status' => [
@@ -45,9 +45,12 @@ return [
             'type'      => 'tinyint(1)',
             'default'   => 1,
             'value'     => [
-                1 => '展示',
-                2 => '不展示',
+                1 => '开启',
+                2 => '关闭',
             ],
         ],
     ],
+    'index' => [
+        'search' => '`key`,status',
+    ],
 ];