rabin 8 months ago
commit
e5080af595

+ 14 - 0
api/Task.php

@@ -0,0 +1,14 @@
+<?php namespace Msg\Lib;
+use Dever;
+class Task
+{
+    # 定期清理记录
+    public function drop()
+    {
+        # 删除1个月前的数据
+        list($start, $end) = Date::month(1);
+        $where['cdate'] = array('<=', $end);
+        Dever::db('code', 'sms')->delete($where);
+        Dever::db('notify', 'sms')->delete($where);
+    }
+}

+ 5 - 0
index.php

@@ -0,0 +1,5 @@
+<?php
+define('DEVER_APP_NAME', 'msg');
+define('DEVER_APP_LANG', '消息管理');
+define('DEVER_APP_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);
+include(DEVER_APP_PATH . '../../boot.php');

+ 19 - 0
lib/Manage.php

@@ -0,0 +1,19 @@
+<?php namespace Msg\Lib;
+use Dever;
+class Manage
+{
+    # 发送消息
+    public function showRecord($id, $type = 'code')
+    {
+        $info = Dever::db('code', 'msg')->find($id);
+        if (is_object($info['record'])) {
+            $info['record'] = (array)$info['record'];
+        }
+        if (is_array($info['record'])) {
+            $info['record'] = Dever::json_encode($info['record']);
+        }
+        $result['type'] = 'string';
+        $result['content'] = $info['record'];
+        return $result;
+    }
+}

+ 68 - 0
lib/Method/Email.php

@@ -0,0 +1,68 @@
+<?php namespace Msg\Lib\Method;
+use Dever;
+class Email
+{
+    private $config;
+
+    # 初始化
+    public function init($template_id, $account)
+    {
+        if (!Dever::project('email')) {
+            Dever::error('账户不存在');
+        }
+        if (empty($account['email'])) {
+            Dever::error('账户不存在');
+        }
+        $this->config = Dever::db('template_email', 'msg')->find(array('template_id' => $template_id));
+        if (!$this->config) {
+            Dever::error('邮件配置不存在');
+        }
+        $this->config['account'] = $account['email'];
+        return $this->config['account'];
+    }
+
+    # 发送短信
+    public function send($content, $param)
+    {
+        if (!$this->config) {
+            Dever::error('短信配置不存在');
+        }
+        Dever::apply('PHPMailer', 'email', 'src');
+        Dever::apply('Exception', 'email', 'src');
+        Dever::apply('SMTP', 'email', 'src');
+        $mail = new \PHPMailer\PHPMailer\PHPMailer();
+
+        list($host, $port) = explode(':', $this->config['host']);
+        $mail->isSMTP();
+        //$mail->SMTPDebug = 2;
+        $mail->CharSet = 'UTF-8';
+        $mail->Host = $host;
+        $mail->Port = $port;
+        if ($port == 465) {
+            $mail->SMTPSecure = 'ssl';
+        } else {
+            $mail->SMTPSecure = 'tls';
+        }
+        $mail->SMTPAuth = true;
+        $mail->Username = $this->config['user'];
+        $mail->Password = $this->config['pwd'];
+        $mail->setFrom($this->config['user'], $this->config['username']);
+        $mail->addAddress($this->config['account'], $this->config['account']);
+        if (empty($param['title'])) {
+            $param['title'] = $this->config['title'];
+        }
+        $mail->Subject = "=?utf-8?B?" . base64_encode($param['title']) . "?=";
+        $mail->Body = $content;
+        $mail->isHTML(true);
+        if (isset($param['file'])) {
+            $mail->addAttachment($param['file']);
+        }
+        if (!$mail->send()) {
+            $param['error'] = $mail->ErrorInfo;
+            Dever::log($param, 'email');
+            Dever::error("Mailer Error: " . $mail->ErrorInfo);
+        } else {
+            return 'ok';
+        }
+    }
+}

+ 34 - 0
lib/Method/Sms.php

@@ -0,0 +1,34 @@
+<?php namespace Msg\Lib\Method;
+use Dever;
+class Sms
+{
+    private $config;
+
+    # 初始化
+    public function init($template_id, $account)
+    {
+        if (empty($account['mobile'])) {
+            Dever::error('账户不存在');
+        }
+        $this->config = Dever::db('template_sms', 'msg')->find(array('template_id' => $template_id));
+        if (!$this->config) {
+            Dever::error('短信配置不存在');
+        }
+        $this->config['account'] = $account['mobile'];
+        return $this->config['account'];
+    }
+
+    # 发送短信
+    public function send($content, $param)
+    {
+        if (!$this->config) {
+            Dever::error('短信配置不存在');
+        }
+        $send['mobile'] = $this->config['account'];
+        $send['TemplateCode'] = $this->config['code'];
+        $send['TemplateParam'] = $param;
+        $send['log'] = true;
+        $result = Dever::load('account', 'api')->run($this->config['account_key'], 'send_sms', $send);
+        return Dever::json_encode($result);
+    }
+}

+ 35 - 0
lib/Template.php

@@ -0,0 +1,35 @@
+<?php namespace Msg\Lib;
+use Dever;
+class Template
+{
+    # 发送消息
+    public function send($template, $account, $param = array())
+    {
+        $template = Dever::db('template', 'msg')->find(array('key' => $template));
+        if (!$template) {
+            Dever::error('消息模板不存在');
+        }
+        if ($template['type'] == 1) {
+            $method = 'notify';
+        } elseif ($template['type'] == 2) {
+            $method = 'code';
+        }
+        $template['method'] = explode(',', $template['method']);
+        return Dever::load('type/' . $method, 'msg')->send($template, $account, $param);
+    }
+
+    # 验证码检查
+    public function check($template, $account, $code, $update = 1)
+    {
+        $template = Dever::db('template', 'msg')->find(array('key' => $template));
+        if (!$template) {
+            Dever::error('消息模板不存在');
+        }
+        if ($template['type'] == 2) {
+            $state = Dever::load('type/code', 'msg')->check($template['id'], $account, $code, $update);
+            if (!$state) {
+                Dever::error('验证码错误');
+            }
+        }
+    }
+}

+ 68 - 0
lib/Type/Code.php

@@ -0,0 +1,68 @@
+<?php namespace Msg\Lib\Type;
+use Dever;
+class Code
+{
+    private $config;
+    # 发送验证码
+    public function send($template, $account, $param = array())
+    {
+        $this->config = Dever::db('template_code', 'msg')->find(array('template_id' => $template['id']));
+        if (!$this->config) {
+            Dever::error('验证码未配置');
+        }
+        $data['template_id'] = $template['id'];
+        $data['day'] = date('Ymd', DEVER_TIME);
+        $code = $this->createCode();
+        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);
+            $this->valid($data);
+            if ($template['status'] == 1) {
+                $data['record'] = $handle->send($template['content'], array('code' => $code));
+            }
+            $data['code'] = $code;
+            $data['status'] = 1;
+            Dever::db('code', 'msg')->insert($data);
+        }
+        $msg = '验证码发送成功';
+        if ($template['status'] == 2) {
+            $msg .= '::' . $code;
+        }
+        return $msg;
+    }
+
+    # 检测并使用验证码
+    public function check($template_id, $account, $code, $update = 1)
+    {
+        $info = Dever::db('code', 'msg')->find(array('template_id' => $template_id, 'account' => $account));
+        if ($info && $info['status'] == 1 && $code == $info['code']) {
+            if ($update == 1) {
+                Dever::db('code', 'msg')->update($info['id'], array('status' => 2));
+            }
+            return true;
+        }
+        return false;
+    }
+
+    private function createCode()
+    {
+        $len = $this->config['length'] ? $this->config['length'] : 4;
+        $type = $this->config['type'] ? $this->config['type'] : 1;
+        return \Dever\Helper\Str::rand($len, $type - 1);
+    }
+
+    protected function valid($where)
+    {
+        $info = Dever::db('code', 'msg')->find($where);
+        if ($info) {
+            if (DEVER_TIME - $this->config['cdate'] < $this->config['interval']) {
+                Dever::error('请不要在'.$this->config['interval'].'秒之内申请多次验证码,请您稍后再试');
+            } elseif (Dever::db('code', 'msg')->count($where) >= $this->config['total']) {
+                Dever::alert('很抱歉,您已经申请获取验证码超过' . $this->config['total'] . '次,今天您已经无法获取验证码了,请您明天再来');
+            }
+        }
+    }
+}

