来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
131 lines
4.6 KiB
TypeScript
131 lines
4.6 KiB
TypeScript
import { randomUUID } from 'crypto';
|
|
import { pool } from '../db';
|
|
import { generateInitialPassword, initialPasswordFromPhone, hashPassword } from '../utils/password';
|
|
import { findAcrossActiveTenants, runInBranchSchema } from './tenantSchemaFallback';
|
|
|
|
/**
|
|
* 解析新建关联账号的初始密码:未提供且为手机号登录时默认用手机号后 6 位,否则随机生成。
|
|
*/
|
|
function resolveInitialPassword(provided?: string, phoneLogin?: string): string {
|
|
const password = String(provided || '').trim();
|
|
if (!password) {
|
|
if (phoneLogin) return initialPasswordFromPhone(phoneLogin);
|
|
return generateInitialPassword();
|
|
}
|
|
return password;
|
|
}
|
|
|
|
async function assertLoginNameAvailable(username: string, email: string) {
|
|
const existing = await pool.query(
|
|
`SELECT 1 FROM public.profiles WHERE username = $1
|
|
UNION ALL
|
|
SELECT 1 FROM auth.users WHERE email = $2
|
|
LIMIT 1`,
|
|
[username, email]
|
|
);
|
|
if (existing.rowCount) {
|
|
throw Object.assign(new Error('该登录名已被使用'), { code: 'login_name_taken' });
|
|
}
|
|
}
|
|
|
|
export async function createDeliveryPersonLoginAccount(params: {
|
|
personCode: string;
|
|
loginName: string;
|
|
password?: string;
|
|
branchCode?: string | null;
|
|
}) {
|
|
const loginName = String(params.loginName || '').trim();
|
|
if (!loginName) {
|
|
throw Object.assign(new Error('请填写登录名'), { code: 'invalid_login_name' });
|
|
}
|
|
|
|
const located =
|
|
params.branchCode
|
|
? await runInBranchSchema(params.branchCode, async () => {
|
|
const person = await pool.query(
|
|
`SELECT person_code, person_name, phone, branch_code FROM delivery_persons WHERE person_code = $1 LIMIT 1`,
|
|
[params.personCode]
|
|
);
|
|
if (!person.rowCount) return null;
|
|
return {
|
|
branch_code: String(params.branchCode),
|
|
row: person.rows[0] as Record<string, unknown>,
|
|
};
|
|
})
|
|
: await findAcrossActiveTenants(
|
|
`SELECT person_code, person_name, phone, branch_code FROM delivery_persons WHERE person_code = $1 LIMIT 1`,
|
|
[params.personCode]
|
|
);
|
|
|
|
if (!located) {
|
|
throw Object.assign(new Error('配送员不存在'), { code: 'not_found' });
|
|
}
|
|
const row = located.row;
|
|
const branchCode =
|
|
params.branchCode ?? (row.branch_code as string | null) ?? located.branch_code ?? null;
|
|
if (!branchCode) {
|
|
throw Object.assign(new Error('配送员未分配分公司'), { code: 'invalid_branch' });
|
|
}
|
|
|
|
const phoneForPassword =
|
|
/^1[3-9]\d{9}$/.test(loginName) ? loginName : String(row.phone || '').trim() || undefined;
|
|
const password = resolveInitialPassword(params.password, phoneForPassword);
|
|
|
|
return runInBranchSchema(branchCode, async () => {
|
|
const existingLink = await pool.query(
|
|
`SELECT user_id, username FROM delivery_person_users WHERE person_code = $1 LIMIT 1`,
|
|
[params.personCode]
|
|
);
|
|
if (existingLink.rowCount) {
|
|
throw Object.assign(new Error('该配送员已绑定登录账号'), { code: 'account_exists' });
|
|
}
|
|
|
|
const email = `${loginName}@delivery.meoo.local`;
|
|
await assertLoginNameAvailable(loginName, email);
|
|
|
|
const userId = randomUUID();
|
|
const passwordHash = await hashPassword(password);
|
|
const fullName = String(row.person_name || loginName).trim() || loginName;
|
|
const client = await pool.connect();
|
|
|
|
try {
|
|
await client.query('BEGIN');
|
|
await client.query(
|
|
`INSERT INTO auth.users (
|
|
instance_id, id, aud, role, email, encrypted_password, email_confirmed_at,
|
|
raw_app_meta_data, raw_user_meta_data
|
|
) VALUES (
|
|
'00000000-0000-0000-0000-000000000000', $1, 'authenticated', 'authenticated', $2,
|
|
$3, NOW(),
|
|
'{"provider":"email","providers":["email"]}'::jsonb, $4::jsonb
|
|
)`,
|
|
[userId, email, passwordHash, JSON.stringify({ username: loginName, full_name: fullName })]
|
|
);
|
|
await client.query(
|
|
`INSERT INTO public.profiles (id, username, full_name, phone, branch_code, status, must_change_password)
|
|
VALUES ($1, $2, $3, $4, $5, 1, true)`,
|
|
[userId, loginName, fullName, row.phone || null, branchCode]
|
|
);
|
|
await client.query(
|
|
`INSERT INTO delivery_person_users (user_id, person_code, username, status, is_primary)
|
|
VALUES ($1, $2, $3, 1, true)`,
|
|
[userId, params.personCode, loginName]
|
|
);
|
|
await client.query('COMMIT');
|
|
return {
|
|
user_id: userId,
|
|
login_name: loginName,
|
|
default_password: password,
|
|
created: true,
|
|
};
|
|
} catch (error) {
|
|
await client.query('ROLLBACK');
|
|
throw error;
|
|
} finally {
|
|
client.release();
|
|
}
|
|
});
|
|
}
|
|
|
|
|