- 登录链路改为用户名 + 密码 + 短信验证码 + DCAuth v2 设备签名 - 迁移 014:新增 login_sms_codes / user_devices / device_challenges 与 users.phone - 新增 /api/dcauth/authorize 与 challenge 状态轮询;旧密码登录仅放行未绑定手机号的账号 - 成员管理支持手机号绑定、设备重置、账号改名、昵称与重置密码 - 移除最后一个商户关系时软删除账号,释放用户名、手机号并撤销设备绑定
178 lines
5.1 KiB
Go
178 lines
5.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"affiliate_dash/internal/middleware"
|
|
"affiliate_dash/internal/pkg/response"
|
|
"affiliate_dash/internal/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func (h *MerchantHandler) ListMembers(c *gin.Context) {
|
|
members, err := h.merchantSvc.ListMembers(middleware.GetMerchantID(c))
|
|
if err != nil {
|
|
response.ServerError(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, members)
|
|
}
|
|
|
|
type addMemberReq struct {
|
|
UserID uint `json:"user_id"`
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
Nickname string `json:"nickname"`
|
|
Role string `json:"role" binding:"required"`
|
|
IsDefault bool `json:"is_default"`
|
|
}
|
|
|
|
func (h *MerchantHandler) AddCurrentMerchantMember(c *gin.Context) {
|
|
var req addMemberReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误:角色必填")
|
|
return
|
|
}
|
|
member, err := h.merchantSvc.AddMember(middleware.GetMerchantID(c), service.AddMemberInput{
|
|
UserID: req.UserID,
|
|
Username: req.Username,
|
|
Password: req.Password,
|
|
Nickname: req.Nickname,
|
|
Role: req.Role,
|
|
IsDefault: req.IsDefault,
|
|
}, middleware.GetUserID(c))
|
|
if err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, member)
|
|
}
|
|
|
|
type updateMemberReq struct {
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
Nickname *string `json:"nickname"`
|
|
Role string `json:"role" binding:"required"`
|
|
Status int `json:"status"`
|
|
IsDefault bool `json:"is_default"`
|
|
}
|
|
|
|
func (h *MerchantHandler) UpdateCurrentMerchantMember(c *gin.Context) {
|
|
memberID, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
var req updateMemberReq
|
|
if memberID == 0 || c.ShouldBindJSON(&req) != nil {
|
|
response.BadRequest(c, "参数错误:角色必填")
|
|
return
|
|
}
|
|
member, err := h.merchantSvc.UpdateMember(middleware.GetMerchantID(c), uint(memberID), service.UpdateMemberInput{
|
|
Username: req.Username, Password: req.Password, Nickname: req.Nickname,
|
|
Role: req.Role, Status: req.Status, IsDefault: req.IsDefault,
|
|
}, middleware.GetUserID(c))
|
|
if err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, member)
|
|
}
|
|
|
|
func (h *MerchantHandler) RemoveCurrentMerchantMember(c *gin.Context) {
|
|
memberID, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if memberID == 0 {
|
|
response.BadRequest(c, "参数错误")
|
|
return
|
|
}
|
|
if err := h.merchantSvc.RemoveMember(middleware.GetMerchantID(c), uint(memberID), middleware.GetUserID(c)); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, nil)
|
|
}
|
|
|
|
func (h *MerchantHandler) ListRoles(c *gin.Context) {
|
|
roles, err := h.merchantSvc.ListRoles(middleware.GetMerchantID(c))
|
|
if err != nil {
|
|
response.ServerError(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, roles)
|
|
}
|
|
|
|
type createMerchantRoleReq struct {
|
|
Code string `json:"code" binding:"required"`
|
|
Name string `json:"name" binding:"required"`
|
|
Permissions []string `json:"permissions"`
|
|
}
|
|
|
|
func (h *MerchantHandler) CreateRole(c *gin.Context) {
|
|
var req createMerchantRoleReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误:角色编码和名称必填")
|
|
return
|
|
}
|
|
role, err := h.merchantSvc.CreateRole(middleware.GetMerchantID(c), service.MerchantRoleInput{
|
|
Code: req.Code, Name: req.Name, Permissions: req.Permissions,
|
|
}, middleware.GetUserID(c))
|
|
if err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, role)
|
|
}
|
|
|
|
func (h *MerchantHandler) UpdateRole(c *gin.Context) {
|
|
roleID, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
var req createMerchantRoleReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误:角色名称必填")
|
|
return
|
|
}
|
|
role, err := h.merchantSvc.UpdateRole(middleware.GetMerchantID(c), uint(roleID), service.MerchantRoleInput{
|
|
Name: req.Name, Permissions: req.Permissions,
|
|
}, middleware.GetUserID(c))
|
|
if err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, role)
|
|
}
|
|
|
|
type bindMemberPhoneReq struct {
|
|
Phone string `json:"phone" binding:"required"`
|
|
}
|
|
|
|
// BindCurrentMerchantMemberPhone 商户管理员为当前商户成员绑定/修改登录手机号。
|
|
func (h *MerchantHandler) BindCurrentMerchantMemberPhone(c *gin.Context) {
|
|
memberID, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
var req bindMemberPhoneReq
|
|
if memberID == 0 || c.ShouldBindJSON(&req) != nil {
|
|
response.BadRequest(c, "请输入手机号")
|
|
return
|
|
}
|
|
user, err := h.merchantSvc.SetMemberPhone(
|
|
middleware.GetMerchantID(c), uint(memberID), req.Phone, middleware.GetUserID(c),
|
|
)
|
|
if err != nil {
|
|
writeServiceError(c, err, "手机号绑定失败")
|
|
return
|
|
}
|
|
response.OK(c, user)
|
|
}
|
|
|
|
// ResetCurrentMerchantMemberDevice 重置当前商户成员绑定的 DCAuth 设备。
|
|
func (h *MerchantHandler) ResetCurrentMerchantMemberDevice(c *gin.Context) {
|
|
memberID, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if memberID == 0 {
|
|
response.BadRequest(c, "参数错误")
|
|
return
|
|
}
|
|
reset, err := h.merchantSvc.ResetMemberDevice(
|
|
middleware.GetMerchantID(c), uint(memberID), middleware.GetUserID(c),
|
|
)
|
|
if err != nil {
|
|
writeServiceError(c, err, "设备重置失败")
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"reset": reset})
|
|
}
|