+ 22 - 0
lib/Type/Notify.php

@@ -0,0 +1,22 @@
+<?php namespace Msg\Lib\Type;
+use Dever;
+class Notify
+{
+    # 发送通知
+    public function send($template, $account, $param = array())
+    {
+        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);
+            }
+            $data['content'] = $template['content'];
+            Dever::db('notify', 'msg')->insert($data);
+        }
+        return '通知发送成功';
+    }
+}

+ 39 - 0
manage/code.php

@@ -0,0 +1,39 @@
+<?php
+return array
+(
+    'list' => array
+    (
+        'field'      => array
+        (
+            'index' => array
+            (
+                'name' => '序号',
+            ),
+            'account',
+            'code',
+            'record' => array
+            (
+                'type' => 'popover',
+                'title' => '查看',
+                'location' => 'left',
+                'show' => 'Dever::load("msg/manage")->showRecord("{id}")',
+            ),
+            'status',
+            'cdate',
+        ),
+        'button' => array
+        (
+            //'新增' => 'fastadd',
+        ),
+        'data_button' => array
+        (
+            //'设置' => 'fastedit',
+        ),
+        'search' => array
+        (
+            'account',
+            'code',
+            'status',
+        )
+    ),
+);

+ 30 - 0
manage/core.php

@@ -0,0 +1,30 @@
+<?php
+return array
+(
+    'menu' => array
+    (
+        'msg' => array
+        (
+            'parent' => 'connect',
+            'name' => '消息管理',
+            'icon' => 'message-3-line',
+            'sort' => '70',
+        ),
+
+        'template' => array
+        (
+            'parent'    => 'msg',
+            'name'      => '消息模板',
+            'icon'      => 'message-3-line',
+            'sort'      => '1',
+        ),
+
+        'code' => array
+        (
+            'parent'    => 'msg',
+            'name'      => '验证码记录',
+            'icon'      => 'codepen-fill',
+            'sort'      => '2',
+        ),
+    ),
+);

