Skip to content

Commit

Permalink
Repair of invitations for transfers
Browse files Browse the repository at this point in the history
  • Loading branch information
luxlzz6 committed Jan 10, 2024
1 parent e4da848 commit e130e8a
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
TZ=Asia/Shanghai
TZ=Asia/Shanghai
SQL_DSN=chatapi:MeyeFpcbNGZXMSWJ@tcp(localhost:3306)/chatapi
Binary file added chat-api
Binary file not shown.
2 changes: 2 additions & 0 deletions common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ var Zfb = true
var DrawingEnabled = true
var DataExportEnabled = true
var DataExportInterval = 5 // unit: minute
var MiniQuota = 1.0
var ProporTions = 10
var UserGroup = "default"
var VipUserGroup = "default"

Expand Down
2 changes: 1 addition & 1 deletion controller/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func GetOptions(c *gin.Context) {
func GetUserOptions(c *gin.Context) {
var options []*model.Option
common.OptionMapRWMutex.RLock() // 使用读锁
keys := []string{"TopUpLink", "YzfZfb", "YzfWx", "BillingByRequestEnabled", "ModelRatioEnabled"}
keys := []string{"TopUpLink", "YzfZfb", "YzfWx", "BillingByRequestEnabled", "ModelRatioEnabled", "MiniQuota", "ProporTions"}

for _, key := range keys {
if value, exists := common.OptionMap[key]; exists {
Expand Down
3 changes: 3 additions & 0 deletions controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func Logout(c *gin.Context) {
}

func Register(c *gin.Context) {

if !common.RegisterEnabled {
c.JSON(http.StatusOK, gin.H{
"message": "管理员关闭了新用户注册",
Expand All @@ -131,6 +132,7 @@ func Register(c *gin.Context) {
})
return
}

if err := common.Validate.Struct(&user); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
Expand Down Expand Up @@ -180,6 +182,7 @@ func Register(c *gin.Context) {
CreatedAt: time.Now().Unix(),
Group: UserGroup,
}

if common.EmailVerificationEnabled {
cleanUser.Email = user.Email
}
Expand Down
6 changes: 6 additions & 0 deletions model/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ func InitOptionMap() {
common.OptionMap["DataExportInterval"] = strconv.Itoa(common.DataExportInterval)
common.OptionMap["UserGroup"] = common.UserGroup
common.OptionMap["VipUserGroup"] = common.VipUserGroup
common.OptionMap["MiniQuota"] = strconv.FormatFloat(common.MiniQuota, 'f', -1, 64)
common.OptionMap["ProporTions"] = strconv.Itoa(common.ProporTions)

common.OptionMapRWMutex.Unlock()
loadOptionsFromDatabase()
Expand Down Expand Up @@ -216,6 +218,8 @@ func updateOptionMap(key string, value string) (err error) {
common.EpayKey = value
case "Price":
common.Price, _ = strconv.ParseFloat(value, 64)
case "MiniQuota":
common.MiniQuota, _ = strconv.ParseFloat(value, 64)
case "TopupGroupRatio":
err = common.UpdateTopupGroupRatioByJSONString(value)
case "GitHubClientId":
Expand Down Expand Up @@ -254,6 +258,8 @@ func updateOptionMap(key string, value string) (err error) {
common.RetryTimes, _ = strconv.Atoi(value)
case "DataExportInterval":
common.DataExportInterval, _ = strconv.Atoi(value)
case "ProporTions":
common.ProporTions, _ = strconv.Atoi(value)
case "ModelRatio":
err = common.UpdateModelRatioByJSONString(value)
case "ModelPrice":
Expand Down
11 changes: 4 additions & 7 deletions model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,7 @@ func inviteUser(inviterId int) (err error) {
}

func (user *User) TransferAffQuotaToQuota(quota int) error {
// 检查quota是否小于最小额度
if float64(quota) < common.QuotaPerUnit {
return fmt.Errorf("转移额度最小为%s!", common.LogQuota(int(common.QuotaPerUnit)))
}
transferAmount := int(float64(quota) * common.QuotaPerUnit)

// 开始数据库事务
tx := DB.Begin()
Expand All @@ -147,13 +144,13 @@ func (user *User) TransferAffQuotaToQuota(quota int) error {
}

// 再次检查用户的AffQuota是否足够
if user.AffQuota < quota {
if user.AffQuota < transferAmount {
return errors.New("邀请额度不足!")
}

// 更新用户额度
user.AffQuota -= quota
user.Quota += quota
user.AffQuota -= transferAmount
user.Quota += transferAmount

// 保存用户状态
if err := tx.Save(user).Error; err != nil {
Expand Down
32 changes: 30 additions & 2 deletions web-admin/src/components/OperationSetting.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ const OperationSetting = () => {
DrawingEnabled: '',
DataExportEnabled: '',
DataExportInterval: 5,
RetryTimes: 0
});
RetryTimes: 0,
MiniQuota: 10,
ProporTions: 10,
});
const [originInputs, setOriginInputs] = useState({});
let [loading, setLoading] = useState(false);
let [historyTimestamp, setHistoryTimestamp] = useState(timestamp2string(now.getTime() / 1000 - 30 * 24 * 3600)); // a month ago
Expand Down Expand Up @@ -127,6 +129,12 @@ const OperationSetting = () => {
if (originInputs['PreConsumedQuota'] !== inputs.PreConsumedQuota) {
await updateOption('PreConsumedQuota', inputs.PreConsumedQuota);
}
if (originInputs['MiniQuota'] !== inputs.MiniQuota) {
await updateOption('MiniQuota', inputs.MiniQuota);
}
if (originInputs['ProporTions'] !== inputs.ProporTions) {
await updateOption('ProporTions', inputs.ProporTions);
}
break;
case 'general':
if (originInputs['TopUpLink'] !== inputs.TopUpLink) {
Expand Down Expand Up @@ -351,6 +359,26 @@ const OperationSetting = () => {
min='0'
placeholder='例如:1000'
/>
<Form.Input
label='提现/划转最低金额(1=1元)'
name='MiniQuota'
onChange={handleInputChange}
autoComplete='new-password'
value={inputs.MiniQuota}
type='number'
min='0'
placeholder='例如:10'
/>
<Form.Input
label='邀请用户返现比例'
name='ProporTions'
onChange={handleInputChange}
autoComplete='new-password'
value={inputs.ProporTions}
type='number'
min='0'
placeholder='例如:10'
/>
</Form.Group>
<Form.Button onClick={() => {
submitConfig('quota').then();
Expand Down

0 comments on commit e130e8a

Please sign in to comment.