first commit

This commit is contained in:
Your Name
2026-09-08 11:40:15 +08:00
commit a5353f7eb5
9568 changed files with 1646214 additions and 0 deletions
@@ -0,0 +1,178 @@
-- 一诊 / 企业微信推广助手
-- 页面组件:first_visit/wecom_promotion/index
-- 页面权限:firstvisit.wecomPromotion/overview
START TRANSACTION;
CREATE TABLE IF NOT EXISTS `zyt_qywx_promotion_provider_state` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`suite_id` varchar(128) NOT NULL DEFAULT '' COMMENT '第三方应用 SuiteID',
`suite_ticket_cipher` text NOT NULL COMMENT '加密后的 suite_ticket',
`ticket_received_at` int unsigned NOT NULL DEFAULT 0,
`create_time` int unsigned NOT NULL DEFAULT 0,
`update_time` int unsigned NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_suite_id` (`suite_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='企微推广服务商状态';
CREATE TABLE IF NOT EXISTS `zyt_qywx_promotion_account` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`corp_id` varchar(128) NOT NULL DEFAULT '' COMMENT '授权企业 CorpID(服务商体系)',
`corp_name` varchar(160) NOT NULL DEFAULT '',
`permanent_code_cipher` text NOT NULL COMMENT 'AES-GCM 加密永久授权码',
`agent_id` varchar(64) NOT NULL DEFAULT '',
`auth_info_json` mediumtext NULL,
`auth_status` tinyint unsigned NOT NULL DEFAULT 1 COMMENT '0取消 1有效',
`owner_admin_id` int unsigned NOT NULL DEFAULT 0,
`dept_id` int unsigned NOT NULL DEFAULT 0,
`authorized_at` int unsigned NOT NULL DEFAULT 0,
`last_refresh_at` int unsigned NOT NULL DEFAULT 0,
`create_time` int unsigned NOT NULL DEFAULT 0,
`update_time` int unsigned NOT NULL DEFAULT 0,
`delete_time` int unsigned NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_corp_id` (`corp_id`),
KEY `idx_owner_status` (`owner_admin_id`,`auth_status`),
KEY `idx_dept` (`dept_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='企微推广授权企业';
CREATE TABLE IF NOT EXISTS `zyt_qywx_promotion_pool` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(60) NOT NULL DEFAULT '',
`public_key` char(32) NOT NULL COMMENT '公开 JS 分流键',
`status` tinyint unsigned NOT NULL DEFAULT 1,
`fallback_url` varchar(1000) NOT NULL DEFAULT '',
`widget_config_json` text NULL COMMENT '公开浮窗配置 JSON v1',
`click_count` bigint unsigned NOT NULL DEFAULT 0,
`owner_admin_id` int unsigned NOT NULL DEFAULT 0,
`dept_id` int unsigned NOT NULL DEFAULT 0,
`create_time` int unsigned NOT NULL DEFAULT 0,
`update_time` int unsigned NOT NULL DEFAULT 0,
`delete_time` int unsigned NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_public_key` (`public_key`),
KEY `idx_owner_status` (`owner_admin_id`,`status`),
KEY `idx_dept` (`dept_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='企微推广分流方案';
CREATE TABLE IF NOT EXISTS `zyt_qywx_promotion_link` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`pool_id` int unsigned NOT NULL DEFAULT 0,
`account_id` int unsigned NOT NULL DEFAULT 0,
`name` varchar(80) NOT NULL DEFAULT '',
`group_name` varchar(60) NOT NULL DEFAULT '默认分组',
`wecom_url` varchar(1000) NOT NULL DEFAULT '',
`remote_link_id` varchar(128) NULL DEFAULT NULL COMMENT '企业微信获客助手 link_id',
`remote_status` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '0历史手工 1远端有效 2远端已删除 3远端不可见 4同步失败',
`remote_create_time` int unsigned NOT NULL DEFAULT 0,
`range_user_json` text NULL COMMENT '获客链接可用成员 userid JSON',
`range_department_json` text NULL COMMENT '获客链接可用企微部门 ID JSON',
`skip_verify` tinyint unsigned NOT NULL DEFAULT 0,
`priority_option_json` text NULL,
`remote_snapshot` mediumtext NULL,
`last_sync_time` int unsigned NOT NULL DEFAULT 0,
`sync_error` varchar(500) NOT NULL DEFAULT '',
`weight` smallint unsigned NOT NULL DEFAULT 1,
`status` tinyint unsigned NOT NULL DEFAULT 1,
`daily_limit` int unsigned NOT NULL DEFAULT 0 COMMENT '0为不限',
`today_count` int unsigned NOT NULL DEFAULT 0,
`today_date` date NULL DEFAULT NULL,
`active_start` int unsigned NOT NULL DEFAULT 0,
`active_end` int unsigned NOT NULL DEFAULT 0,
`click_count` bigint unsigned NOT NULL DEFAULT 0,
`last_click_time` int unsigned NOT NULL DEFAULT 0,
`owner_admin_id` int unsigned NOT NULL DEFAULT 0,
`dept_id` int unsigned NOT NULL DEFAULT 0,
`remark` varchar(255) NOT NULL DEFAULT '',
`create_time` int unsigned NOT NULL DEFAULT 0,
`update_time` int unsigned NOT NULL DEFAULT 0,
`delete_time` int unsigned NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_remote_link_id` (`remote_link_id`),
KEY `idx_pool_eligible` (`pool_id`,`status`,`active_start`,`active_end`),
KEY `idx_account` (`account_id`),
KEY `idx_owner` (`owner_admin_id`),
KEY `idx_today` (`today_date`,`today_count`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='企微推广链接池';
CREATE TABLE IF NOT EXISTS `zyt_qywx_promotion_click_log` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`pool_id` int unsigned NOT NULL DEFAULT 0,
`link_id` int unsigned NOT NULL DEFAULT 0,
`source_url` varchar(500) NOT NULL DEFAULT '',
`referer` varchar(500) NOT NULL DEFAULT '',
`user_agent` varchar(500) NOT NULL DEFAULT '',
`ip_hash` char(64) NOT NULL DEFAULT '',
`click_date` date NOT NULL,
`create_time` int unsigned NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
KEY `idx_pool_date` (`pool_id`,`click_date`),
KEY `idx_link_date` (`link_id`,`click_date`),
KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='企微推广点击日志';
SET @first_visit_menu_id = (
SELECT `id` FROM `zyt_system_menu`
WHERE `pid` = 0 AND `type` = 'M'
AND (TRIM(`name`) = '一诊' OR LOWER(TRIM(`paths`)) IN ('first_visit', 'firstvisit', 'first-visit'))
ORDER BY CASE WHEN TRIM(`name`) = '一诊' AND TRIM(`paths`) = 'first_visit' THEN 0 ELSE 1 END, `id`
LIMIT 1
);
INSERT INTO `zyt_system_menu`
(`pid`, `type`, `name`, `icon`, `sort`, `perms`, `paths`, `component`, `selected`, `params`, `is_cache`, `is_show`, `is_disable`, `create_time`, `update_time`)
SELECT
@first_visit_menu_id, 'C', '企业微信获客助手', 'el-icon-Promotion', 60,
'firstvisit.wecomPromotion/overview', 'wecom_promotion', 'first_visit/wecom_promotion/index',
'', '', 0, 1, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
FROM DUAL
WHERE @first_visit_menu_id IS NOT NULL
AND NOT EXISTS (
SELECT 1 FROM `zyt_system_menu`
WHERE `perms` = 'firstvisit.wecomPromotion/overview'
OR (`pid` = @first_visit_menu_id AND (
TRIM(`name`) IN ('企业微信推广助手', '企业微信获客助手')
OR TRIM(`paths`) = 'wecom_promotion'
OR TRIM(`component`) = 'first_visit/wecom_promotion/index'
))
);
SET @wecom_promotion_menu_id = (
SELECT `id` FROM `zyt_system_menu`
WHERE `pid` = @first_visit_menu_id
AND (`perms` = 'firstvisit.wecomPromotion/overview'
OR TRIM(`name`) IN ('企业微信推广助手', '企业微信获客助手')
OR TRIM(`component`) = 'first_visit/wecom_promotion/index')
ORDER BY CASE WHEN `perms` = 'firstvisit.wecomPromotion/overview' THEN 0 ELSE 1 END, `id`
LIMIT 1
);
UPDATE `zyt_system_menu`
SET `name` = '企业微信获客助手',
`icon` = 'el-icon-Promotion',
`sort` = 60,
`perms` = 'firstvisit.wecomPromotion/overview',
`paths` = 'wecom_promotion',
`component` = 'first_visit/wecom_promotion/index',
`is_cache` = 0,
`is_show` = 1,
`is_disable` = 0,
`update_time` = UNIX_TIMESTAMP()
WHERE `id` = @wecom_promotion_menu_id;
-- 医助维护本人获客助手链接;组长/经理按部门范围管理;管理员全量管理。
INSERT IGNORE INTO `zyt_system_role_menu` (`role_id`, `menu_id`)
SELECT `id`, @first_visit_menu_id
FROM `zyt_system_role`
WHERE @first_visit_menu_id IS NOT NULL
AND `delete_time` IS NULL
AND `name` IN ('医助', '经理', '诊室组长', '管理员');
INSERT IGNORE INTO `zyt_system_role_menu` (`role_id`, `menu_id`)
SELECT `id`, @wecom_promotion_menu_id
FROM `zyt_system_role`
WHERE @wecom_promotion_menu_id IS NOT NULL
AND `delete_time` IS NULL
AND `name` IN ('医助', '经理', '诊室组长', '管理员');
COMMIT;
@@ -0,0 +1,15 @@
-- 已安装“企业微信获客助手”旧版数据表时执行一次。
-- 新安装环境直接执行 add_first_visit_wecom_promotion.sql,无需再执行本文件。
ALTER TABLE `zyt_qywx_promotion_link`
ADD COLUMN `remote_link_id` varchar(128) NULL DEFAULT NULL COMMENT '企业微信获客助手 link_id' AFTER `wecom_url`,
ADD COLUMN `remote_status` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '0历史手工 1远端有效 2远端已删除 3远端不可见 4同步失败' AFTER `remote_link_id`,
ADD COLUMN `remote_create_time` int unsigned NOT NULL DEFAULT 0 AFTER `remote_status`,
ADD COLUMN `range_user_json` text NULL COMMENT '获客链接可用成员 userid JSON' AFTER `remote_create_time`,
ADD COLUMN `range_department_json` text NULL COMMENT '获客链接可用企微部门 ID JSON' AFTER `range_user_json`,
ADD COLUMN `skip_verify` tinyint unsigned NOT NULL DEFAULT 0 AFTER `range_department_json`,
ADD COLUMN `priority_option_json` text NULL AFTER `skip_verify`,
ADD COLUMN `remote_snapshot` mediumtext NULL AFTER `priority_option_json`,
ADD COLUMN `last_sync_time` int unsigned NOT NULL DEFAULT 0 AFTER `remote_snapshot`,
ADD COLUMN `sync_error` varchar(500) NOT NULL DEFAULT '' AFTER `last_sync_time`,
ADD UNIQUE KEY `uk_remote_link_id` (`remote_link_id`);
@@ -0,0 +1,66 @@
-- 企业微信获客助手:客户归因、累计收消息数与回调重试审计
CREATE TABLE IF NOT EXISTS `zyt_qywx_customer_acquisition_customer` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`promotion_link_id` int unsigned NOT NULL DEFAULT 0 COMMENT '本地 qywx_promotion_link.id',
`link_id` varchar(128) NOT NULL DEFAULT '' COMMENT '企业微信获客链接 ID',
`external_userid` varchar(128) NOT NULL DEFAULT '',
`userid` varchar(128) NOT NULL DEFAULT '' COMMENT '接待成员企业微信 userid',
`owner_admin_id` int unsigned NOT NULL DEFAULT 0 COMMENT 'userid 对应后台管理员 ID,0 为未映射',
`dept_id` int unsigned NOT NULL DEFAULT 0,
`state` varchar(255) NOT NULL DEFAULT '',
`chat_status` tinyint unsigned NOT NULL DEFAULT 0,
`recv_msg_cnt` int unsigned NOT NULL DEFAULT 0 COMMENT 'get_chat_info 返回的累计值,只覆盖/max,不累加',
`message_count_known` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '1已在 ChatKey 有效期内取得精确累计数,0仅有聊天状态',
`first_acquired_time` int unsigned NOT NULL DEFAULT 0,
`last_chat_time` int unsigned NOT NULL DEFAULT 0,
`last_sync_time` int unsigned NOT NULL DEFAULT 0,
`raw_snapshot` mediumtext NULL,
`create_time` int unsigned NOT NULL DEFAULT 0,
`update_time` int unsigned NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_link_external_user` (`link_id`,`external_userid`,`userid`),
KEY `idx_owner_chat` (`owner_admin_id`,`chat_status`),
KEY `idx_dept` (`dept_id`),
KEY `idx_local_link` (`promotion_link_id`),
KEY `idx_last_chat` (`last_chat_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='企微获客客户归因与消息统计';
CREATE TABLE IF NOT EXISTS `zyt_qywx_customer_acquisition_event` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`event_key` char(64) NOT NULL COMMENT '回调稳定字段 SHA-256 幂等键',
`change_type` varchar(64) NOT NULL DEFAULT '',
`chat_key` varchar(512) NOT NULL DEFAULT '',
`link_id` varchar(128) NOT NULL DEFAULT '',
`external_userid` varchar(128) NOT NULL DEFAULT '',
`userid` varchar(128) NOT NULL DEFAULT '',
`status` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '0处理中 1成功 2待重试 3终态失败(过期或无效)',
`attempts` smallint unsigned NOT NULL DEFAULT 0,
`event_time` int unsigned NOT NULL DEFAULT 0,
`expire_time` int unsigned NOT NULL DEFAULT 0 COMMENT 'ChatKey 硬截止时间',
`next_retry` int unsigned NOT NULL DEFAULT 0,
`error_message` varchar(1000) NOT NULL DEFAULT '',
`raw_json` mediumtext NULL,
`create_time` int unsigned NOT NULL DEFAULT 0,
`update_time` int unsigned NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_event_key` (`event_key`),
KEY `idx_retry` (`status`,`next_retry`,`expire_time`),
KEY `idx_chat_key` (`chat_key`(191)),
KEY `idx_event_time` (`event_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='企微获客回调幂等与失败重试审计';
-- 依赖系统层每分钟执行 php think crontab;本任务自身也按每分钟调度,确保不浪费 ChatKey 30 分钟窗口。
INSERT INTO `zyt_dev_crontab` (
`name`, `type`, `system`, `remark`, `command`, `params`,
`status`, `expression`, `create_time`, `update_time`
)
SELECT
'重试企微获客会话回调', 1, 0,
'重试 customer_start_chat/message_from_customer 的 get_chat_info30 分钟后明确 failed_expired',
'qywx:retry-customer-acquisition-events', '', 1, '* * * * *', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
FROM DUAL
WHERE NOT EXISTS (
SELECT 1 FROM `zyt_dev_crontab`
WHERE `command` = 'qywx:retry-customer-acquisition-events' AND `delete_time` IS NULL
);