#!/bin/bash # ======================================== # 教务系统 - 智能产业启动脚本 # 功能:自动检测依赖、安装、启动服务 # ======================================== # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # 产业配置 declare -A industries=( ["1"]="frontend:文旅产业:5150" ["2"]="frontend_智能制造:智能制造:5151" ["3"]="frontend_智能开发:智能开发:5152" ["4"]="frontend_财经商贸:财经商贸:5153" ["5"]="frontend_视觉设计:视觉设计:5154" ["6"]="frontend_交通物流:交通物流:5155" ["7"]="frontend_大健康:大健康:5156" ["8"]="frontend_土木水利:土木水利:5157" ["9"]="frontend_食品:食品产业:5158" ["10"]="frontend_化工:化工产业:5159" ["11"]="frontend_能源:能源产业:5160" ["12"]="frontend_环保:环保产业:5161" ) # 打印标题 print_header() { clear echo -e "${BLUE}========================================${NC}" echo -e "${BLUE} 教务系统 - 智能启动脚本 ${NC}" echo -e "${BLUE}========================================${NC}" echo "" } # 检查端口是否被占用 check_port() { local port=$1 if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null 2>&1; then return 0 # 端口被占用 else return 1 # 端口空闲 fi } # 停止指定端口的服务 stop_port() { local port=$1 local pid=$(lsof -Pi :$port -sTCP:LISTEN -t 2>/dev/null) if [ ! -z "$pid" ]; then echo -e "${YELLOW}⚠️ 端口 $port 被占用,正在停止进程 $pid...${NC}" kill $pid 2>/dev/null sleep 1 echo -e "${GREEN}✅ 已停止端口 $port 的服务${NC}" fi } # 检查并安装依赖 check_and_install_deps() { local dir=$1 local name=$2 if [ ! -d "$dir/node_modules" ]; then echo -e "${YELLOW}📦 检测到 $name 依赖未安装,正在安装...${NC}" echo -e "${BLUE} 这可能需要1-3分钟,请稍候...${NC}" cd "$dir" npm install > /dev/null 2>&1 if [ $? -eq 0 ]; then echo -e "${GREEN}✅ $name 依赖安装成功!${NC}" cd - > /dev/null return 0 else echo -e "${RED}❌ $name 依赖安装失败!${NC}" cd - > /dev/null return 1 fi else echo -e "${GREEN}✓ $name 依赖已存在${NC}" return 0 fi } # 启动单个产业 start_industry() { local dir=$1 local name=$2 local port=$3 echo "" echo -e "${BLUE}🚀 启动 $name (端口: $port)${NC}" echo "----------------------------------------" # 检查目录是否存在 if [ ! -d "$dir" ]; then echo -e "${RED}❌ 目录 $dir 不存在!${NC}" return 1 fi # 检查端口 if check_port $port; then echo -e "${YELLOW}⚠️ 端口 $port 已被占用${NC}" read -p "是否停止现有服务并重启?(y/n): " choice if [[ "$choice" == "y" || "$choice" == "Y" ]]; then stop_port $port else echo -e "${YELLOW}跳过 $name${NC}" return 1 fi fi # 检查并安装依赖 check_and_install_deps "$dir" "$name" if [ $? -ne 0 ]; then return 1 fi # 启动服务 echo -e "${BLUE}🔄 正在启动 $name...${NC}" cd "$dir" npm run dev > /dev/null 2>&1 & local pid=$! cd - > /dev/null # 等待服务启动 sleep 3 # 验证启动 if check_port $port; then echo -e "${GREEN}✅ $name 启动成功!${NC}" echo -e "${GREEN} 访问地址: http://localhost:$port${NC}" return 0 else echo -e "${RED}❌ $name 启动失败!${NC}" return 1 fi } # 启动所有产业 start_all() { echo -e "${BLUE}启动所有产业...${NC}" echo "" local success_count=0 local fail_count=0 for key in "${!industries[@]}"; do IFS=':' read -r dir name port <<< "${industries[$key]}" start_industry "$dir" "$name" "$port" if [ $? -eq 0 ]; then ((success_count++)) else ((fail_count++)) fi done echo "" echo "========================================" echo -e "${GREEN}成功启动: $success_count 个产业${NC}" if [ $fail_count -gt 0 ]; then echo -e "${RED}启动失败: $fail_count 个产业${NC}" fi } # 停止所有产业 stop_all() { echo -e "${YELLOW}停止所有产业服务...${NC}" for key in "${!industries[@]}"; do IFS=':' read -r dir name port <<< "${industries[$key]}" if check_port $port; then stop_port $port fi done echo -e "${GREEN}✅ 所有服务已停止${NC}" } # 显示菜单 show_menu() { echo -e "${BLUE}请选择操作:${NC}" echo "" echo " 0) 启动所有产业" echo " --------------" for key in $(echo "${!industries[@]}" | tr ' ' '\n' | sort -n); do IFS=':' read -r dir name port <<< "${industries[$key]}" printf " %2s) %-12s (端口: %s)" "$key" "$name" "$port" # 检查运行状态 if check_port $port; then echo -e " ${GREEN}[运行中]${NC}" else echo -e " ${YELLOW}[未启动]${NC}" fi done echo " --------------" echo " a) 停止所有产业" echo " s) 查看运行状态" echo " q) 退出" echo "" } # 显示运行状态 show_status() { echo "" echo -e "${BLUE}产业运行状态:${NC}" echo "========================================" local running_count=0 local stopped_count=0 for key in $(echo "${!industries[@]}" | tr ' ' '\n' | sort -n); do IFS=':' read -r dir name port <<< "${industries[$key]}" printf "%-15s (端口: %s): " "$name" "$port" if check_port $port; then echo -e "${GREEN}运行中${NC}" echo -e " 访问地址: http://localhost:$port" ((running_count++)) else echo -e "${YELLOW}未启动${NC}" ((stopped_count++)) fi done echo "========================================" echo -e "${GREEN}运行中: $running_count${NC} | ${YELLOW}未启动: $stopped_count${NC}" echo "" } # 主函数 main() { # 检查是否有参数传入(快速启动模式) if [ $# -gt 0 ]; then case "$1" in all) print_header start_all ;; stop) print_header stop_all ;; status) print_header show_status ;; [0-9]|1[0-2]) print_header if [ "$1" == "0" ]; then start_all else IFS=':' read -r dir name port <<< "${industries[$1]}" start_industry "$dir" "$name" "$port" fi ;; *) echo -e "${RED}无效参数!${NC}" echo "用法: $0 [all|stop|status|1-12]" ;; esac exit 0 fi # 交互模式 while true; do print_header show_menu read -p "请输入选项: " choice case "$choice" in 0) start_all read -p "按回车键继续..." ;; [1-9]|1[0-2]) IFS=':' read -r dir name port <<< "${industries[$choice]}" start_industry "$dir" "$name" "$port" read -p "按回车键继续..." ;; a|A) stop_all read -p "按回车键继续..." ;; s|S) show_status read -p "按回车键继续..." ;; q|Q) echo -e "${BLUE}再见!${NC}" exit 0 ;; *) echo -e "${RED}无效选项!${NC}" sleep 1 ;; esac done } # 启动主程序 main "$@"