Files
yunzerwebsiteallinone/go/docs/sql/add_module_platforms.sql
T
2026-10-03 23:46:55 +08:00

90 lines
4.3 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- =============================================================
-- 模块 / 菜单「展示端(platforms)」
--
-- 需求:后台管理模块层面需要能判断一个模块(及其菜单)
-- 是 Web 端显示、小程序显示,还是两端都显示。
--
-- 与已有的 views 字段是两个独立维度,不要混淆:
-- views —— [1]平台端 / [2]租户端(决定出现在哪个后台)
-- platforms —— ["web"] / ["app"] / ["web","app"](决定 Web 还是移动端可见)
--
-- 取值约定:
-- ["web"] 仅 Web 端(租户管理后台)可见
-- ["app"] 仅移动端(uniapp APP / 微信小程序)可见
-- ["web","app"] 两端都可见
-- '' 或 NULL 全端可见(默认,保证历史数据行为不变)
--
-- 继承规则:
-- 菜单自身配置了 platforms → 以自身为准
-- 菜单未配置 → 继承所属模块的 platforms(按 path 前缀归属)
-- 两者都未配置 → 全端可见
--
-- 本脚本可重复执行:MySQL 不支持 ADD COLUMN IF NOT EXISTS,
-- 故先用 information_schema 判断,再由动态 SQL 决定加不加列。
-- 若已存在则只更新注释,不会报 1060 Duplicate column name。
-- =============================================================
-- ---------- yz_system_modules ----------
SET @db := DATABASE();
SET @tbl := 'yz_system_modules';
SET @col := 'platforms';
SET @exists := (
SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = @tbl AND COLUMN_NAME = @col
);
SET @sql := IF(@exists = 0,
CONCAT('ALTER TABLE `', @tbl, '` ADD COLUMN `', @col,
'` VARCHAR(64) NULL DEFAULT NULL COMMENT ''展示端:web/app JSON数组;空或NULL=全端'' AFTER `is_show`'),
CONCAT('ALTER TABLE `', @tbl, '` MODIFY COLUMN `', @col,
'` VARCHAR(64) NULL DEFAULT NULL COMMENT ''展示端:web/app JSON数组;空或NULL=全端''')
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- ---------- yz_system_menu ----------
SET @tbl := 'yz_system_menu';
SET @exists := (
SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = @tbl AND COLUMN_NAME = @col
);
SET @sql := IF(@exists = 0,
CONCAT('ALTER TABLE `', @tbl, '` ADD COLUMN `', @col,
'` VARCHAR(64) NULL DEFAULT NULL COMMENT ''展示端:web/app JSON数组;空或NULL=继承所属模块'' AFTER `views`'),
CONCAT('ALTER TABLE `', @tbl, '` MODIFY COLUMN `', @col,
'` VARCHAR(64) NULL DEFAULT NULL COMMENT ''展示端:web/app JSON数组;空或NULL=继承所属模块''')
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- ---------- 校验 ----------
SELECT TABLE_NAME, COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, COLUMN_COMMENT
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE() AND COLUMN_NAME = 'platforms'
AND TABLE_NAME IN ('yz_system_modules', 'yz_system_menu');
-- =============================================================
-- 使用示例(按需执行)
-- 注意:MySQL 5.7+ 可直接用 JSON_ARRAY('web') 生成 '["web"]',
-- 比手写转义双引号更省事。
-- =============================================================
-- 例1:把「客户关系管理」设为仅 Web 端可见(移动端不显示该子系统卡片)
-- UPDATE yz_system_modules SET platforms = JSON_ARRAY('web') WHERE code = 'crm';
-- 例2:把「便捷工具」设为仅移动端可见(Web 端后台不再展示)
-- UPDATE yz_system_modules SET platforms = JSON_ARRAY('app') WHERE code = 'bianjiegongju';
-- 例3:某功能两端都要
-- UPDATE yz_system_modules SET platforms = JSON_ARRAY('web','app') WHERE code = 'cms';
-- 例4:某个具体功能只在小程序上用(如扫码打卡),单独给菜单配置
-- UPDATE yz_system_menu SET platforms = JSON_ARRAY('app') WHERE path = '/tools/notebook';
-- 例5:恢复全端可见(清空即可,NULL 与 '' 都表示全端)
-- UPDATE yz_system_modules SET platforms = NULL WHERE code = 'crm';
-- UPDATE yz_system_modules SET platforms = NULL; -- 全部恢复
-- =============================================================
-- 回滚
-- =============================================================
-- ALTER TABLE yz_system_modules DROP COLUMN platforms;
-- ALTER TABLE yz_system_menu DROP COLUMN platforms;