Dinghorn.php 8.8 KB
<?php

namespace addons\dinghorn;

use addons\dinghorn\library\DinghornLib;
use app\common\library\Menu;
use think\Addons;
use think\Db;

/**
 * 钉钉小喇叭
 */
class Dinghorn extends Addons
{
    /**
     * 通过模板发送通知消息
     * @param string $template 消息模板Id或Code
     * @param array $tpl_data 要覆盖的模板数据 (参见:\application\admin\controller\dinghorn\Example.php 的 example2 方法)
     * @param array $tpl_variable 模板变量键值对,若模板变量已预先定义,则会自动获取值,无需在此传递
     * @return array                发送结果
     */
    public static function msgNotice($template, $tpl_data = array(), $tpl_variable = array())
    {
        $now_time        = time();
        $dinghorn_config = get_addon_config('dinghorn');
        $template        = Db::name('dinghorn_template')
            ->where('id = :tpl OR code = :tpl_code', ['tpl' => $template, 'tpl_code' => $template])
            ->where('deletetime', null)
            ->find();

        if (!$template || !$template['openswitch']) {

            if ($dinghorn_config['error_log']) {

                $log_arr = array(
                    'robot_id'      => 0,
                    'template_id'   => isset($template['id']) ? $template['id'] : 0,
                    'template_data' => json_encode($tpl_data),
                    'errmsg'        => '消息模板未找到或已禁用!(-1)',
                    'status'        => '0',
                    'createtime'    => $now_time
                );

                Db::name('dinghorn_msglog')->insert($log_arr);
            }

            return ['errcode' => -1, 'errmsg' => '消息模板未找到或已禁用!'];
        }

        $DinghornLib = new DinghornLib($template, $tpl_data, $tpl_variable);

        // 获取模板内的机器人
        $robot_ids = isset($tpl_data['robot_ids']) ? $tpl_data['robot_ids'] : $template['robot_ids'];
        $robots    = Db::name('dinghorn_robot')
            ->whereIn('id', $robot_ids)
            ->where('openswitch', 1)
            ->where('deletetime', null)
            ->select();

        // 组装消息数据
        $msg_data = $DinghornLib->deal_with_msg();

        $log_arr = [];//要插入的日志
        $is_err  = false;//是否有错误,用于判定本次操作的返回值

        // 循环所有要发送的机器人
        foreach ($robots as $key => $value) {
            $res = $DinghornLib->msgSend($value['access_token'], $msg_data, $value['sign']);

            if ($res['errcode'] == 0 && $dinghorn_config['success_log']) {

                $log_arr[] = array(
                    'robot_id'      => $value['id'],
                    'template_id'   => $template['id'],
                    'template_data' => json_encode($msg_data),
                    'errmsg'        => '',
                    'status'        => '1',
                    'createtime'    => $now_time
                );
            } else if ($res['errcode'] != 0) {

                $is_err = true;
                $errmsg = $res['errmsg'] . '(' . $res['errcode'] . ')';

                if ($dinghorn_config['error_log']) {
                    $log_arr[] = array(
                        'robot_id'      => $value['id'],
                        'template_id'   => $template['id'],
                        'template_data' => json_encode($msg_data),
                        'errmsg'        => $errmsg,
                        'status'        => '0',
                        'createtime'    => $now_time
                    );
                }
            }
        }

        if ($log_arr) {
            Db::startTrans();
            try {
                Db::name('dinghorn_msglog')->insertAll($log_arr);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
            }
        }

        return $is_err ? ['errcode' => -2, 'errmsg' => '消息发送失败:' . $errmsg] : ['errcode' => 0];
    }

    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {
        $menu = [
            [
                'name'    => 'dinghorn',
                'title'   => '钉钉小喇叭',
                'icon'    => 'fa fa-volume-up',
                'sublist' => [
                    [
                        'name'    => 'dinghorn/robot',
                        'title'   => '机器人管理',
                        'icon'    => 'fa fa-circle-o',
                        'sublist' => [
                            ['name' => 'dinghorn/robot/index', 'title' => '查看'],
                            ['name' => 'dinghorn/robot/add', 'title' => '添加'],
                            ['name' => 'dinghorn/robot/edit', 'title' => '编辑'],
                            ['name' => 'dinghorn/robot/del', 'title' => '删除'],
                            ['name' => 'dinghorn/robot/multi', 'title' => '批量更新'],
                            ['name' => 'dinghorn/robot/recyclebin', 'title' => '回收站'],
                            ['name' => 'dinghorn/robot/destroy', 'title' => '真实删除'],
                            ['name' => 'dinghorn/robot/restore', 'title' => '还原'],
                        ]
                    ],
                    [
                        'name'    => 'dinghorn/variable',
                        'title'   => '模板变量管理',
                        'icon'    => 'fa fa-circle-o',
                        'remark'  => '在此处设置变量,随后可在模板通知内使用变量,变量值可来源于方法返回值或SQL查询结果',
                        'sublist' => [
                            ['name' => 'dinghorn/variable/index', 'title' => '查看'],
                            ['name' => 'dinghorn/variable/add', 'title' => '添加'],
                            ['name' => 'dinghorn/variable/edit', 'title' => '编辑'],
                            ['name' => 'dinghorn/variable/del', 'title' => '删除'],
                            ['name' => 'dinghorn/variable/multi', 'title' => '批量更新'],
                            ['name' => 'dinghorn/variable/recyclebin', 'title' => '回收站'],
                            ['name' => 'dinghorn/variable/destroy', 'title' => '真实删除'],
                            ['name' => 'dinghorn/variable/restore', 'title' => '还原'],
                            ['name' => 'dinghorn/variable/view_variable', 'title' => '计算变量值'],
                        ]
                    ],
                    [
                        'name'    => 'dinghorn/template',
                        'title'   => '通知模板管理',
                        'icon'    => 'fa fa-circle-o',
                        'sublist' => [
                            ['name' => 'dinghorn/template/index', 'title' => '查看'],
                            ['name' => 'dinghorn/template/add', 'title' => '添加'],
                            ['name' => 'dinghorn/template/edit', 'title' => '编辑'],
                            ['name' => 'dinghorn/template/del', 'title' => '删除'],
                            ['name' => 'dinghorn/template/multi', 'title' => '批量更新'],
                            ['name' => 'dinghorn/template/recyclebin', 'title' => '回收站'],
                            ['name' => 'dinghorn/template/destroy', 'title' => '真实删除'],
                            ['name' => 'dinghorn/template/restore', 'title' => '还原'],
                        ]
                    ],
                    [
                        'name'    => 'dinghorn/msglog',
                        'title'   => '通知发送日志管理',
                        'icon'    => 'fa fa-circle-o',
                        'sublist' => [
                            ['name' => 'dinghorn/msglog/index', 'title' => '查看'],
                            ['name' => 'dinghorn/msglog/edit', 'title' => '编辑'],
                            ['name' => 'dinghorn/msglog/del', 'title' => '删除'],
                            ['name' => 'dinghorn/msglog/multi', 'title' => '批量更新'],
                            ['name' => 'dinghorn/msglog/recyclebin', 'title' => '回收站'],
                            ['name' => 'dinghorn/msglog/destroy', 'title' => '真实删除'],
                            ['name' => 'dinghorn/msglog/restore', 'title' => '还原'],
                        ]
                    ],
                ]
            ]
        ];
        Menu::create($menu);
        return true;
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {
        Menu::delete('dinghorn');
        return true;
    }

    /**
     * 插件启用方法
     * @return bool
     */
    public function enable()
    {
        Menu::enable('dinghorn');
        return true;
    }

    /**
     * 插件禁用方法
     * @return bool
     */
    public function disable()
    {
        Menu::disable('dinghorn');
        return true;
    }
}