|
|
@@ -11,8 +11,29 @@ class AddAccount extends API {
|
|
|
this.setMethod('POST')
|
|
|
}
|
|
|
|
|
|
+ // 生成 6 位数字 + 字母混合码
|
|
|
+ async generateCode() {
|
|
|
+ try {
|
|
|
+ const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
+ let code = ""
|
|
|
+ for (let i = 0; i < 6; i++) {
|
|
|
+ code += chars.charAt(Math.floor(Math.random() * chars.length))
|
|
|
+ }
|
|
|
+
|
|
|
+ let sql = 'SELECT id FROM lepao_face WHERE face_code = ?'
|
|
|
+ let rows = await db.query(sql, [code])
|
|
|
+ if (!rows)
|
|
|
+ throw new Error('数据库错误,请稍后再试')
|
|
|
+ if (rows.length > 0)
|
|
|
+ return await this.generateCode()
|
|
|
+ return code
|
|
|
+ } catch (error) {
|
|
|
+ throw error
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
async onRequest(req, res) {
|
|
|
- let { uuid, session, student_num, email, id, area, max_distance, min_distance, auto_time, auto_run, notes } = req.body
|
|
|
+ let { uuid, session, student_num, email, id, area, auto_time, auto_run, notes } = req.body
|
|
|
|
|
|
if ([uuid, session, student_num, email, auto_time].some(value => value === '' || value === null || value === undefined))
|
|
|
return res.json({
|
|
|
@@ -50,8 +71,15 @@ class AddAccount extends API {
|
|
|
r = await db.query(sql, [uuid, email, area, auto_time, auto_run, time, notes ?? '', countRows[0].id])
|
|
|
}
|
|
|
else {
|
|
|
+ const face_code = await this.generateCode()
|
|
|
+
|
|
|
sql = 'INSERT INTO lepao_account (student_num, email, area, auto_time, auto_run, create_user, create_time, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?)'
|
|
|
r = await db.query(sql, [student_num, email, area, auto_time, auto_run, uuid, time, notes ?? ''])
|
|
|
+
|
|
|
+ let faceSql = 'INSERT INTO lepao_face (student_num, face_code) VALUES (?, ?)'
|
|
|
+ let faceRows = await db.query(faceSql, [student_num, face_code])
|
|
|
+ if (!faceRows || faceRows.affectedRows !== 1)
|
|
|
+ return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
|
|
|
}
|
|
|
} else {
|
|
|
sql = 'UPDATE lepao_account SET student_num = ?, email = ?, area = ?, auto_time = ?, auto_run = ?, notes = ? WHERE id = ?'
|