Files
cmautobuy/admin/handler/web/syb_shop.go
T

77 lines
2.1 KiB
Go

package web
import (
"errors"
"net/http"
"net/url"
"time"
"github.com/gin-gonic/gin"
"cmautobuy/admin/repository"
"cmautobuy/admin/service"
)
func (h *Handler) SybAllowedShopList(c *gin.Context) {
rows, err := service.ListSybAllowedShops(h.db, currentUser(c))
if err != nil {
fail(c, http.StatusInternalServerError, "读取顺运宝同步店铺失败,数据没有被改动。刷新后重试。")
return
}
enabled := 0
for _, row := range rows {
if row.Enabled {
enabled++
}
}
c.HTML(http.StatusOK, "syb/shops", page(c, "syb", "顺运宝同步店铺", gin.H{
"Rows": rows, "EnabledCount": enabled, "Message": c.Query("msg"), "Error": c.Query("error"),
}))
}
func (h *Handler) SybAllowedShopCreate(c *gin.Context) {
err := service.CreateSybAllowedShop(h.db, currentUser(c), c.PostForm("shop_name"), time.Now())
if err != nil {
if service.IsValidationError(err) || errors.Is(err, repository.ErrSybAllowedShopExists) {
redirectSybShops(c, "", err.Error())
return
}
fail(c, http.StatusInternalServerError, "新增同步店铺失败,已有店铺没有被改动。")
return
}
redirectSybShops(c, "店铺已加入允许列表并启用", "")
}
func (h *Handler) SybAllowedShopStatus(c *gin.Context) {
enabled := c.PostForm("enabled") == "1"
err := service.SetSybAllowedShopEnabled(h.db, currentUser(c), c.PostForm("shop_id"), enabled, time.Now())
if err != nil {
if service.IsValidationError(err) {
redirectSybShops(c, "", err.Error())
return
}
fail(c, http.StatusInternalServerError, "更新同步店铺失败,原状态保持不变。")
return
}
message := "店铺已停用,后续同步会跳过该店铺"
if enabled {
message = "店铺已重新启用"
}
redirectSybShops(c, message, "")
}
func redirectSybShops(c *gin.Context, message, errorMessage string) {
values := url.Values{}
if message != "" {
values.Set("msg", message)
}
if errorMessage != "" {
values.Set("error", errorMessage)
}
target := "/syb/shops"
if encoded := values.Encode(); encoded != "" {
target += "?" + encoded
}
c.Redirect(http.StatusSeeOther, target)
}