Compare commits
5 Commits
89dfd58e1c
...
cac0161585
Author | SHA1 | Date | |
---|---|---|---|
|
cac0161585 | ||
|
28cc8a19ba | ||
|
b27aaab6b6 | ||
|
29d5c91736 | ||
|
dfcceaea8e |
@ -1,9 +1,12 @@
|
||||
package config
|
||||
|
||||
import {{.authImport}}
|
||||
import "fusenapi/server/faq/internal/types"
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
SourceMysql string
|
||||
Auth types.Auth
|
||||
{{.auth}}
|
||||
{{.jwtTrans}}
|
||||
}
|
||||
|
@ -27,7 +27,11 @@ func (m *FsGuestModel) GenerateGuestID(ctx context.Context, AccessSecret *string
|
||||
}
|
||||
record.AuthKey = &authKey
|
||||
record.CreatedAt = &now
|
||||
tx.Updates(record)
|
||||
err = tx.Updates(record).Error
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
|
@ -1,6 +1,9 @@
|
||||
package gmodel
|
||||
|
||||
import "context"
|
||||
import (
|
||||
"context"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func (ml *FsMapLibraryModel) GetAllEnabledList(ctx context.Context, fields ...string) (resp []FsMapLibrary, err error) {
|
||||
db := ml.db.WithContext(ctx).Model(&FsMapLibrary{}).Where("`status` = ?", 1)
|
||||
@ -25,3 +28,26 @@ func (ml *FsMapLibraryModel) ChangeStatusByIds(ctx context.Context, ids []int64,
|
||||
}
|
||||
return ml.db.WithContext(ctx).Model(&FsMapLibrary{}).Where("`id` in (?) ", ids).Update("status", 0).Error
|
||||
}
|
||||
func (ml *FsMapLibraryModel) SaveMapLibraryWithTransaction(ctx context.Context, createList []FsMapLibrary, updateList []FsMapLibrary) error {
|
||||
return ml.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
//创建
|
||||
for _, v := range createList {
|
||||
if err := tx.Model(&FsMapLibrary{}).Create(&v).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if len(updateList) == 0 {
|
||||
return nil
|
||||
}
|
||||
//更新
|
||||
notInIds := make([]int64, 0, len(updateList))
|
||||
for _, v := range updateList {
|
||||
notInIds = append(notInIds, v.Id)
|
||||
if err := tx.Model(&FsMapLibrary{}).Where("`id` = ?", v.Id).Updates(&v).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
//删除
|
||||
return tx.Model(&FsMapLibrary{}).Where("`id` not in (?) ", notInIds).Update("status", 0).Error
|
||||
})
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
package gmodel
|
||||
|
||||
import "context"
|
||||
import (
|
||||
"context"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// TODO: 使用model的属性做你想做的
|
||||
|
||||
@ -12,3 +15,23 @@ func (bm *FsProductTemplateBasemapModel) GetAllEnabledList(ctx context.Context,
|
||||
err = db.Find(&resp).Error
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (bm *FsProductTemplateBasemapModel) UpdateBaseMapWithTransaction(ctx context.Context, dataList []FsProductTemplateBasemap) error {
|
||||
return bm.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
notInIds := make([]int64, 0, len(dataList))
|
||||
//更新
|
||||
for _, v := range dataList {
|
||||
notInIds = append(notInIds, v.Id)
|
||||
err := tx.Where("`id` = ? ", v.Id).Updates(&v).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if len(notInIds) == 0 {
|
||||
return nil
|
||||
}
|
||||
//删除不在ids里面的
|
||||
return tx.Model(&FsProductTemplateBasemap{}).
|
||||
Where("`status` = ? and `id` not in (?)", 1, notInIds).Update("status", 0).Error
|
||||
})
|
||||
}
|
||||
|
8
server/faq/etc/faq.yaml
Normal file
8
server/faq/etc/faq.yaml
Normal file
@ -0,0 +1,8 @@
|
||||
Name: faq
|
||||
Host: 0.0.0.0
|
||||
Port: 8888
|
||||
SourceMysql: ""
|
||||
Auth:
|
||||
AccessSecret: fusen2023
|
||||
AccessExpire: 604800
|
||||
RefreshAfter: 345600
|
49
server/faq/faq.go
Normal file
49
server/faq/faq.go
Normal file
@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
"fusenapi/server/faq/internal/config"
|
||||
"fusenapi/server/faq/internal/handler"
|
||||
"fusenapi/server/faq/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/faq.yaml", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
|
||||
server := rest.MustNewServer(c.RestConf)
|
||||
defer server.Stop()
|
||||
|
||||
ctx := svc.NewServiceContext(c)
|
||||
handler.RegisterHandlers(server, ctx)
|
||||
|
||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||
server.Start()
|
||||
}
|
||||
|
||||
// var testConfigFile = flag.String("f", "../etc/faq.yaml", "the config file")
|
||||
// var cnf config.Config
|
||||
|
||||
// func GetTestServer() *rest.Server {
|
||||
// flag.Parse()
|
||||
|
||||
// conf.MustLoad(*testConfigFile, &cnf)
|
||||
|
||||
// server := rest.MustNewServer(cnf.RestConf)
|
||||
// defer server.Stop()
|
||||
|
||||
// ctx := svc.NewServiceContext(cnf)
|
||||
// handler.RegisterHandlers(server, ctx)
|
||||
|
||||
// fmt.Printf("Starting server at %s:%d...\n", cnf.Host, cnf.Port)
|
||||
// return server
|
||||
// }
|
13
server/faq/internal/config/config.go
Normal file
13
server/faq/internal/config/config.go
Normal file
@ -0,0 +1,13 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fusenapi/server/faq/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
SourceMysql string
|
||||
Auth types.Auth
|
||||
}
|
22
server/faq/internal/handler/routes.go
Normal file
22
server/faq/internal/handler/routes.go
Normal file
@ -0,0 +1,22 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"fusenapi/server/faq/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/web-set/setting",
|
||||
Handler: WetSetSettingHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
78
server/faq/internal/handler/wetsetsettinghandler.go
Normal file
78
server/faq/internal/handler/wetsetsettinghandler.go
Normal file
@ -0,0 +1,78 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"fusenapi/server/faq/internal/logic"
|
||||
"fusenapi/server/faq/internal/svc"
|
||||
"fusenapi/server/faq/internal/types"
|
||||
)
|
||||
|
||||
func WetSetSettingHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var (
|
||||
// 定义错误变量
|
||||
err error
|
||||
// 定义用户信息变量
|
||||
userinfo *auth.UserInfo
|
||||
)
|
||||
// 解析JWT token,并对空用户进行判断
|
||||
claims, err := svcCtx.ParseJwtToken(r)
|
||||
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
|
||||
if err != nil {
|
||||
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
|
||||
Code: 401, // 返回401状态码,表示未授权
|
||||
Message: "unauthorized", // 返回未授权信息
|
||||
})
|
||||
logx.Info("unauthorized:", err.Error()) // 记录错误日志
|
||||
return
|
||||
}
|
||||
|
||||
if claims != nil {
|
||||
// 从token中获取对应的用户信息
|
||||
userinfo, err = auth.GetUserInfoFormMapClaims(claims)
|
||||
// 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息
|
||||
if err != nil {
|
||||
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
|
||||
Code: 401,
|
||||
Message: "unauthorized",
|
||||
})
|
||||
logx.Info("unauthorized:", err.Error())
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// 如果claims为nil,则认为用户身份为白板用户
|
||||
userinfo = &auth.UserInfo{UserId: 0, GuestId: 0}
|
||||
}
|
||||
|
||||
var req types.Request
|
||||
// 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
|
||||
Code: 510,
|
||||
Message: "parameter error",
|
||||
})
|
||||
logx.Info(err)
|
||||
return
|
||||
}
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewWetSetSettingLogic(r.Context(), svcCtx)
|
||||
resp := l.WetSetSetting(&req, userinfo)
|
||||
// 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应;
|
||||
if resp != nil {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
} else {
|
||||
err := errors.New("server logic is error, resp must not be nil")
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
logx.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
34
server/faq/internal/logic/wetsetsettinglogic.go
Normal file
34
server/faq/internal/logic/wetsetsettinglogic.go
Normal file
@ -0,0 +1,34 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/faq/internal/svc"
|
||||
"fusenapi/server/faq/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type WetSetSettingLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewWetSetSettingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WetSetSettingLogic {
|
||||
return &WetSetSettingLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *WetSetSettingLogic) WetSetSetting(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
|
||||
return resp.SetStatus(basic.CodeOK)
|
||||
}
|
56
server/faq/internal/svc/servicecontext.go
Normal file
56
server/faq/internal/svc/servicecontext.go
Normal file
@ -0,0 +1,56 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"fusenapi/server/faq/internal/config"
|
||||
"net/http"
|
||||
|
||||
"fusenapi/initalize"
|
||||
"fusenapi/model/gmodel"
|
||||
|
||||
"github.com/golang-jwt/jwt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
|
||||
MysqlConn *gorm.DB
|
||||
AllModels *gmodel.AllModelsGen
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
MysqlConn: initalize.InitMysql(c.SourceMysql),
|
||||
AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)),
|
||||
}
|
||||
}
|
||||
|
||||
func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) {
|
||||
AuthKey := r.Header.Get("Authorization")
|
||||
if len(AuthKey) <= 50 {
|
||||
return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey)))
|
||||
}
|
||||
|
||||
token, err := jwt.Parse(AuthKey, func(token *jwt.Token) (interface{}, error) {
|
||||
// 检查签名方法是否为 HS256
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
// 返回用于验证签名的密钥
|
||||
return []byte(svcCtx.Config.Auth.AccessSecret), nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprint("Error parsing token:", err))
|
||||
}
|
||||
|
||||
// 验证成功返回
|
||||
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
|
||||
return claims, nil
|
||||
}
|
||||
|
||||
return nil, errors.New(fmt.Sprint("Invalid token", err))
|
||||
}
|
61
server/faq/internal/types/types.go
Normal file
61
server/faq/internal/types/types.go
Normal file
@ -0,0 +1,61 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
package types
|
||||
|
||||
import (
|
||||
"fusenapi/utils/basic"
|
||||
)
|
||||
|
||||
type Request struct {
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
type Auth struct {
|
||||
AccessSecret string `json:"accessSecret"`
|
||||
AccessExpire int64 `json:"accessExpire"`
|
||||
RefreshAfter int64 `json:"refreshAfter"`
|
||||
}
|
||||
|
||||
// Set 设置Response的Code和Message值
|
||||
func (resp *Response) Set(Code int, Message string) *Response {
|
||||
return &Response{
|
||||
Code: Code,
|
||||
Message: Message,
|
||||
}
|
||||
}
|
||||
|
||||
// Set 设置整个Response
|
||||
func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response {
|
||||
return &Response{
|
||||
Code: Code,
|
||||
Message: Message,
|
||||
Data: Data,
|
||||
}
|
||||
}
|
||||
|
||||
// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数
|
||||
func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) *Response {
|
||||
newResp := &Response{
|
||||
Code: sr.Code,
|
||||
}
|
||||
if len(data) == 1 {
|
||||
newResp.Data = data[0]
|
||||
}
|
||||
return newResp
|
||||
}
|
||||
|
||||
// SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数
|
||||
func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) *Response {
|
||||
newResp := &Response{
|
||||
Code: sr.Code,
|
||||
Message: msg,
|
||||
}
|
||||
if len(data) == 1 {
|
||||
newResp.Data = data[0]
|
||||
}
|
||||
return newResp
|
||||
}
|
@ -125,10 +125,10 @@ func (l *UserOderDeleteLogic) UserOderDelete(req *types.RequestOrderId, userinfo
|
||||
Uploads: stripe.GetBackendWithConfig(stripe.UploadsBackend, config),
|
||||
})
|
||||
// ['order_number' => $order->sn, 'is_refund' => 0, 'pay_status' => 1]
|
||||
pay := l.svcCtx.AllModels.FsPay
|
||||
payM := l.svcCtx.AllModels.FsPay
|
||||
|
||||
// 查询支付信息
|
||||
pays, err := pay.GetOrderPayList(l.ctx, *order.Sn, 1, 0)
|
||||
pays, err := payM.GetOrderPayList(l.ctx, *order.Sn, 1, 0)
|
||||
for _, pay := range pays {
|
||||
sc.Refunds.New(&stripe.RefundParams{
|
||||
PaymentIntent: pay.TradeNo,
|
||||
@ -140,12 +140,11 @@ func (l *UserOderDeleteLogic) UserOderDelete(req *types.RequestOrderId, userinfo
|
||||
}
|
||||
}
|
||||
|
||||
// if err == nil && err == nil {
|
||||
// return ResponseSuccess(200, "Cancel successfully", order)
|
||||
// }
|
||||
return resp.SetStatus(basic.CodePayCancelOk, uOrder)
|
||||
|
||||
// return ResponseError(500, "Cancellation failure")
|
||||
|
||||
return resp.SetStatus(basic.CodePayCancelNotOk)
|
||||
// return resp.SetStatus(basic.CodePayCancelNotOk)
|
||||
}
|
||||
|
||||
// func (l *OrderLogic) CancelOrder(req *types.RequestCancelOrder, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
|
@ -6,7 +6,6 @@ import (
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"gorm.io/gorm"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@ -43,66 +42,44 @@ func (l *SaveMapLibraryLogic) SaveMapLibrary(userinfo *auth.UserInfo, req *http.
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeSaveErr, "param err")
|
||||
}
|
||||
//获取所有贴图数据[获取id]
|
||||
mapLibraryModel := gmodel.NewFsMapLibraryModel(l.svcCtx.MysqlConn)
|
||||
maplibraryList, err := mapLibraryModel.GetAllEnabledList(l.ctx, "id")
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get map library list")
|
||||
}
|
||||
|
||||
sort := int64(0)
|
||||
status := int64(1)
|
||||
now := time.Now().Unix()
|
||||
needDeleteMid := make([]int64, 0, len(postData))
|
||||
mapPostMid := make(map[int64]struct{})
|
||||
createList := make([]gmodel.FsMapLibrary, 0, len(postData))
|
||||
updateList := make([]gmodel.FsMapLibrary, 0, len(postData))
|
||||
//开启事务
|
||||
err = l.svcCtx.MysqlConn.Transaction(func(tx *gorm.DB) error {
|
||||
for _, v := range postData {
|
||||
infoByte, _ := json.Marshal(v.Info)
|
||||
postDataVal := v
|
||||
infoByte, _ := json.Marshal(postDataVal.Info)
|
||||
infoJsonStr := string(infoByte)
|
||||
switch v.Mid {
|
||||
switch postDataVal.Mid {
|
||||
case "": //新增
|
||||
err = mapLibraryModel.Create(l.ctx, &gmodel.FsMapLibrary{
|
||||
Title: &v.Info.Title,
|
||||
createList = append(createList, gmodel.FsMapLibrary{
|
||||
Title: &postDataVal.Info.Title,
|
||||
Info: &infoJsonStr,
|
||||
Sort: &sort,
|
||||
Status: &status,
|
||||
Ctime: &now,
|
||||
TagId: &v.Tag.Id,
|
||||
TagId: &postDataVal.Tag.Id,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
default: //修改
|
||||
midInt, err := strconv.ParseInt(v.Mid, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mapPostMid[midInt] = struct{}{}
|
||||
err = mapLibraryModel.Update(l.ctx, midInt, &gmodel.FsMapLibrary{
|
||||
Title: &v.Info.Title,
|
||||
Info: &infoJsonStr,
|
||||
TagId: &v.Tag.Id,
|
||||
Status: &status,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
//删除需要删除的
|
||||
for _, v := range maplibraryList {
|
||||
//旧的不在新的里面则删除
|
||||
if _, ok := mapPostMid[v.Id]; !ok {
|
||||
needDeleteMid = append(needDeleteMid, v.Id)
|
||||
}
|
||||
}
|
||||
return mapLibraryModel.ChangeStatusByIds(l.ctx, needDeleteMid, 0)
|
||||
})
|
||||
midInt, err := strconv.ParseInt(postDataVal.Mid, 10, 64)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeSaveErr, "failed to save map library info")
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "mid is not a number")
|
||||
}
|
||||
updateList = append(updateList, gmodel.FsMapLibrary{
|
||||
Id: midInt,
|
||||
Title: &postDataVal.Info.Title,
|
||||
Info: &infoJsonStr,
|
||||
TagId: &postDataVal.Tag.Id,
|
||||
})
|
||||
}
|
||||
}
|
||||
err = l.svcCtx.AllModels.FsMapLibrary.SaveMapLibraryWithTransaction(l.ctx, createList, updateList)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to save map library")
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success")
|
||||
}
|
||||
|
@ -22,6 +22,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
Path: "/product-template/base-map-list",
|
||||
Handler: GetBaseMapListHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/product-template/base-map-update",
|
||||
Handler: SaveBaseMapHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
@ -0,0 +1,28 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
|
||||
"fusenapi/server/product-templatev2/internal/logic"
|
||||
"fusenapi/server/product-templatev2/internal/svc"
|
||||
)
|
||||
|
||||
func SaveBaseMapHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewSaveBaseMapLogic(r.Context(), svcCtx)
|
||||
resp := l.SaveBaseMap(r)
|
||||
// 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应;
|
||||
if resp != nil {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
} else {
|
||||
err := errors.New("server logic is error, resp must not be nil")
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
logx.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
79
server/product-templatev2/internal/logic/savebasemaplogic.go
Normal file
79
server/product-templatev2/internal/logic/savebasemaplogic.go
Normal file
@ -0,0 +1,79 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/server/product-templatev2/internal/types"
|
||||
"fusenapi/utils/basic"
|
||||
"gorm.io/gorm"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/product-templatev2/internal/svc"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type SaveBaseMapLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewSaveBaseMapLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveBaseMapLogic {
|
||||
return &SaveBaseMapLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SaveBaseMapLogic) SaveBaseMap(r *http.Request) (resp *basic.Response) {
|
||||
authKey := r.Header.Get("Auth-Key")
|
||||
genentModel := gmodel.NewFsGerentModel(l.svcCtx.MysqlConn)
|
||||
_, err := genentModel.Find(l.ctx, authKey)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first..")
|
||||
}
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeUnAuth, "failed to get user info")
|
||||
}
|
||||
bodyBytes, err := ioutil.ReadAll(r.Body)
|
||||
defer r.Body.Close()
|
||||
var postData []types.SaveBaseMapReq
|
||||
if err = json.Unmarshal(bodyBytes, &postData); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid request param")
|
||||
}
|
||||
//空数组
|
||||
if len(postData) == 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param can`t be empty array")
|
||||
}
|
||||
updDataArr := make([]gmodel.FsProductTemplateBasemap, 0, len(postData))
|
||||
for _, v := range postData {
|
||||
val := v
|
||||
ctimeT, err := time.ParseInLocation("2006-01-02 15:04:05", v.Ctime, time.Local)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err create time format")
|
||||
}
|
||||
ctime := ctimeT.Unix()
|
||||
updDataArr = append(updDataArr, gmodel.FsProductTemplateBasemap{
|
||||
Id: val.Id,
|
||||
Name: &val.Name,
|
||||
Url: &val.Url,
|
||||
Ctime: &ctime,
|
||||
})
|
||||
}
|
||||
//更新
|
||||
err = l.svcCtx.AllModels.FsProductTemplateBasemap.UpdateBaseMapWithTransaction(l.ctx, updDataArr)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to update base map")
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success")
|
||||
}
|
@ -35,6 +35,13 @@ type GetBaseMapListRsp struct {
|
||||
Ctime string `json:"ctime"`
|
||||
}
|
||||
|
||||
type SaveBaseMapReq struct {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Url string `json:"url"`
|
||||
Ctime string `json:"ctime"`
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"msg"`
|
||||
|
@ -7,15 +7,7 @@ info (
|
||||
email: ""
|
||||
)
|
||||
|
||||
type request {
|
||||
// TODO: add members here and delete this comment
|
||||
Name string `path:"name,options=you|me"` // parameters are auto validated
|
||||
}
|
||||
|
||||
type response {
|
||||
// TODO: add members here and delete this comment
|
||||
Message string `json:"message"`
|
||||
}
|
||||
import "basic.api"
|
||||
|
||||
service user-auth {
|
||||
@handler GreetHandler
|
||||
|
@ -7,6 +7,9 @@ info (
|
||||
email: ""
|
||||
)
|
||||
|
||||
type request {}
|
||||
|
||||
|
||||
|
||||
// response 统一返回码
|
||||
type response {
|
||||
@ -15,17 +18,6 @@ type response {
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
|
||||
// responseJwt 统一返回码
|
||||
type responseJwt {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
AccessSecret string `json:"accessSecret"`
|
||||
AccessExpire int64 `json:"accessExpire"`
|
||||
}
|
||||
|
||||
|
||||
// Auth 认证结构
|
||||
type Auth {
|
||||
AccessSecret string `json:"accessSecret"`
|
||||
|
@ -7,6 +7,7 @@ info (
|
||||
email: ""
|
||||
)
|
||||
import "basic.api"
|
||||
|
||||
service canteen {
|
||||
//获取餐厅详情
|
||||
@handler GetCanteenDetailHandler
|
||||
|
@ -7,15 +7,7 @@ info (
|
||||
email: ""
|
||||
)
|
||||
|
||||
type request {
|
||||
// TODO: add members here and delete this comment
|
||||
Name string `path:"name,options=you|me"` // parameters are auto validated
|
||||
}
|
||||
|
||||
type response {
|
||||
// TODO: add members here and delete this comment
|
||||
Message string `json:"message"`
|
||||
}
|
||||
import "basic.api"
|
||||
|
||||
service user-auth {
|
||||
@handler GreetHandler
|
||||
|
@ -7,15 +7,7 @@ info (
|
||||
email: ""
|
||||
)
|
||||
|
||||
type request {
|
||||
// TODO: add members here and delete this comment
|
||||
Name string `path:"name,options=you|me"` // parameters are auto validated
|
||||
}
|
||||
|
||||
type response {
|
||||
// TODO: add members here and delete this comment
|
||||
Message string `json:"message"`
|
||||
}
|
||||
import "basic.api"
|
||||
|
||||
service user-auth {
|
||||
@handler GreetHandler
|
||||
|
@ -7,15 +7,7 @@ info (
|
||||
email: ""
|
||||
)
|
||||
|
||||
type request {
|
||||
// TODO: add members here and delete this comment
|
||||
Name string `path:"name,options=you|me"` // parameters are auto validated
|
||||
}
|
||||
|
||||
type response {
|
||||
// TODO: add members here and delete this comment
|
||||
Message string `json:"message"`
|
||||
}
|
||||
import "basic.api"
|
||||
|
||||
service user-auth {
|
||||
@handler GreetHandler
|
||||
|
@ -7,17 +7,9 @@ info (
|
||||
email: ""
|
||||
)
|
||||
|
||||
type request {
|
||||
// TODO: add members here and delete this comment
|
||||
Name string `path:"name,options=you|me"` // parameters are auto validated
|
||||
}
|
||||
import "basic.api"
|
||||
|
||||
type response {
|
||||
// TODO: add members here and delete this comment
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
service user-auth {
|
||||
@handler GreetHandler
|
||||
get /greet/from/:name(request) returns (response);
|
||||
service faq {
|
||||
@handler WetSetSettingHandler
|
||||
get /web-set/setting(request) returns (response);
|
||||
}
|
@ -42,6 +42,9 @@ service home-user-auth {
|
||||
@handler UserAddAddressHandler
|
||||
post /user/add-address(RequestAddAddress) returns (response);
|
||||
|
||||
// @handler UserOderListHandler
|
||||
// get /user/order-list(RequestOrderId) returns (response);
|
||||
|
||||
@handler UserOderDeleteHandler
|
||||
post /user/order-delete(RequestOrderId) returns (response);
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ info (
|
||||
author: ""
|
||||
email: ""
|
||||
)
|
||||
|
||||
import "basic.api"
|
||||
|
||||
service map-library {
|
||||
|
@ -6,7 +6,9 @@ info (
|
||||
author: ""
|
||||
email: ""
|
||||
)
|
||||
|
||||
import "basic.api"
|
||||
|
||||
service orders {
|
||||
//获取订单发票
|
||||
@handler GetOrderInvoiceHandler
|
||||
|
@ -6,7 +6,9 @@ info (
|
||||
author: ""
|
||||
email: ""
|
||||
)
|
||||
|
||||
import "basic.api"
|
||||
|
||||
service product-templatev2 {
|
||||
//获取产品模板详情
|
||||
@handler GetTemplatevDetailHandler
|
||||
@ -14,6 +16,10 @@ service product-templatev2 {
|
||||
//获取底图列表
|
||||
@handler GetBaseMapListHandler
|
||||
get /product-template/base-map-list( ) returns (response);
|
||||
//保存底图信息
|
||||
@handler SaveBaseMapHandler
|
||||
post /product-template/base-map-update ( ) returns (response);
|
||||
|
||||
}
|
||||
|
||||
//获取产品模板详情
|
||||
@ -45,3 +51,10 @@ type GetBaseMapListRsp {
|
||||
Url string `json:"url"`
|
||||
Ctime string `json:"ctime"`
|
||||
}
|
||||
//保存底图信息
|
||||
type SaveBaseMapReq {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Url string `json:"url"`
|
||||
Ctime string `json:"ctime"`
|
||||
}
|
@ -6,7 +6,9 @@ info (
|
||||
author: ""
|
||||
email: ""
|
||||
)
|
||||
|
||||
import "basic.api"
|
||||
|
||||
service product {
|
||||
//获取产品列表
|
||||
@handler GetProductListHandler
|
||||
|
@ -7,15 +7,7 @@ info (
|
||||
email: ""
|
||||
)
|
||||
|
||||
type request {
|
||||
// TODO: add members here and delete this comment
|
||||
Name string `path:"name,options=you|me"` // parameters are auto validated
|
||||
}
|
||||
|
||||
type response {
|
||||
// TODO: add members here and delete this comment
|
||||
Message string `json:"message"`
|
||||
}
|
||||
import "basic.api"
|
||||
|
||||
service user-auth {
|
||||
@handler GreetHandler
|
||||
|
@ -6,7 +6,9 @@ info (
|
||||
author: "ldm"
|
||||
email: ""
|
||||
)
|
||||
|
||||
import "basic.api"
|
||||
|
||||
service shopping-cart-confirmation {
|
||||
//添加入购物车
|
||||
@handler CartAddHandler
|
||||
|
@ -116,6 +116,7 @@ func (resp *Response) SetStatusAddMessage(sr *StatusResponse, msg string, data .
|
||||
Code: sr.Code,
|
||||
Message: sr.Message + ":" + msg,
|
||||
}
|
||||
|
||||
switch len(data) {
|
||||
case 0:
|
||||
// 0 直接返回
|
||||
|
Loading…
x
Reference in New Issue
Block a user