This commit is contained in:
chaosBreaking
2019-08-31 14:59:13 +08:00
commit dc5423d356
65 changed files with 3813 additions and 0 deletions

View File

@@ -0,0 +1,88 @@
'use strict';
const mongoose = require('mongoose');
const ASSETTYPES = ['log', 'profit', 'usdtBTC', 'usdtETH', 'usdtTRX'];
const UserSchema = new mongoose.Schema({
phone: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
nickname: {
type: String,
required: false
},
inviter: {
type: String,
},
gender: {
type: String
},
asset: {
type: {},
default: {
log: 0, //充值的log
profit: 0 // 买卖地皮赚到的
}
},
tokenAddress: {
type: {},
default: {
'usdtBTC': '',
'usdtETH': '',
'usdtTRX': ''
}
},
avatar: {
type: String
},
option: {
type: {}
},
group: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}]
}, {
timestamps: {
createdAt: 'createdAt',
updatedAt: 'updatedAt'
}
});
const UserModel = mongoose.model('User', UserSchema);
UserModel.prototype.increase = async function (assetName, amount, option = {}) {
if (!assetName || !amount || amount <= 0 || !Number.isFinite(amount) || !ASSETTYPES.includes(assetName)) return null;
const asset = 'asset.' + assetName;
const res = await UserModel.findByIdAndUpdate(this.id, {
$inc: {
[asset]: amount
}
}, { ...option, returnOriginal: false }).exec().catch(err => null);
if (!res || !res.asset) throw new Error('用户资产修改[increase]失败!');
if (res.asset[assetName] < 0) throw new Error('Insufficient funds');
return res;
};
UserModel.prototype.decrease = async function (assetName, amount, option) {
// 传入的amount应该是大于0的正数
if (!assetName || !amount || amount <= 0 || !Number.isFinite(amount) || !ASSETTYPES.includes(assetName)) return null;
amount = 0 - amount;
const asset = 'asset.' + assetName;
const res = await UserModel.findByIdAndUpdate(this.id, {
$inc: {
[asset]: amount
}
}, { ...option, returnOriginal: false }).exec().catch(err => null);
if (!res || !res.asset) throw new Error('用户资产修改[decrease]失败!');
if (res.asset[assetName] < 0) throw new Error('Insufficient funds');
return res;
};
module.exports = UserModel;