feat: 新增充值分组倍率设置
This commit is contained in:
parent
bd22fee12d
commit
a4e269262b
31
common/topup-ratio.go
Normal file
31
common/topup-ratio.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import "encoding/json"
|
||||||
|
|
||||||
|
var TopupGroupRatio = map[string]float64{
|
||||||
|
"default": 1,
|
||||||
|
"vip": 1,
|
||||||
|
"svip": 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
func TopupGroupRatio2JSONString() string {
|
||||||
|
jsonBytes, err := json.Marshal(TopupGroupRatio)
|
||||||
|
if err != nil {
|
||||||
|
SysError("error marshalling model ratio: " + err.Error())
|
||||||
|
}
|
||||||
|
return string(jsonBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateTopupGroupRatioByJSONString(jsonStr string) error {
|
||||||
|
TopupGroupRatio = make(map[string]float64)
|
||||||
|
return json.Unmarshal([]byte(jsonStr), &TopupGroupRatio)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTopupGroupRatio(name string) float64 {
|
||||||
|
ratio, ok := TopupGroupRatio[name]
|
||||||
|
if !ok {
|
||||||
|
SysError("topup group ratio not found: " + name)
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return ratio
|
||||||
|
}
|
@ -38,9 +38,13 @@ func GetEpayClient() *epay.Client {
|
|||||||
return withUrl
|
return withUrl
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAmount(count float64) float64 {
|
func GetAmount(count float64, user model.User) float64 {
|
||||||
// 别问为什么用float64,问就是这么点钱没必要
|
// 别问为什么用float64,问就是这么点钱没必要
|
||||||
amount := count * common.Price
|
topupGroupRatio := common.GetTopupGroupRatio(user.Group)
|
||||||
|
if topupGroupRatio == 0 {
|
||||||
|
topupGroupRatio = 1
|
||||||
|
}
|
||||||
|
amount := count * common.Price * topupGroupRatio
|
||||||
return amount
|
return amount
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,7 +61,8 @@ func RequestEpay(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
id := c.GetInt("id")
|
id := c.GetInt("id")
|
||||||
amount := GetAmount(float64(req.Amount))
|
user, _ := model.GetUserById(id, false)
|
||||||
|
amount := GetAmount(float64(req.Amount), *user)
|
||||||
|
|
||||||
if req.PaymentMethod == "zfb" {
|
if req.PaymentMethod == "zfb" {
|
||||||
req.PaymentMethod = "alipay"
|
req.PaymentMethod = "alipay"
|
||||||
@ -148,7 +153,7 @@ func EpayNotify(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("易支付回调更新用户成功 %v", topUp)
|
log.Printf("易支付回调更新用户成功 %v", topUp)
|
||||||
model.RecordLog(topUp.UserId, model.LogTypeTopup, fmt.Sprintf("使用在线充值成功,充值金额: %v", common.LogQuota(topUp.Amount*500000)))
|
model.RecordLog(topUp.UserId, model.LogTypeTopup, fmt.Sprintf("使用在线充值成功,充值金额: %v,支付金额:%d", common.LogQuota(topUp.Amount*500000), topUp.Money))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Printf("易支付异常回调: %v", verifyInfo)
|
log.Printf("易支付异常回调: %v", verifyInfo)
|
||||||
@ -166,6 +171,7 @@ func RequestAmount(c *gin.Context) {
|
|||||||
c.JSON(200, gin.H{"message": "error", "data": "充值金额不能小于1"})
|
c.JSON(200, gin.H{"message": "error", "data": "充值金额不能小于1"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
id := c.GetInt("id")
|
||||||
c.JSON(200, gin.H{"message": "success", "data": GetAmount(float64(req.Amount))})
|
user, _ := model.GetUserById(id, false)
|
||||||
|
c.JSON(200, gin.H{"message": "success", "data": GetAmount(float64(req.Amount), *user)})
|
||||||
}
|
}
|
||||||
|
@ -57,6 +57,7 @@ func InitOptionMap() {
|
|||||||
common.OptionMap["EpayId"] = ""
|
common.OptionMap["EpayId"] = ""
|
||||||
common.OptionMap["EpayKey"] = ""
|
common.OptionMap["EpayKey"] = ""
|
||||||
common.OptionMap["Price"] = strconv.FormatFloat(common.Price, 'f', -1, 64)
|
common.OptionMap["Price"] = strconv.FormatFloat(common.Price, 'f', -1, 64)
|
||||||
|
common.OptionMap["TopupGroupRatio"] = common.TopupGroupRatio2JSONString()
|
||||||
common.OptionMap["GitHubClientId"] = ""
|
common.OptionMap["GitHubClientId"] = ""
|
||||||
common.OptionMap["GitHubClientSecret"] = ""
|
common.OptionMap["GitHubClientSecret"] = ""
|
||||||
common.OptionMap["WeChatServerAddress"] = ""
|
common.OptionMap["WeChatServerAddress"] = ""
|
||||||
@ -184,6 +185,8 @@ func updateOptionMap(key string, value string) (err error) {
|
|||||||
common.EpayKey = value
|
common.EpayKey = value
|
||||||
case "Price":
|
case "Price":
|
||||||
common.Price, _ = strconv.ParseFloat(value, 64)
|
common.Price, _ = strconv.ParseFloat(value, 64)
|
||||||
|
case "TopupGroupRatio":
|
||||||
|
err = common.UpdateTopupGroupRatioByJSONString(value)
|
||||||
case "GitHubClientId":
|
case "GitHubClientId":
|
||||||
common.GitHubClientId = value
|
common.GitHubClientId = value
|
||||||
case "GitHubClientSecret":
|
case "GitHubClientSecret":
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import React, {useEffect, useState} from 'react';
|
import React, {useEffect, useState} from 'react';
|
||||||
import {Button, Divider, Form, Grid, Header, Modal, Message} from 'semantic-ui-react';
|
import {Button, Divider, Form, Grid, Header, Modal, Message} from 'semantic-ui-react';
|
||||||
import {API, removeTrailingSlash, showError} from '../helpers';
|
import {API, removeTrailingSlash, showError, verifyJSON} from '../helpers';
|
||||||
|
|
||||||
const SystemSetting = () => {
|
const SystemSetting = () => {
|
||||||
let [inputs, setInputs] = useState({
|
let [inputs, setInputs] = useState({
|
||||||
@ -20,6 +20,7 @@ const SystemSetting = () => {
|
|||||||
EpayId: '',
|
EpayId: '',
|
||||||
EpayKey: '',
|
EpayKey: '',
|
||||||
Price: 7.3,
|
Price: 7.3,
|
||||||
|
TopupGroupRatio: '',
|
||||||
PayAddress: '',
|
PayAddress: '',
|
||||||
Footer: '',
|
Footer: '',
|
||||||
WeChatAuthEnabled: '',
|
WeChatAuthEnabled: '',
|
||||||
@ -45,6 +46,9 @@ const SystemSetting = () => {
|
|||||||
if (success) {
|
if (success) {
|
||||||
let newInputs = {};
|
let newInputs = {};
|
||||||
data.forEach((item) => {
|
data.forEach((item) => {
|
||||||
|
if (item.key === 'TopupGroupRatio') {
|
||||||
|
item.value = JSON.stringify(JSON.parse(item.value), null, 2);
|
||||||
|
}
|
||||||
newInputs[item.key] = item.value;
|
newInputs[item.key] = item.value;
|
||||||
});
|
});
|
||||||
setInputs({
|
setInputs({
|
||||||
@ -123,7 +127,8 @@ const SystemSetting = () => {
|
|||||||
name === 'WeChatAccountQRCodeImageURL' ||
|
name === 'WeChatAccountQRCodeImageURL' ||
|
||||||
name === 'TurnstileSiteKey' ||
|
name === 'TurnstileSiteKey' ||
|
||||||
name === 'TurnstileSecretKey' ||
|
name === 'TurnstileSecretKey' ||
|
||||||
name === 'EmailDomainWhitelist'
|
name === 'EmailDomainWhitelist' ||
|
||||||
|
name === 'TopupGroupRatio'
|
||||||
) {
|
) {
|
||||||
setInputs((inputs) => ({...inputs, [name]: value}));
|
setInputs((inputs) => ({...inputs, [name]: value}));
|
||||||
} else {
|
} else {
|
||||||
@ -141,6 +146,13 @@ const SystemSetting = () => {
|
|||||||
showError('请先填写服务器地址');
|
showError('请先填写服务器地址');
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if (originInputs['TopupGroupRatio'] !== inputs.TopupGroupRatio) {
|
||||||
|
if (!verifyJSON(inputs.TopupGroupRatio)) {
|
||||||
|
showError('充值分组倍率不是合法的 JSON 字符串');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await updateOption('TopupGroupRatio', inputs.TopupGroupRatio);
|
||||||
|
}
|
||||||
let PayAddress = removeTrailingSlash(inputs.PayAddress);
|
let PayAddress = removeTrailingSlash(inputs.PayAddress);
|
||||||
await updateOption('PayAddress', PayAddress);
|
await updateOption('PayAddress', PayAddress);
|
||||||
await updateOption('EpayId', inputs.EpayId);
|
await updateOption('EpayId', inputs.EpayId);
|
||||||
@ -297,8 +309,19 @@ const SystemSetting = () => {
|
|||||||
onChange={handleInputChange}
|
onChange={handleInputChange}
|
||||||
/>
|
/>
|
||||||
</Form.Group>
|
</Form.Group>
|
||||||
|
<Form.Group widths='equal'>
|
||||||
|
<Form.TextArea
|
||||||
|
label='充值分组倍率'
|
||||||
|
name='TopupGroupRatio'
|
||||||
|
onChange={handleInputChange}
|
||||||
|
style={{minHeight: 250, fontFamily: 'JetBrains Mono, Consolas'}}
|
||||||
|
autoComplete='new-password'
|
||||||
|
value={inputs.TopupGroupRatio}
|
||||||
|
placeholder='为一个 JSON 文本,键为组名称,值为倍率'
|
||||||
|
/>
|
||||||
|
</Form.Group>
|
||||||
<Form.Button onClick={submitPayAddress}>
|
<Form.Button onClick={submitPayAddress}>
|
||||||
更新支付地址
|
更新支付设置
|
||||||
</Form.Button>
|
</Form.Button>
|
||||||
<Divider/>
|
<Divider/>
|
||||||
<Header as='h3'>配置登录注册</Header>
|
<Header as='h3'>配置登录注册</Header>
|
||||||
|
Loading…
Reference in New Issue
Block a user