feat: add charge page

This commit is contained in:
Rogee
2024-12-12 19:46:59 +08:00
parent 38a45958bd
commit 3ce3c9ee84
5 changed files with 68 additions and 0 deletions

BIN
backend/__debug_bin3277441022 Executable file

Binary file not shown.

View File

@@ -2,6 +2,7 @@ package users
import (
"backend/pkg/consts"
"backend/pkg/errorx"
"backend/providers/jwt"
"github.com/gofiber/fiber/v3"
@@ -56,3 +57,38 @@ func (c *Controller) Info(ctx fiber.Ctx) error {
return ctx.JSON(info)
}
func (c *Controller) GetChargeCodes(ctx fiber.Ctx) error {
claim := fiber.Locals[*jwt.Claims](ctx, consts.CtxKeyClaim)
log.Debug(claim)
tenant, err := c.svc.GetTenantByID(ctx.Context(), claim.TenantID)
if err != nil {
return errors.Wrapf(err, "get tenant: %d", claim.TenantID)
}
if tenant.BindUserID != claim.UserID {
return errorx.RequestParseError
}
type generateCode struct {
Amount int64 `json:"amount"`
Code string `json:"code"`
}
amount := []int64{1, 5, 10, 20, 50, 100}
codes := []generateCode{}
for _, a := range amount {
code, err := c.svc.GenerateChargeCode(ctx.Context(), claim.TenantID, a*100)
if err != nil {
log.WithError(err).Errorf("generate charge code")
return errorx.InternalError
}
codes = append(codes, generateCode{
Amount: a * 100,
Code: code,
})
}
return ctx.JSON(codes)
}

View File

@@ -28,4 +28,5 @@ func (r *Router) Register(router fiber.Router) {
group := router.Group(r.Name())
group.Get("info", r.controller.Info)
group.Patch("charge/:code", r.controller.Charge)
group.Get("codes", r.controller.GetChargeCodes)
}

View File

@@ -11,6 +11,9 @@
</van-field>
</van-cell-group>
<van-cell-group title="充值码" v-if="user.is_admin">
<van-cell v-for="c in codes" size="large" :title="getCodeAmountTitle(c)" :value="c.code" @click="copyCode(c)" />
</van-cell-group>
@@ -19,12 +22,17 @@
<script setup>
import request from "@/utils/request";
const codes = ref({});
const user = ref({});
const chargeCode = ref("")
const loadUserInfo = () => {
request.get("/users/info").then((res) => {
user.value = res.data;
if (user.value.is_admin) {
loadChargeCodes();
}
}).catch((err) => {
console.log(err);
});
@@ -34,6 +42,18 @@ onMounted(() => {
loadUserInfo();
});
const loadChargeCodes = () => {
if (!user.value.is_admin) {
return;
}
request.get("/users/codes").then((res) => {
codes.value = res.data;
}).catch((err) => {
console.log(err);
});
};
const confirmCharge = () => {
if (!chargeCode.value) {
return showFailToast('请输入充值码');
@@ -59,6 +79,17 @@ const confirmCharge = () => {
console.log("取消充值");
});
};
const getCodeAmountTitle = (code) => {
return code.amount / 100 + " 元/ " + code.amount + " 点";
};
const copyCode = (c) => {
// h5 copy c.code to clipboard
navigator.clipboard.writeText(c.code);
showSuccessToast("充值码已复制");
};
</script>