2023-05-08 05:11:29 +08:00
|
|
|
|
import BaseModel from './BaseModel.js'
|
2023-05-09 11:03:38 +08:00
|
|
|
|
import lodash from 'lodash'
|
2023-05-10 05:55:21 +08:00
|
|
|
|
import { UserGameDB } from './index.js'
|
|
|
|
|
import MysUtil from '../mys/MysUtil.js'
|
2023-05-08 05:11:29 +08:00
|
|
|
|
|
|
|
|
|
const { Types } = BaseModel
|
|
|
|
|
|
|
|
|
|
const COLUMNS = {
|
|
|
|
|
// 用户ID,qq为数字
|
|
|
|
|
id: {
|
|
|
|
|
type: Types.STRING,
|
|
|
|
|
autoIncrement: false,
|
|
|
|
|
primaryKey: true
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
type: {
|
|
|
|
|
type: Types.STRING,
|
|
|
|
|
defaultValue: 'qq',
|
|
|
|
|
notNull: true
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 昵称
|
|
|
|
|
name: Types.STRING,
|
|
|
|
|
|
|
|
|
|
// 头像
|
|
|
|
|
face: Types.STRING,
|
|
|
|
|
|
2023-06-04 05:03:50 +08:00
|
|
|
|
ltuids: Types.STRING,
|
|
|
|
|
games: {
|
|
|
|
|
type: Types.STRING,
|
|
|
|
|
get () {
|
|
|
|
|
let data = this.getDataValue('games')
|
|
|
|
|
let ret = {}
|
|
|
|
|
try {
|
|
|
|
|
data = JSON.parse(data) || {}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
data = {}
|
|
|
|
|
}
|
|
|
|
|
MysUtil.eachGame((game) => {
|
|
|
|
|
let ds = data[game] || {}
|
|
|
|
|
ret[game] = {
|
|
|
|
|
uid: ds.uid || '',
|
|
|
|
|
data: ds.data || {}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return ret
|
|
|
|
|
},
|
|
|
|
|
set (data) {
|
|
|
|
|
this.setDataValue('games', JSON.stringify(data))
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
data: Types.STRING
|
2023-05-08 05:11:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class UserDB extends BaseModel {
|
|
|
|
|
static async find (id, type = 'qq') {
|
|
|
|
|
// user_id
|
|
|
|
|
id = type === 'qq' ? '' + id : type + id
|
|
|
|
|
// DB查询
|
2023-06-04 05:03:50 +08:00
|
|
|
|
let user = await UserDB.findByPk(id)
|
2023-05-08 05:11:29 +08:00
|
|
|
|
if (!user) {
|
|
|
|
|
user = await UserDB.build({
|
|
|
|
|
id,
|
|
|
|
|
type
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return user
|
|
|
|
|
}
|
2023-05-09 11:03:38 +08:00
|
|
|
|
|
|
|
|
|
async saveDB (user) {
|
|
|
|
|
let db = this
|
|
|
|
|
let ltuids = []
|
|
|
|
|
lodash.forEach(user.mysUsers, (mys) => {
|
2023-05-10 05:55:21 +08:00
|
|
|
|
if (mys.ck && mys.ltuid) {
|
2023-05-09 11:03:38 +08:00
|
|
|
|
ltuids.push(mys.ltuid)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
db.ltuids = ltuids.join(',')
|
2023-06-29 04:18:38 +08:00
|
|
|
|
let games = {}
|
|
|
|
|
lodash.forEach(user._games, (gameDs, game) => {
|
|
|
|
|
games[game] = {
|
|
|
|
|
uid: gameDs.uid,
|
|
|
|
|
data: {}
|
|
|
|
|
}
|
|
|
|
|
lodash.forEach(gameDs.data, (ds, uid) => {
|
|
|
|
|
games[game].data[uid] = {
|
|
|
|
|
uid: ds.uid,
|
|
|
|
|
type: ds.type
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
db.games = games
|
2023-05-09 11:03:38 +08:00
|
|
|
|
await this.save()
|
|
|
|
|
}
|
2023-05-08 05:11:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BaseModel.initDB(UserDB, COLUMNS)
|
2023-06-04 05:03:50 +08:00
|
|
|
|
await UserDB.sync({ alter: true })
|
2023-05-08 05:11:29 +08:00
|
|
|
|
|
|
|
|
|
export default UserDB
|