+ 34 - 0
manage/notify.php

@@ -0,0 +1,34 @@
+<?php
+return array
+(
+    'list' => array
+    (
+        'field'      => array
+        (
+            'id',
+            'account',
+            'content',
+            'record' => array
+            (
+                'type' => 'popover',
+                'title' => '查看',
+                'location' => 'right',
+                'show' => '{record}',
+            ),
+            'cdate',
+        ),
+        'button' => array
+        (
+            //'新增' => 'fastadd',
+        ),
+        'data_button' => array
+        (
+            //'设置' => 'fastedit',
+        ),
+        'search' => array
+        (
+            'account',
+            'content',
+        )
+    ),
+);

+ 97 - 0
manage/template.php

@@ -0,0 +1,97 @@
+<?php
+return array
+(
+    'list' => array
+    (
+        'field'      => array
+        (
+            'name',
+            'key',
+            'type',
+            'method',
+            'cdate',
+            'status' => array
+            (
+                'type' => 'switch',
+                'show'  => '{status}',
+                'active_value' => 1,
+                'inactive_value' => 2,
+            ),
+        ),
+        'button' => array
+        (
+            '新增' => 'add',
+        ),
+        'data_button' => array
+        (
+            '设置' => 'edit',
+        ),
+        'search' => array
+        (
+            'name',
+            'key',
+            'status',
+        )
+    ),
+    'update' => array
+    (
+        'tab' => array
+        (
+            '基本设置' => 'name,key,content,type,msg/template_code',
+            '功能设置' => 'method,msg/template_sms,msg/template_email',
+        ),
+        'field'    => array
+        (
+            'name',
+            'key',
+            'content' => array
+            (
+                'type' => 'textarea',
+                'autosize' => array('minRows' => 2),
+                'desc' => '变量用大括号包含即可,如:{name}',
+            ),
+            'type' => 'radio',
+            'msg/template_code' => array
+            (
+                'name' => '验证码设置',
+                'where'  => array('template_id' => 'id'),
+                'default' => array(array()),
+                # 默认使用表格形式展示,可以改成每行展示
+                'type' => 'line',
+            ),
+            
+            'method' => 'checkbox',
+            'msg/template_sms' => array
+            (
+                'name' => '短信设置',
+                'where'  => array('template_id' => 'id'),
+                'default' => array(array()),
+                # 默认使用表格形式展示,可以改成每行展示
+                'type' => 'line',
+            ),
+            'msg/template_email' => array
+            (
+                'name' => '邮件设置',
+                'where'  => array('template_id' => 'id'),
+                'default' => array(array()),
+                # 默认使用表格形式展示,可以改成每行展示
+                'type' => 'line',
+            ),
+        ),
+        'control' => array
+        (
+            'msg/template_code' => array
+            (
+                'type' => 2,
+            ),
+            'msg/template_sms' => array
+            (
+                'method' => array('sms'),
+            ),
+            'msg/template_email' => array
+            (
+                'method' => array('email'),
+            ),
+        ),
+    ),
+);

