From c8665001207864f321c00701bdc611dc9bb21e0f Mon Sep 17 00:00:00 2001 From: kuaifan Date: Fri, 7 Mar 2025 16:18:06 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/publish.yml | 4 +- cmd | 11 +++-- docker/es/data/.gitignore | 0 docker/mysql/repassword.sh | 86 ++++++++++++++++++++++++++++++++--- 4 files changed, 87 insertions(+), 14 deletions(-) mode change 100644 => 100755 docker/es/data/.gitignore diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index ac170ea93..2c339f08e 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -299,7 +299,7 @@ jobs: env: PUBLISH_KEY: ${{ secrets.PUBLISH_KEY }} run: | - pushd electron + pushd electron || exit npm install - popd + popd || exit node ./electron/build.js published diff --git a/cmd b/cmd index 36a237fd7..58f00fbbc 100755 --- a/cmd +++ b/cmd @@ -156,9 +156,9 @@ run_electron() { npm install fi if [ ! -d "./electron/node_modules" ]; then - pushd electron + pushd electron || exit npm install - popd + popd || exit fi # if [ -d "./electron/dist" ]; then @@ -178,8 +178,9 @@ run_electron() { run_exec() { local container=$1 - local cmd=$2 - local name=`docker_name $container` + shift 1 + local cmd=$@ + local name=$(docker_name "$container") if [ -z "$name" ]; then error "没有找到 $container 容器!" exit 1 @@ -561,7 +562,7 @@ if [ $# -gt 0 ]; then success "修改成功" elif [[ "$1" == "repassword" ]]; then shift 1 - run_exec mariadb "sh /etc/mysql/repassword.sh \"$@\"" + run_exec mariadb "sh /etc/mysql/repassword.sh $@" elif [[ "$1" == "serve" ]] || [[ "$1" == "dev" ]] || [[ "$1" == "development" ]]; then shift 1 run_compile dev diff --git a/docker/es/data/.gitignore b/docker/es/data/.gitignore old mode 100644 new mode 100755 diff --git a/docker/mysql/repassword.sh b/docker/mysql/repassword.sh index 645629604..72a547501 100644 --- a/docker/mysql/repassword.sh +++ b/docker/mysql/repassword.sh @@ -1,27 +1,99 @@ #!/bin/sh +# +# 重置用户密码脚本 +# +# 使用方法: +# ./repassword.sh [账号标识符] [自定义密码] +# +# 参数说明: +# [账号标识符]: 可选,可以是用户ID(纯数字)或邮箱地址。不提供时默认为第一个管理员用户 +# [自定义密码]: 可选,指定要设置的新密码。不提供时会自动生成随机密码 +# +# 使用示例: +# ./repassword.sh # 重置第一个管理员用户密码(随机生成) +# ./repassword.sh 123 # 重置ID=123的用户密码(随机生成) +# ./repassword.sh user@example.com # 重置邮箱为user@example.com的用户密码(随机生成) +# ./repassword.sh 123 newpass # 重置ID=123的用户密码为"newpass" +# ./repassword.sh user@example.com newpass # 重置邮箱为user@example.com的用户密码为"newpass" +# -new_password=$1 +account_identifier=$1 +custom_password=$2 GreenBG="\033[42;37m" +RedBG="\033[41;37m" Font="\033[0m" +# 生成随机密码 new_encrypt=$(date +%s%N | md5sum | awk '{print $1}' | cut -c 1-6) -if [ -z "$new_password" ]; then +if [ -z "$custom_password" ]; then new_password=$(date +%s%N | md5sum | awk '{print $1}' | cut -c 1-16) +else + new_password=$custom_password fi md5_password=$(echo -n `echo -n $new_password | md5sum | awk '{print $1}'`$new_encrypt | md5sum | awk '{print $1}') -content=$(echo "select \`email\` from ${MYSQL_PREFIX}users where \`userid\`=1;" | mysql -u$MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE) -account=$(echo "$content" | sed -n '2p') +# 构建查询条件 +if [ -z "$account_identifier" ]; then + # 默认查询第一个管理员 + admin_query=$(echo "SELECT userid FROM ${MYSQL_PREFIX}users WHERE identity LIKE '%,admin,%' ORDER BY userid LIMIT 1;" | mysql -u$MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE) + identifier_value=$(echo "$admin_query" | sed -n '2p') + + if [ -z "$identifier_value" ]; then + echo "${RedBG}错误:未找到管理员用户!${Font}" + exit 1 + fi + + where_field="userid" + identifier_type="管理员ID" +else + # 检查是否为纯数字(ID) + # 使用更兼容的 shell 语法检查是否为纯数字 + case "$account_identifier" in + ''|*[!0-9]*) + # 非纯数字,视为邮箱 + where_field="email" + identifier_type="邮箱" + identifier_value="$account_identifier" + ;; + *) + # 纯数字,视为ID + where_field="userid" + identifier_type="ID" + identifier_value="$account_identifier" + ;; + esac +fi + +# 构建 WHERE 条件(为邮箱添加引号) +if [ "$where_field" = "email" ]; then + where_condition="where $where_field='$identifier_value'" +else + where_condition="where $where_field=$identifier_value" +fi + +# 查询用户信息 +content=$(echo "select userid,email from ${MYSQL_PREFIX}users $where_condition;" | mysql -u$MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE) + +# 提取用户ID和邮箱 +user_id=$(echo "$content" | sed -n '2p' | awk '{print $1}') +account=$(echo "$content" | sed -n '2p' | awk '{print $2}') if [ -z "$account" ]; then - echo "错误:账号不存在!" + echo "${RedBG}错误:${identifier_type} '${identifier_value}' 的账号不存在!${Font}" exit 1 fi +# 更新密码 mysql -u$MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE <