优化小程序登录流程,增加用户协议和隐私政策的勾选机制,确保用户主动同意后方可登录,符合审核要求。同时,增强错误处理逻辑,提升用户体验和系统稳定性。新增用户协议和隐私政策页面,更新相关样式以改善界面交互。
This commit is contained in:
@@ -9,19 +9,18 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// AdminCheck GET /api/admin 鉴权检查(与 next-project 一致:校验 admin_session cookie,已登录返回 success 或概览占位)
|
||||
// AdminCheck GET /api/admin 鉴权检查(JWT:Authorization Bearer 或 Cookie),已登录返回 success 或概览占位
|
||||
func AdminCheck(c *gin.Context) {
|
||||
cfg := config.Get()
|
||||
if cfg == nil {
|
||||
c.JSON(http.StatusOK, gin.H{"success": true})
|
||||
return
|
||||
}
|
||||
token := auth.GetAdminTokenFromRequest(c.Request)
|
||||
if !auth.VerifyAdminToken(token, cfg.AdminSessionSecret) {
|
||||
token := auth.GetAdminJWTFromRequest(c.Request)
|
||||
if _, ok := auth.ParseAdminJWT(token, cfg.AdminSessionSecret); !ok {
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"success": false, "error": "未授权访问,请先登录"})
|
||||
return
|
||||
}
|
||||
// 与 next 一致:返回 success,可选带概览占位供前端扩展
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"content": gin.H{
|
||||
@@ -40,7 +39,7 @@ func AdminCheck(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// AdminLogin POST /api/admin 登录(与 next-project 一致:校验 ADMIN_USERNAME/PASSWORD,写 admin_session cookie)
|
||||
// AdminLogin POST /api/admin 登录(校验 ADMIN_USERNAME/PASSWORD,返回 JWT,前端存 token 并带 Authorization: Bearer)
|
||||
func AdminLogin(c *gin.Context) {
|
||||
cfg := config.Get()
|
||||
if cfg == nil {
|
||||
@@ -61,19 +60,22 @@ func AdminLogin(c *gin.Context) {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"success": false, "error": "用户名或密码错误"})
|
||||
return
|
||||
}
|
||||
token := auth.CreateAdminToken(cfg.AdminSessionSecret)
|
||||
c.SetCookie(auth.AdminCookieName(), token, auth.MaxAgeSec(), "/", "", false, true)
|
||||
token, err := auth.IssueAdminJWT(cfg.AdminSessionSecret, username)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": "签发失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"token": token,
|
||||
"user": gin.H{
|
||||
"id": "admin", "username": cfg.AdminUsername, "role": "admin", "name": "卡若",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// AdminLogout POST /api/admin/logout 清除 admin_session cookie
|
||||
// AdminLogout POST /api/admin/logout 服务端无状态,仅返回成功;前端需清除本地 token
|
||||
func AdminLogout(c *gin.Context) {
|
||||
c.SetCookie(auth.AdminCookieName(), "", -1, "/", "", false, true)
|
||||
c.JSON(http.StatusOK, gin.H{"success": true})
|
||||
}
|
||||
|
||||
|
||||
@@ -149,9 +149,7 @@ func AdminWithdrawalsAction(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 调用微信转账接口;未初始化时仅标记为已打款(线下打款)
|
||||
outBatchNo := wechat.GenerateTransferBatchNo()
|
||||
outDetailNo := wechat.GenerateTransferDetailNo()
|
||||
// 调用微信转账接口(FundApp 单笔发起转账,与 商家转账.md 示例一致)
|
||||
remark := "提现"
|
||||
if w.Remark != nil && *w.Remark != "" {
|
||||
remark = *w.Remark
|
||||
@@ -161,26 +159,21 @@ func AdminWithdrawalsAction(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"success": false, "error": "提现金额异常"})
|
||||
return
|
||||
}
|
||||
// 微信批次备注最多 32 字符,用简短文案避免超长
|
||||
batchRemark := fmt.Sprintf("提现 %.2f 元", w.Amount)
|
||||
if len([]rune(batchRemark)) > 32 {
|
||||
batchRemark = "用户提现"
|
||||
}
|
||||
params := wechat.TransferParams{
|
||||
OutBatchNo: outBatchNo,
|
||||
OutDetailNo: outDetailNo,
|
||||
OpenID: openID,
|
||||
Amount: amountFen,
|
||||
Remark: remark,
|
||||
BatchName: "用户提现",
|
||||
BatchRemark: batchRemark,
|
||||
outBillNo := w.ID // 商户单号,回调时 out_bill_no 即此值,用于更新该条提现
|
||||
params := wechat.FundAppTransferParams{
|
||||
OutBillNo: outBillNo,
|
||||
OpenID: openID,
|
||||
Amount: amountFen,
|
||||
Remark: remark,
|
||||
NotifyURL: "", // 由 wechat 包从配置读取 WechatTransferURL
|
||||
TransferSceneId: "1005",
|
||||
}
|
||||
|
||||
result, err := wechat.InitiateTransfer(params)
|
||||
result, err := wechat.InitiateTransferByFundApp(params)
|
||||
if err != nil {
|
||||
errMsg := err.Error()
|
||||
fmt.Printf("[AdminWithdrawals] 发起转账失败 id=%s: %s(微信侧不会产生批次记录)\n", body.ID, errMsg)
|
||||
// 未初始化或未配置转账:仅标记为已打款并提示线下处理(与 transfer 包返回文案一致)
|
||||
fmt.Printf("[AdminWithdrawals] 发起转账失败 id=%s: %s\n", body.ID, errMsg)
|
||||
// 未初始化或未配置转账:仅标记为已打款并提示线下处理
|
||||
if errMsg == "支付/转账未初始化,请先调用 wechat.Init" || errMsg == "转账客户端未初始化" {
|
||||
_ = db.Model(&w).Updates(map[string]interface{}{
|
||||
"status": "success",
|
||||
@@ -192,7 +185,7 @@ func AdminWithdrawalsAction(c *gin.Context) {
|
||||
})
|
||||
return
|
||||
}
|
||||
// 其他打款失败(含调用腾讯接口失败):记失败原因
|
||||
// 微信接口报错或其它失败:把微信/具体原因返回给管理端展示,不返回「微信处理中」
|
||||
failMsg := errMsg
|
||||
_ = db.Model(&w).Updates(map[string]interface{}{
|
||||
"status": "failed",
|
||||
@@ -200,6 +193,23 @@ func AdminWithdrawalsAction(c *gin.Context) {
|
||||
"error_message": failMsg,
|
||||
"processed_at": now,
|
||||
}).Error
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": false,
|
||||
"error": "发起打款失败",
|
||||
"message": failMsg, // 管理端直接展示微信报错信息(如 IP 白名单、参数错误等)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 防护:微信未返回商户单号时也按失败返回,避免管理端显示「已发起打款」却无单号
|
||||
if result.OutBillNo == "" {
|
||||
failMsg := "微信未返回商户单号,请检查商户平台(如 IP 白名单)或查看服务端日志"
|
||||
_ = db.Model(&w).Updates(map[string]interface{}{
|
||||
"status": "failed",
|
||||
"fail_reason": failMsg,
|
||||
"error_message": failMsg,
|
||||
"processed_at": now,
|
||||
}).Error
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": false,
|
||||
"error": "发起打款失败",
|
||||
@@ -208,28 +218,26 @@ func AdminWithdrawalsAction(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 打款已受理,更新为处理中并保存商家批次/明细单号及微信批次号
|
||||
fmt.Printf("[AdminWithdrawals] 微信已受理 id=%s out_batch_no=%s batch_id=%s(商户后台「商家转账到零钱」可凭商家批次单号查询)\n", body.ID, result.OutBatchNo, result.BatchID)
|
||||
// 打款已受理(FundApp 单笔),更新为处理中并保存商户单号、微信转账单号
|
||||
fmt.Printf("[AdminWithdrawals] 微信已受理 id=%s out_bill_no=%s transfer_bill_no=%s\n", body.ID, result.OutBillNo, result.TransferBillNo)
|
||||
processingStatus := "processing"
|
||||
batchID := result.BatchID
|
||||
if err := db.Model(&w).Updates(map[string]interface{}{
|
||||
"status": processingStatus,
|
||||
"batch_no": outBatchNo,
|
||||
"detail_no": outDetailNo,
|
||||
"batch_id": batchID,
|
||||
"detail_no": result.OutBillNo, // 回调用 out_bill_no 匹配此字段
|
||||
"batch_no": result.OutBillNo, // 单笔无批次,存同一单号便于查询
|
||||
"batch_id": result.TransferBillNo,
|
||||
"processed_at": now,
|
||||
}).Error; err != nil {
|
||||
fmt.Printf("[AdminWithdrawals] 更新提现状态失败 id=%s: %v\n", body.ID, err)
|
||||
c.JSON(http.StatusOK, gin.H{"success": false, "error": "更新状态失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
// 始终返回 out_batch_no 便于追踪;batch_id 为微信返回,可能为空
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"message": "已发起打款,微信处理中",
|
||||
"data": gin.H{
|
||||
"batch_id": batchID,
|
||||
"out_batch_no": outBatchNo,
|
||||
"out_bill_no": result.OutBillNo,
|
||||
"transfer_bill_no": result.TransferBillNo,
|
||||
},
|
||||
})
|
||||
return
|
||||
@@ -275,20 +283,29 @@ func AdminWithdrawalsSync(c *gin.Context) {
|
||||
if w.DetailNo != nil {
|
||||
detailNo = *w.DetailNo
|
||||
}
|
||||
if batchNo == "" || detailNo == "" {
|
||||
if detailNo == "" {
|
||||
continue
|
||||
}
|
||||
res, err := wechat.QueryTransfer(batchNo, detailNo)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
status := ""
|
||||
if s, ok := res["detail_status"].(string); ok {
|
||||
status = s
|
||||
}
|
||||
failReason := ""
|
||||
if s, ok := res["fail_reason"].(string); ok {
|
||||
failReason = s
|
||||
var status, failReason string
|
||||
// FundApp 单笔:batch_no == detail_no 时用商户单号查询
|
||||
if batchNo == detailNo {
|
||||
state, _, fail, err := wechat.QueryTransferByOutBill(detailNo)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
status = state
|
||||
failReason = fail
|
||||
} else {
|
||||
res, err := wechat.QueryTransfer(batchNo, detailNo)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if s, ok := res["detail_status"].(string); ok {
|
||||
status = s
|
||||
}
|
||||
if s, ok := res["fail_reason"].(string); ok {
|
||||
failReason = s
|
||||
}
|
||||
}
|
||||
up := map[string]interface{}{"processed_at": now}
|
||||
switch status {
|
||||
|
||||
Reference in New Issue
Block a user