+ 30 - 0
manage/template_code.php

@@ -0,0 +1,30 @@
+<?php
+return array
+(
+    'update' => array
+    (
+        'field'    => array
+        (
+            'type' => array
+            (
+                'type' => 'select',
+            ),
+            'timeout' => array
+            (
+                'desc' => '单位为秒数',
+            ),
+            'length' => array
+            (
+                'desc' => '设置验证码生成的长度',
+            ),
+            'total' => array
+            (
+                'desc' => '设置每天可以发送的最大次数',
+            ),
+            'interval' => array
+            (
+                'desc' => '单位为秒数,填写60就是每60秒可以发送一次',
+            ),
+        ),
+    ),
+);

+ 18 - 0
manage/template_email.php

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

+ 18 - 0
manage/template_sms.php

@@ -0,0 +1,18 @@
+<?php
+return array
+(
+    'update' => array
+    (
+        'field'    => array
+        (
+            'account_key' => array
+            (
+                'desc' => '',
+            ),
+            'code' => array
+            (
+                'desc' => '填写短信模板编码,阿里云短信需要模板编码',
+            ),
+        ),
+    ),
+);

+ 52 - 0
table/code.php

@@ -0,0 +1,52 @@
+<?php
+return array
+(
+    'name' => '验证码',
+    'store' => 'log',
+    'order' => 'cdate desc',
+    'struct' => array
+    (
+        'template_id'        => array
+        (
+            'type'      => 'int(11)',
+            'name'      => '模板',
+        ),
+
+        'account'      => array
+        (
+            'type'      => 'varchar(50)',
+            'name'      => '账户',
+        ),
+
+        'code'      => array
+        (
+            'type'      => 'varchar(50)',
+            'name'      => '验证码',
+        ),
+
+        'day'      => array
+        (
+            'type'      => 'int(11)',
+            'name'      => '发送的时间',
+            'default'   => '5',
+        ),
+
+        'status'      => array
+        (
+            'type'      => 'tinyint(1)',
+            'name'      => '状态',
+            'default'   => '1',
+            'value'     => array
+            (
+                1 => '未使用',
+                2 => '已使用',
+            )
+        ),
+
+        'record'        => array
+        (
+            'type'      => 'text(255)',
+            'name'      => '返回记录',
+        ),
+    ),
+);

+ 32 - 0
table/notify.php

@@ -0,0 +1,32 @@
+<?php
+return array
+(
+    'name' => '通知记录',
+    'store' => 'log',
+    'struct' => array
+    (
+        'template_id'        => array
+        (
+            'type'      => 'int(11)',
+            'name'      => '模板',
+        ),
+
+        'account'      => array
+        (
+            'type'      => 'varchar(50)',
+            'name'      => '账户',
+        ),
+
+        'record'        => array
+        (
+            'type'      => 'text(255)',
+            'name'      => '返回记录',
+        ),
+
+        'content'        => array
+        (
+            'type'      => 'text(255)',
+            'name'      => '发送内容',
+        ),
+    ),
+);

+ 64 - 0
table/template.php

@@ -0,0 +1,64 @@
+<?php
+return array
+(
+    'name' => '消息模板',
+    'struct' => array
+    (
+        'name'      => array
+        (
+            'type'      => 'varchar(60)',
+            'name'      => '模板名称',
+        ),
+
+        'key'       => array
+        (
+            'type'      => 'varchar(60)',
+            'name'      => '模板标识',
+        ),
+
+        'type'      => array
+        (
+            'type'      => 'tinyint(1)',
+            'name'      => '模板类型',
+            'default'   => '1',
+            'value'     => array
+            (
+                1 => '通知',
+                2 => '验证码',
+            ),
+        ),
+
+        'method'      => array
+        (
+            'type'      => 'varchar(100)',
+            'name'      => '模板功能',
+            'default'   => '',
+            'value'     => array
+            (
+                //'letter' => '站内信',
+                'sms' => '短信',
+                'email' => '邮箱',
+                //'wechat' => '微信消息',
+                //'app' => 'APP推送',
+            ),
+        ),
+
+        'content'      => array
+        (
+            'type'      => 'varchar(2000)',
+            'name'      => '模板内容',
+        ),
+
+        'status' => array
+        (
+            'name'      => '状态',
+            'type'      => 'tinyint(1)',
+            'default'   => 1,
+            'value'     => array
+            (
+                1 => '展示',
+                2 => '不展示',
+            ),
+        ),
+    ),
+);

+ 56 - 0
table/template_code.php

@@ -0,0 +1,56 @@
+<?php
+return array
+(
+    'name' => '验证码设置',
+    'struct' => array
+    (
+        'template_id'        => array
+        (
+            'type'      => 'int(11)',
+            'name'      => '模板',
+        ),
+
+        'timeout'      => array
+        (
+            'type'      => 'int(11)',
+            'name'      => '有效期',
+            'default'   => '600',
+        ),
+
+        'length'       => array
+        (
+            'type'      => 'int(11)',
+            'name'      => '长度',
+            'default'   => '4',
+        ),
+
+        'type'      => array
+        (
+            'type'      => 'tinyint(1)',
+            'name'      => '类型',
+            'default'   => '1',
+            'value'     => array
+            (
+                1 => '全数字',
+                2 => '小写字母',
+                3 => '大写字母',
+                4 => '大小写字母',
+                5 => '数字+大小写字母',
+            ),
+        ),
+
+        'total'      => array
+        (
+            'type'      => 'int(11)',
+            'name'      => '最大发送次数',
+            'default'   => '5',
+        ),
+
+        'interval'       => array
+        (
+            'type'      => 'int(11)',
+            'name'      => '发送间隔',
+            'default'   => '60',
+        ),
+    ),
+);

+ 43 - 0
table/template_email.php

@@ -0,0 +1,43 @@
+<?php
+return array
+(
+    'name' => '邮箱设置',
+    'struct' => array
+    (
+        'template_id'        => array
+        (
+            'type'      => 'int(11)',
+            'name'      => '模板',
+        ),
+
+        'host'      => array
+        (
+            'type'      => 'varchar(100)',
+            'name'      => '主机地址',
+        ),
+
+        'user'      => array
+        (
+            'type'      => 'varchar(100)',
+            'name'      => '邮件账户',
+        ),
+
+        'pwd'      => array
+        (
+            'type'      => 'varchar(100)',
+            'name'      => '账户密码',
+        ),
+
+        'username'      => array
+        (
+            'type'      => 'varchar(100)',
+            'name'      => '账户名称',
+        ),
+
+        'title'      => array
+        (
+            'type'      => 'varchar(100)',
+            'name'      => '邮件默认标题',
+        ),
+    ),
+);

+ 25 - 0
table/template_sms.php

@@ -0,0 +1,25 @@
+<?php
+return array
+(
+    'name' => '短信设置',
+    'struct' => array
+    (
+        'template_id'        => array
+        (
+            'type'      => 'int(11)',
+            'name'      => '模板',
+        ),
+
+        'account_key'      => array
+        (
+            'type'      => 'varchar(50)',
+            'name'      => '账户标识',
+        ),
+
+        'code'      => array
+        (
+            'type'      => 'varchar(50)',
+            'name'      => '短信模板编码',
+        ),
+    ),
+);