liujiandao
2023-10-13 f65ce9c19568745003b22e82060fb38c2885c701
Merge branch 'master' of http://192.168.5.5:10010/r/aps/crm

# Conflicts:
# api/v1/contact.go
# api/v1/salesDetails.go
# model/quotation.go
# model/saleChance.go
# service/salesDetails.go
3个文件已添加
54个文件已修改
1765 ■■■■■ 已修改文件
api/v1/captcha.go 104 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/client.go 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/contact.go 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/contract.go 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/followRecord.go 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/masterOrder.go 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/quotation.go 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/saleChance.go 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/salesDetails.go 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/salesLeads.go 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/salesRefund.go 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/salesReturn.go 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/serviceContract.go 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/serviceFollowup.go 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/serviceOrder.go 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/subOrder.go 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/user.go 723 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
conf/aps-crm.json 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
constvar/const.go 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
go.mod 5 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
go.sum 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
main.go 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
middleware/interceptor.go 21 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
middleware/jwt.go 20 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
middleware/user.go 78 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/client.go 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/contact.go 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/contract.go 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/followRecord.go 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/masterOrder.go 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/quotation.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/request/jwt.go 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/saleChance.go 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/salesDetails.go 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/salesRefund.go 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/salesReturn.go 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/serviceContract.go 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/serviceFollowup.go 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/serviceOrder.go 24 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/subOrder.go 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/user.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
proto/user.proto 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
proto/user/user.pb.go 194 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
proto/user/user_grpc.pb.go 49 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
router/base.go 32 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
router/image.go 31 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
router/index.go 25 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
router/user.go 48 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/lru.go 40 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/salesDetails.go 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/salesRefund.go 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/salesReturn.go 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/serviceContract.go 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/serviceFollowup.go 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/serviceOrder.go 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/user.go 55 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
utils/clamis.go 24 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/captcha.go
@@ -1,62 +1,50 @@
package v1
import (
    "aps_crm/conf"
    "aps_crm/constvar"
    "aps_crm/model/response"
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/convertx"
    "aps_crm/pkg/ecode"
    "aps_crm/pkg/logx"
    "github.com/gin-gonic/gin"
    "github.com/mojocn/base64Captcha"
    "time"
)
type BaseApi struct{}
// Captcha
//    @Tags        Base
//    @Summary    获取验证码
//    @Produce    application/json
//    @Success    200    {object}    contextx.Response{data=response.LoginResponse}    "成功"
//    @Router        /api/base/captcha [post]
func (slf *BaseApi) Captcha(c *gin.Context) {
    ctx, ok := contextx.NewContext(c, nil)
    if !ok {
        return
    }
    // 判断验证码是否开启
    openCaptcha := conf.Conf.Captcha.OpenCaptcha               // 是否开启防爆次数
    openCaptchaTimeOut := conf.Conf.Captcha.OpenCaptchaTimeOut // 缓存超时时间
    key := c.ClientIP()
    v, ok := constvar.BlackCache.Get(key)
    if !ok {
        constvar.BlackCache.Set(key, 1, time.Second*time.Duration(openCaptchaTimeOut))
    }
    var oc bool
    if openCaptcha == 0 || convertx.InterfaceToInt(v) > openCaptcha { // 0 表示每次登录都需要验证码 或者当前次数已超过防爆次数
        oc = true
    }
    // 字符,公式,验证码配置
    // 生成默认数字的driver
    driver := base64Captcha.NewDriverDigit(conf.Conf.Captcha.ImgHeight, conf.Conf.Captcha.ImgWidth, conf.Conf.Captcha.KeyLong, 0.7, 80)
    cp := base64Captcha.NewCaptcha(driver, store)
    id, b64s, err := cp.Generate()
    captcha := store.Get(id, false)
    logx.Infof("Captcha Generate captchaId:%v captcha:%v", id, captcha)
    if err != nil {
        logx.Errorf("Captcha Generate err:%v", err)
        ctx.Fail(ecode.CaptchaGenerateFailed)
        return
    }
    ctx.OkWithDetailed(response.CaptchaResponse{
        CaptchaId:     id,
        PicPath:       b64s,
        CaptchaLength: conf.Conf.Captcha.KeyLong,
        OpenCaptcha:   oc,
    })
}
//
//// Captcha
////    @Tags        Base
////    @Summary    获取验证码
////    @Produce    application/json
////    @Success    200    {object}    contextx.Response{data=response.LoginResponse}    "成功"
////    @Router        /api/base/captcha [post]
//func (slf *BaseApi) Captcha(c *gin.Context) {
//    ctx, ok := contextx.NewContext(c, nil)
//    if !ok {
//        return
//    }
//
//    // 判断验证码是否开启
//    openCaptcha := conf.Conf.Captcha.OpenCaptcha               // 是否开启防爆次数
//    openCaptchaTimeOut := conf.Conf.Captcha.OpenCaptchaTimeOut // 缓存超时时间
//    key := c.ClientIP()
//    v, ok := constvar.BlackCache.Get(key)
//    if !ok {
//        constvar.BlackCache.Set(key, 1, time.Second*time.Duration(openCaptchaTimeOut))
//    }
//
//    var oc bool
//    if openCaptcha == 0 || convertx.InterfaceToInt(v) > openCaptcha { // 0 表示每次登录都需要验证码 或者当前次数已超过防爆次数
//        oc = true
//    }
//    // 字符,公式,验证码配置
//    // 生成默认数字的driver
//    driver := base64Captcha.NewDriverDigit(conf.Conf.Captcha.ImgHeight, conf.Conf.Captcha.ImgWidth, conf.Conf.Captcha.KeyLong, 0.7, 80)
//    cp := base64Captcha.NewCaptcha(driver, store)
//    id, b64s, err := cp.Generate()
//    captcha := store.Get(id, false)
//    logx.Infof("Captcha Generate captchaId:%v captcha:%v", id, captcha)
//    if err != nil {
//        logx.Errorf("Captcha Generate err:%v", err)
//        ctx.Fail(ecode.CaptchaGenerateFailed)
//        return
//    }
//
//    ctx.OkWithDetailed(response.CaptchaResponse{
//        CaptchaId:     id,
//        PicPath:       b64s,
//        CaptchaLength: conf.Conf.Captcha.KeyLong,
//        OpenCaptcha:   oc,
//    })
//}
api/v1/client.go
@@ -1,11 +1,13 @@
package v1
import (
    "aps_crm/constvar"
    "aps_crm/model"
    "aps_crm/model/request"
    "aps_crm/model/response"
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/ecode"
    "aps_crm/utils"
    "github.com/gin-gonic/gin"
)
@@ -206,6 +208,14 @@
        return
    }
    userInfo := utils.GetUserInfo(c)
    if userInfo.UserType == constvar.UserTypeSub {
        if params.SearchMap == nil {
            params.SearchMap = make(map[string]interface{}, 0)
        }
        params.SearchMap["member_id"] = userInfo.CrmUserId
    }
    clients, total, errCode := clientService.GetClientList(params.Page, params.PageSize, params.SearchMap)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
api/v1/contact.go
@@ -1,11 +1,13 @@
package v1
import (
    "aps_crm/constvar"
    "aps_crm/model"
    "aps_crm/model/request"
    "aps_crm/model/response"
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/ecode"
    "aps_crm/utils"
    "github.com/gin-gonic/gin"
)
@@ -167,6 +169,14 @@
        return
    }
    userInfo := utils.GetUserInfo(c)
    if userInfo.UserType == constvar.UserTypeSub {
        if params.SearchMap == nil {
            params.SearchMap = make(map[string]interface{}, 0)
        }
        params.SearchMap["member_id"] = userInfo.CrmUserId
    }
    contacts, total, errCode := contactService.GetContactList(params.Page, params.PageSize, params.SearchMap, params.ClientId)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
api/v1/contract.go
@@ -1,11 +1,13 @@
package v1
import (
    "aps_crm/constvar"
    "aps_crm/model"
    "aps_crm/model/request"
    "aps_crm/model/response"
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/ecode"
    "aps_crm/utils"
    "github.com/gin-gonic/gin"
)
@@ -142,6 +144,14 @@
        return
    }
    userInfo := utils.GetUserInfo(c)
    if userInfo.UserType == constvar.UserTypeSub {
        if params.SearchMap == nil {
            params.SearchMap = make(map[string]interface{}, 0)
        }
        params.SearchMap["member_id"] = userInfo.CrmUserId
    }
    contracts, total, errCode := contractService.GetContractList(params.Page, params.PageSize, params.SearchMap)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
api/v1/followRecord.go
@@ -1,11 +1,13 @@
package v1
import (
    "aps_crm/constvar"
    "aps_crm/model"
    "aps_crm/model/request"
    "aps_crm/model/response"
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/ecode"
    "aps_crm/utils"
    "errors"
    "github.com/gin-gonic/gin"
    "time"
@@ -229,6 +231,14 @@
        return
    }
    userInfo := utils.GetUserInfo(c)
    if userInfo.UserType == constvar.UserTypeSub {
        if params.SearchMap == nil {
            params.SearchMap = make(map[string]interface{}, 0)
        }
        params.SearchMap["member_id"] = userInfo.CrmUserId
    }
    followRecords, total, errCode := followRecordService.GetFollowRecordList(params.Page, params.PageSize, params.SearchMap)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
api/v1/masterOrder.go
@@ -1,11 +1,13 @@
package v1
import (
    "aps_crm/constvar"
    "aps_crm/model"
    "aps_crm/model/request"
    "aps_crm/model/response"
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/ecode"
    "aps_crm/utils"
    "github.com/gin-gonic/gin"
)
@@ -152,6 +154,14 @@
        return
    }
    userInfo := utils.GetUserInfo(c)
    if userInfo.UserType == constvar.UserTypeSub {
        if params.SearchMap == nil {
            params.SearchMap = make(map[string]interface{}, 0)
        }
        params.SearchMap["member_id"] = userInfo.CrmUserId
    }
    masterOrders, total, errCode := masterOrderService.GetMasterOrderList(params.Page, params.PageSize, params.SearchMap)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
api/v1/quotation.go
@@ -1,11 +1,13 @@
package v1
import (
    "aps_crm/constvar"
    "aps_crm/model"
    "aps_crm/model/request"
    "aps_crm/model/response"
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/ecode"
    "aps_crm/utils"
    "github.com/gin-gonic/gin"
)
@@ -169,6 +171,14 @@
        return
    }
    userInfo := utils.GetUserInfo(c)
    if userInfo.UserType == constvar.UserTypeSub {
        if params.SearchMap == nil {
            params.SearchMap = make(map[string]interface{}, 0)
        }
        params.SearchMap["member_id"] = userInfo.CrmUserId
    }
    quotations, total, errCode := quotationService.GetQuotationList(params.Page, params.PageSize, params.SearchMap)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
api/v1/saleChance.go
@@ -1,11 +1,13 @@
package v1
import (
    "aps_crm/constvar"
    "aps_crm/model"
    "aps_crm/model/request"
    "aps_crm/model/response"
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/ecode"
    "aps_crm/utils"
    "github.com/gin-gonic/gin"
)
@@ -220,6 +222,14 @@
        return
    }
    userInfo := utils.GetUserInfo(c)
    if userInfo.UserType == constvar.UserTypeSub {
        if params.SearchMap == nil {
            params.SearchMap = make(map[string]interface{}, 0)
        }
        params.SearchMap["member_id"] = userInfo.CrmUserId
    }
    saleChances, total, errCode := saleChanceService.GetSaleChanceList(params.Page, params.PageSize, params.SearchMap)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
api/v1/salesDetails.go
@@ -1,11 +1,13 @@
package v1
import (
    "aps_crm/constvar"
    "aps_crm/model"
    "aps_crm/model/request"
    "aps_crm/model/response"
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/ecode"
    "aps_crm/utils"
    "github.com/gin-gonic/gin"
    "strconv"
)
@@ -166,6 +168,12 @@
        return
    }
    var memberIds []int
    userInfo := utils.GetUserInfo(c)
    if userInfo.UserType == constvar.UserTypeSub {
        memberIds = []int{userInfo.CrmUserId}
    }
    salesDetailss, total, errCode := salesDetailsService.GetSalesDetailsList(params)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
api/v1/salesLeads.go
@@ -7,6 +7,7 @@
    "aps_crm/model/response"
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/ecode"
    "aps_crm/utils"
    "github.com/gin-gonic/gin"
)
@@ -160,6 +161,14 @@
        return
    }
    userInfo := utils.GetUserInfo(c)
    if userInfo.UserType == constvar.UserTypeSub {
        if params.SearchMap == nil {
            params.SearchMap = make(map[string]interface{}, 0)
        }
        params.SearchMap["member_id"] = userInfo.CrmUserId
    }
    salesLeadss, total, errCode := salesLeadsService.GetSalesLeadsList(params.Page, params.PageSize, params.SearchMap)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
api/v1/salesRefund.go
@@ -1,11 +1,13 @@
package v1
import (
    "aps_crm/constvar"
    "aps_crm/model"
    "aps_crm/model/request"
    "aps_crm/model/response"
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/ecode"
    "aps_crm/utils"
    "github.com/gin-gonic/gin"
    "strconv"
    "strings"
@@ -170,7 +172,13 @@
        return
    }
    salesRefunds, total, errCode := salesRefundService.GetSalesRefundList(params.Page, params.PageSize, params.KeywordType, params.Keyword, params.SourceId)
    var memberIds []int
    userInfo := utils.GetUserInfo(c)
    if userInfo.UserType == constvar.UserTypeSub {
        memberIds = []int{userInfo.CrmUserId}
    }
    salesRefunds, total, errCode := salesRefundService.GetSalesRefundList(params.Page, params.PageSize, params.KeywordType, params.Keyword, params.SourceId, memberIds)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
api/v1/salesReturn.go
@@ -1,12 +1,14 @@
package v1
import (
    "aps_crm/constvar"
    "aps_crm/model"
    "aps_crm/model/request"
    "aps_crm/model/response"
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/ecode"
    "aps_crm/pkg/structx"
    "aps_crm/utils"
    "github.com/gin-gonic/gin"
    "strconv"
    "strings"
@@ -165,7 +167,13 @@
        return
    }
    salesReturns, total, errCode := salesReturnService.GetSalesReturnList(params)
    var memberIds []int
    userInfo := utils.GetUserInfo(c)
    if userInfo.UserType == constvar.UserTypeSub {
        memberIds = []int{userInfo.CrmUserId}
    }
    salesReturns, total, errCode := salesReturnService.GetSalesReturnList(params, memberIds)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
api/v1/serviceContract.go
@@ -1,12 +1,14 @@
package v1
import (
    "aps_crm/constvar"
    "aps_crm/model"
    "aps_crm/model/request"
    "aps_crm/model/response"
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/ecode"
    "aps_crm/pkg/structx"
    "aps_crm/utils"
    "github.com/gin-gonic/gin"
)
@@ -144,7 +146,13 @@
        return
    }
    serviceContracts, total, errCode := serviceContractService.GetServiceContractList(params)
    var memberIds []int
    userInfo := utils.GetUserInfo(c)
    if userInfo.UserType == constvar.UserTypeSub {
        memberIds = []int{userInfo.CrmUserId}
    }
    serviceContracts, total, errCode := serviceContractService.GetServiceContractList(params, memberIds)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
api/v1/serviceFollowup.go
@@ -1,11 +1,13 @@
package v1
import (
    "aps_crm/constvar"
    "aps_crm/model"
    "aps_crm/model/request"
    "aps_crm/model/response"
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/ecode"
    "aps_crm/utils"
    "github.com/gin-gonic/gin"
)
@@ -151,7 +153,13 @@
        return
    }
    serviceFollowups, total, errCode := serviceFollowupService.GetServiceFollowupList(params.Page, params.PageSize, params.KeywordType, params.Keyword, params.ServiceOrderId)
    var memberIds []int
    userInfo := utils.GetUserInfo(c)
    if userInfo.UserType == constvar.UserTypeSub {
        memberIds = []int{userInfo.CrmUserId}
    }
    serviceFollowups, total, errCode := serviceFollowupService.GetServiceFollowupList(params.Page, params.PageSize, params.KeywordType, params.Keyword, params.ServiceOrderId, memberIds)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
api/v1/serviceOrder.go
@@ -1,6 +1,7 @@
package v1
import (
    "aps_crm/constvar"
    "aps_crm/model"
    "aps_crm/model/request"
    "aps_crm/model/response"
@@ -8,6 +9,7 @@
    "aps_crm/pkg/ecode"
    "aps_crm/pkg/structx"
    "aps_crm/service"
    "aps_crm/utils"
    "github.com/gin-gonic/gin"
    "strconv"
)
@@ -139,7 +141,13 @@
        return
    }
    serviceOrder, total, errCode := service.NewServiceOrderService().GetServiceOrderList(params.Page, params.PageSize, params.QueryClass, params.KeywordType, params.Keyword, params.ServiceContractId, params.SalesDetailsId)
    var memberIds []int
    userInfo := utils.GetUserInfo(c)
    if userInfo.UserType == constvar.UserTypeSub {
        memberIds = []int{userInfo.CrmUserId}
    }
    serviceOrder, total, errCode := service.NewServiceOrderService().GetServiceOrderList(params.Page, params.PageSize, params.QueryClass, params.KeywordType, params.Keyword, params.ServiceContractId, params.SalesDetailsId, memberIds)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
api/v1/subOrder.go
@@ -1,11 +1,13 @@
package v1
import (
    "aps_crm/constvar"
    "aps_crm/model"
    "aps_crm/model/request"
    "aps_crm/model/response"
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/ecode"
    "aps_crm/utils"
    "github.com/gin-gonic/gin"
)
@@ -140,6 +142,14 @@
        return
    }
    userInfo := utils.GetUserInfo(c)
    if userInfo.UserType == constvar.UserTypeSub {
        if params.SearchMap == nil {
            params.SearchMap = make(map[string]interface{}, 0)
        }
        params.SearchMap["member_id"] = userInfo.CrmUserId
    }
    subOrders, total, errCode := subOrderService.GetSubOrderList(params.Page, params.PageSize, params.SearchMap)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
api/v1/user.go
@@ -1,381 +1,364 @@
package v1
import (
    "aps_crm/conf"
    "aps_crm/constvar"
    "aps_crm/model"
    "aps_crm/model/request"
    "aps_crm/model/response"
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/ecode"
    "aps_crm/pkg/encrypt"
    "aps_crm/pkg/logx"
    "aps_crm/pkg/snowflake"
    "aps_crm/utils"
    "fmt"
    "github.com/gin-gonic/gin"
    "github.com/go-redis/redis/v8"
    "github.com/mojocn/base64Captcha"
)
// 当开启多服务器部署时,替换下面的配置,使用redis共享存储验证码
// var store = captcha.NewDefaultRedisStore()
var store = base64Captcha.DefaultMemStore
// Login
//
//    @Tags        Base
//    @Summary    用户登录
//    @Produce    application/json
//    @Param        object    body        request.Login                                    true    "查询参数"
//    @Success    200        {object}    contextx.Response{data=response.LoginResponse}    "成功"
//    @Router        /api/base/login [post]
func (slf *BaseApi) Login(c *gin.Context) {
    var params request.Login
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    // 判断验证码是否开启
    key := c.ClientIP()
    //openCaptcha := conf.Conf.Captcha.OpenCaptcha               // 是否开启防暴次数
    //openCaptchaTimeOut := conf.Conf.Captcha.OpenCaptchaTimeOut // 缓存超时时间
    //v, ok := constvar.BlackCache.Get(key)
    //if !ok {
    //    constvar.BlackCache.Set(key, 1, time.Second*time.Duration(openCaptchaTimeOut))
    //}
    //var oc = openCaptcha == 0 || convertx.InterfaceToInt(v) > openCaptcha // 0 表示每次登录都需要验证码 或者当前次数已超过防暴次数
    //if !oc || store.Verify(params.CaptchaId, params.Captcha, true) {
    u := &model.User{Username: params.Username, Password: params.Password}
    user, errCode := userService.Login(u)
    if errCode != ecode.OK {
        logx.Errorf("登陆失败! 用户名不存在或者密码错误! errCode:%v", errCode)
        // 验证码次数+1
        _ = constvar.BlackCache.Increment(key, 1)
        ctx.Fail(errCode)
        return
    }
    // 赋值菜单ID列表
    //user.MenuIds, _ = menuService.GetUserMenuIds(user.UUID, user.UserType)
    slf.TokenNext(ctx, *user)
    return
    //}
    // 验证码次数+1
    //_ = constvar.BlackCache.Increment(key, 1)
    //ctx.Fail(ecode.CaptchaErr)
}
// TokenNext 登录以后签发jwt
func (slf *BaseApi) TokenNext(ctx *contextx.Context, user model.User) {
    logx.Infof("TokenNext user:%+v", user)
    j := &utils.JWT{SigningKey: []byte(conf.Conf.JWT.SigningKey)} // 唯一签名
    claims := j.CreateClaims(request.BaseClaims{
        UserId:      user.UUID,
        Username:    user.Username,
        UserType:    user.UserType,
        AuthorityId: user.AuthorityId,
    })
    token, err := j.CreateToken(claims)
    if err != nil {
        logx.Errorf("创建token失败! err:%v", err)
        ctx.Fail(ecode.CreateTokenErr)
        return
    }
    if !conf.Conf.System.UseMultipoint { // 不允许多点登录
        ctx.OkWithDetailed(response.LoginResponse{
            User:  user,
            Token: token,
            //ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
        })
        return
    }
    if jwtStr, err := jwtService.GetRedisJWT(user.Username); err == redis.Nil { // redis无JWT数据
        if err := jwtService.SetRedisJWT(token, user.Username); err != nil {
            logx.Errorf("设置登录状态失败! err:%v", err)
            ctx.Fail(ecode.RedisErr)
            return
        }
        ctx.OkWithDetailed(response.LoginResponse{
            User:  user,
            Token: token,
            //ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
        })
    } else if err != nil { // redis获取JWT报错
        logx.Errorf("设置登录状态失败! err:%v", err)
        ctx.Fail(ecode.RedisErr)
    } else { // 成功获取redis的JWT,旧的作废
        var blackJWT model.JwtBlacklist
        blackJWT.Jwt = jwtStr
        if err := jwtService.JsonInBlacklist(blackJWT); err != nil {
            ctx.Fail(ecode.DBErr)
            return
        }
        if err := jwtService.SetRedisJWT(token, user.Username); err != nil {
            ctx.Fail(ecode.RedisErr)
            return
        }
        ctx.OkWithDetailed(response.LoginResponse{
            User:  user,
            Token: token,
            //ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
        })
    }
}
// Register
//// 当开启多服务器部署时,替换下面的配置,使用redis共享存储验证码
//// var store = captcha.NewDefaultRedisStore()
//var store = base64Captcha.DefaultMemStore
//
//    @Tags        User
//    @Summary    注册账号
//    @Produce    application/json
//    @Param        object    body        request.Register                                true    "查询参数"
//    @Success    200        {object}    contextx.Response{data=response.UserResponse}    "成功"
//    @Router        /api/user/register [post]
func (slf *BaseApi) Register(c *gin.Context) {
    var params request.Register
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    userInfo := utils.GetUserInfo(c)
    if len(userInfo.UserId) <= 0 {
        ctx.Fail(ecode.UnknownErr)
        return
    }
    if len(params.Username) == 0 || len(params.Password) == 0 || len(params.NickName) == 0 || params.AuthorityId == 0 || len(params.RePassword) == 0 || params.DepartmentId == 0 {
        ctx.Fail(ecode.ParamsErr)
        return
    }
    var userId = fmt.Sprintf("u%v", snowflake.GenerateId())
    var passWord = encrypt.BcryptHash(params.Password)
    var userType constvar.UserType
    user := &model.User{UUID: userId, Username: params.Username, UserType: userType, NickName: params.NickName, Password: passWord, HeaderImg: params.HeaderImg, Phone: params.Phone, Email: params.Email, DepartmentId: params.DepartmentId, AuthorityId: params.AuthorityId}
    userReturn, errCode := userService.Register(user)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
    //if user.UserType == constvar.UserTypePrimary { // 主账户创建对应的数据库用户和排程数据库
    //    err := model.NewMysql().CreateDatabase(user.Username)
    //    if err != nil {
    //        ctx.Fail(ecode.CreateDatabaseErr)
    //        return
    //    }
    //
    //    defaultPwd := fmt.Sprintf("%v@Basic2023", user.Username)
    //    err = model.NewMysql().CreateUser(user.Username, defaultPwd, user.Username)
    //    if err != nil {
    //        ctx.Fail(ecode.CreateDatabaseUserErr)
    //        return
    //    }
    //}
    ctx.OkWithDetailed(response.UserResponse{User: *userReturn})
}
// ChangePassword
//// Login
////
////    @Tags        Base
////    @Summary    用户登录
////    @Produce    application/json
////    @Param        object    body        request.Login                                    true    "查询参数"
////    @Success    200        {object}    contextx.Response{data=response.LoginResponse}    "成功"
////    @Router        /api/base/login [post]
//func (slf *BaseApi) Login(c *gin.Context) {
//    var params request.Login
//    ctx, ok := contextx.NewContext(c, &params)
//    if !ok {
//        return
//    }
//
//    @Tags        User
//    @Summary    用户修改密码
//    @Produce    application/json
//    @Param        object    body        request.ChangePasswordReq    true    "查询参数"
//    @Success    200        {object}    contextx.Response{}            "成功"
//    @Router        /api/user/changePassword [post]
func (slf *BaseApi) ChangePassword(c *gin.Context) {
    var params request.ChangePasswordReq
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    u := &model.User{UUID: utils.GetUserID(c), Password: params.Password}
    _, errCode := userService.ChangePassword(u, params.NewPassword)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
    ctx.Ok()
}
// GetUserList
//    // 判断验证码是否开启
//    key := c.ClientIP()
//    //openCaptcha := conf.Conf.Captcha.OpenCaptcha               // 是否开启防暴次数
//    //openCaptchaTimeOut := conf.Conf.Captcha.OpenCaptchaTimeOut // 缓存超时时间
//    //v, ok := constvar.BlackCache.Get(key)
//    //if !ok {
//    //    constvar.BlackCache.Set(key, 1, time.Second*time.Duration(openCaptchaTimeOut))
//    //}
//
//    @Tags        User
//    @Summary    分页获取用户列表(不传分页参数,获取全部)
//    @Produce    application/json
//    @Param        object    body        request.GetUserList                            true    "查询参数"
//    @Success    200        {object}    contextx.Response{data=response.PageResult}    "成功"
//    @Router        /api/user/getUserList [post]
func (slf *BaseApi) GetUserList(c *gin.Context) {
    var params request.GetUserList
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    userInfo := utils.GetUserInfo(c)
    if len(userInfo.UserId) <= 0 || len(userInfo.ParentId) == 0 {
        ctx.Fail(ecode.UnknownErr)
        return
    }
    ctx.OkWithDetailed(response.PageResult{
        Page:     params.Page,
        PageSize: params.PageSize,
    })
}
// DeleteUser
//    //var oc = openCaptcha == 0 || convertx.InterfaceToInt(v) > openCaptcha // 0 表示每次登录都需要验证码 或者当前次数已超过防暴次数
//
//    @Tags        User
//    @Summary    删除用户
//    @Produce    application/json
//    @Param        object    body        request.DeleteUserReq    true    "查询参数"
//    @Success    200        {object}    contextx.Response{}        "成功"
//    @Router        /api/user/deleteUser [delete]
func (slf *BaseApi) DeleteUser(c *gin.Context) {
    var params request.DeleteUserReq
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    if len(params.UserId) <= 0 {
        ctx.Fail(ecode.ParamsErr)
        return
    }
    userInfo := utils.GetUserInfo(c)
    if userInfo.UserType != constvar.UserTypePrimary || userInfo.UserId == params.UserId {
        ctx.Fail(ecode.NoPowerErr)
        return
    }
    err := userService.DeleteUser(params.UserId)
    if err != nil {
        logx.Errorf("删除失败! err:%v", err)
        ctx.Fail(ecode.DBErr)
        return
    }
    ctx.Ok()
}
// SetUserInfo
//    //if !oc || store.Verify(params.CaptchaId, params.Captcha, true) {
//    u := &model.User{Username: params.Username, Password: params.Password}
//    user, errCode := userService.Login(u)
//    if errCode != ecode.OK {
//        logx.Errorf("登陆失败! 用户名不存在或者密码错误! errCode:%v", errCode)
//        // 验证码次数+1
//        _ = constvar.BlackCache.Increment(key, 1)
//        ctx.Fail(errCode)
//        return
//    }
//    // 赋值菜单ID列表
//    //user.MenuIds, _ = menuService.GetUserMenuIds(user.UUID, user.UserType)
//    slf.TokenNext(ctx, *user)
//    return
//    //}
//
//    @Tags        User
//    @Summary    设置用户信息
//    @Produce    application/json
//    @Param        object    body        request.ChangeUserInfo    true    "查询参数"
//    @Success    200        {object}    contextx.Response{}        "成功"
//    @Router        /api/user/setUserInfo [post]
func (slf *BaseApi) SetUserInfo(c *gin.Context) {
    var params request.ChangeUserInfo
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    err := userService.SetUserInfo(model.User{
        UUID:      params.ID,
        NickName:  params.NickName,
        HeaderImg: params.HeaderImg,
        Phone:     params.Phone,
        Email:     params.Email,
        Pos:       params.Pos,
    })
    if err != nil {
        logx.Errorf("设置失败! err:%v", err)
        ctx.Fail(ecode.DBErr)
        return
    }
    ctx.Ok()
}
// SetSelfInfo
//    // 验证码次数+1
//    //_ = constvar.BlackCache.Increment(key, 1)
//    //ctx.Fail(ecode.CaptchaErr)
//}
//
//    @Tags        User
//    @Summary    设置用户信息
//    @Produce    application/json
//    @Param        object    body        request.ChangeUserInfo    true    "查询参数"
//    @Success    200        {object}    contextx.Response{}        "成功"
//    @Router        /api/user/setSelfInfo [post]
func (slf *BaseApi) SetSelfInfo(c *gin.Context) {
    var params request.ChangeUserInfo
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    params.ID = utils.GetUserID(c)
    err := userService.SetUserInfo(model.User{
        UUID:      params.ID,
        NickName:  params.NickName,
        HeaderImg: params.HeaderImg,
        Phone:     params.Phone,
        Email:     params.Email,
        Pos:       params.Pos,
    })
    if err != nil {
        logx.Errorf("设置失败! err:%v", err)
        ctx.Fail(ecode.DBErr)
        return
    }
    ctx.Ok()
}
// GetUserInfo
//// TokenNext 登录以后签发jwt
//func (slf *BaseApi) TokenNext(ctx *contextx.Context, user model.User) {
//    logx.Infof("TokenNext user:%+v", user)
//    j := &utils.JWT{SigningKey: []byte(conf.Conf.JWT.SigningKey)} // 唯一签名
//    claims := j.CreateClaims(request.BaseClaims{
//        UserId:      user.UUID,
//        Username:    user.Username,
//        UserType:    user.UserType,
//        AuthorityId: user.AuthorityId,
//    })
//    token, err := j.CreateToken(claims)
//    if err != nil {
//        logx.Errorf("创建token失败! err:%v", err)
//        ctx.Fail(ecode.CreateTokenErr)
//        return
//    }
//    if !conf.Conf.System.UseMultipoint { // 不允许多点登录
//        ctx.OkWithDetailed(response.LoginResponse{
//            User:  user,
//            Token: token,
//            //ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
//        })
//        return
//    }
//
//    @Tags        User
//    @Summary    获取自身信息
//    @Produce    application/json
//    @Success    200    {object}    contextx.Response{}    "成功"
//    @Router        /api/user/getUserInfo [post]
func (slf *BaseApi) GetUserInfo(c *gin.Context) {
    ctx, ok := contextx.NewContext(c, nil)
    if !ok {
        return
    }
    id := utils.GetUserID(c)
    ReqUser, err := userService.GetUserInfo(id)
    if err != nil {
        logx.Errorf("获取失败! err:%v", err)
        ctx.Fail(ecode.DBErr)
        return
    }
    ctx.OkWithDetailed(response.UserResponse{
        User: *ReqUser,
    })
}
// ResetPassword
//    if jwtStr, err := jwtService.GetRedisJWT(user.Username); err == redis.Nil { // redis无JWT数据
//        if err := jwtService.SetRedisJWT(token, user.Username); err != nil {
//            logx.Errorf("设置登录状态失败! err:%v", err)
//            ctx.Fail(ecode.RedisErr)
//            return
//        }
//        ctx.OkWithDetailed(response.LoginResponse{
//            User:  user,
//            Token: token,
//            //ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
//        })
//    } else if err != nil { // redis获取JWT报错
//        logx.Errorf("设置登录状态失败! err:%v", err)
//        ctx.Fail(ecode.RedisErr)
//    } else { // 成功获取redis的JWT,旧的作废
//        var blackJWT model.JwtBlacklist
//        blackJWT.Jwt = jwtStr
//        if err := jwtService.JsonInBlacklist(blackJWT); err != nil {
//            ctx.Fail(ecode.DBErr)
//            return
//        }
//        if err := jwtService.SetRedisJWT(token, user.Username); err != nil {
//            ctx.Fail(ecode.RedisErr)
//            return
//        }
//        ctx.OkWithDetailed(response.LoginResponse{
//            User:  user,
//            Token: token,
//            //ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
//        })
//    }
//}
//
//    @Tags        User
//    @Summary    重置用户密码
//    @Produce    application/json
//    @Param        object    body        model.User            true    "查询参数"
//    @Success    200        {object}    contextx.Response{}    "成功"
//    @Router        /api/user/resetPassword [post]
func (slf *BaseApi) ResetPassword(c *gin.Context) {
    var params model.User
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    err := userService.ResetPassword(params.UUID)
    if err != nil {
        logx.Errorf("重置失败! err:%v", err)
        ctx.Fail(ecode.DBErr)
        return
    }
    ctx.Ok()
}
//// Register
////
////    @Tags        User
////    @Summary    注册账号
////    @Produce    application/json
////    @Param        object    body        request.Register                                true    "查询参数"
////    @Success    200        {object}    contextx.Response{data=response.UserResponse}    "成功"
////    @Router        /api/user/register [post]
//func (slf *BaseApi) Register(c *gin.Context) {
//    var params request.Register
//    ctx, ok := contextx.NewContext(c, &params)
//    if !ok {
//        return
//    }
//
//    userInfo := utils.GetUserInfo(c)
//    if len(userInfo.UserId) <= 0 {
//        ctx.Fail(ecode.UnknownErr)
//        return
//    }
//
//    if len(params.Username) == 0 || len(params.Password) == 0 || len(params.NickName) == 0 || params.AuthorityId == 0 || len(params.RePassword) == 0 || params.DepartmentId == 0 {
//        ctx.Fail(ecode.ParamsErr)
//        return
//    }
//
//    var userId = fmt.Sprintf("u%v", snowflake.GenerateId())
//    var passWord = encrypt.BcryptHash(params.Password)
//    var userType constvar.UserType
//
//    user := &model.User{UUID: userId, Username: params.Username, UserType: userType, NickName: params.NickName, Password: passWord, HeaderImg: params.HeaderImg, Phone: params.Phone, Email: params.Email, DepartmentId: params.DepartmentId, AuthorityId: params.AuthorityId}
//    userReturn, errCode := userService.Register(user)
//    if errCode != ecode.OK {
//        ctx.Fail(errCode)
//        return
//    }
//
//    //if user.UserType == constvar.UserTypePrimary { // 主账户创建对应的数据库用户和排程数据库
//    //    err := model.NewMysql().CreateDatabase(user.Username)
//    //    if err != nil {
//    //        ctx.Fail(ecode.CreateDatabaseErr)
//    //        return
//    //    }
//    //
//    //    defaultPwd := fmt.Sprintf("%v@Basic2023", user.Username)
//    //    err = model.NewMysql().CreateUser(user.Username, defaultPwd, user.Username)
//    //    if err != nil {
//    //        ctx.Fail(ecode.CreateDatabaseUserErr)
//    //        return
//    //    }
//    //}
//
//    ctx.OkWithDetailed(response.UserResponse{User: *userReturn})
//}
//
//// ChangePassword
////
////    @Tags        User
////    @Summary    用户修改密码
////    @Produce    application/json
////    @Param        object    body        request.ChangePasswordReq    true    "查询参数"
////    @Success    200        {object}    contextx.Response{}            "成功"
////    @Router        /api/user/changePassword [post]
//func (slf *BaseApi) ChangePassword(c *gin.Context) {
//    var params request.ChangePasswordReq
//    ctx, ok := contextx.NewContext(c, &params)
//    if !ok {
//        return
//    }
//
//    u := &model.User{UUID: utils.GetUserID(c), Password: params.Password}
//    _, errCode := userService.ChangePassword(u, params.NewPassword)
//    if errCode != ecode.OK {
//        ctx.Fail(errCode)
//        return
//    }
//    ctx.Ok()
//}
//
//// GetUserList
////
////    @Tags        User
////    @Summary    分页获取用户列表(不传分页参数,获取全部)
////    @Produce    application/json
////    @Param        object    body        request.GetUserList                            true    "查询参数"
////    @Success    200        {object}    contextx.Response{data=response.PageResult}    "成功"
////    @Router        /api/user/getUserList [post]
//func (slf *BaseApi) GetUserList(c *gin.Context) {
//    var params request.GetUserList
//    ctx, ok := contextx.NewContext(c, &params)
//    if !ok {
//        return
//    }
//
//    userInfo := utils.GetUserInfo(c)
//    if len(userInfo.UserId) <= 0 || len(userInfo.ParentId) == 0 {
//        ctx.Fail(ecode.UnknownErr)
//        return
//    }
//
//    ctx.OkWithDetailed(response.PageResult{
//        Page:     params.Page,
//        PageSize: params.PageSize,
//    })
//}
//
//// DeleteUser
////
////    @Tags        User
////    @Summary    删除用户
////    @Produce    application/json
////    @Param        object    body        request.DeleteUserReq    true    "查询参数"
////    @Success    200        {object}    contextx.Response{}        "成功"
////    @Router        /api/user/deleteUser [delete]
//func (slf *BaseApi) DeleteUser(c *gin.Context) {
//    var params request.DeleteUserReq
//    ctx, ok := contextx.NewContext(c, &params)
//    if !ok {
//        return
//    }
//
//    if len(params.UserId) <= 0 {
//        ctx.Fail(ecode.ParamsErr)
//        return
//    }
//
//    userInfo := utils.GetUserInfo(c)
//    if userInfo.UserType != constvar.UserTypePrimary || userInfo.UserId == params.UserId {
//        ctx.Fail(ecode.NoPowerErr)
//        return
//    }
//
//    err := userService.DeleteUser(params.UserId)
//    if err != nil {
//        logx.Errorf("删除失败! err:%v", err)
//        ctx.Fail(ecode.DBErr)
//        return
//    }
//    ctx.Ok()
//}
//
//// SetUserInfo
////
////    @Tags        User
////    @Summary    设置用户信息
////    @Produce    application/json
////    @Param        object    body        request.ChangeUserInfo    true    "查询参数"
////    @Success    200        {object}    contextx.Response{}        "成功"
////    @Router        /api/user/setUserInfo [post]
//func (slf *BaseApi) SetUserInfo(c *gin.Context) {
//    var params request.ChangeUserInfo
//    ctx, ok := contextx.NewContext(c, &params)
//    if !ok {
//        return
//    }
//
//    err := userService.SetUserInfo(model.User{
//        UUID:      params.ID,
//        NickName:  params.NickName,
//        HeaderImg: params.HeaderImg,
//        Phone:     params.Phone,
//        Email:     params.Email,
//        Pos:       params.Pos,
//    })
//    if err != nil {
//        logx.Errorf("设置失败! err:%v", err)
//        ctx.Fail(ecode.DBErr)
//        return
//    }
//    ctx.Ok()
//}
//
//// SetSelfInfo
////
////    @Tags        User
////    @Summary    设置用户信息
////    @Produce    application/json
////    @Param        object    body        request.ChangeUserInfo    true    "查询参数"
////    @Success    200        {object}    contextx.Response{}        "成功"
////    @Router        /api/user/setSelfInfo [post]
//func (slf *BaseApi) SetSelfInfo(c *gin.Context) {
//    var params request.ChangeUserInfo
//    ctx, ok := contextx.NewContext(c, &params)
//    if !ok {
//        return
//    }
//
//    params.ID = utils.GetUserID(c)
//    err := userService.SetUserInfo(model.User{
//        UUID:      params.ID,
//        NickName:  params.NickName,
//        HeaderImg: params.HeaderImg,
//        Phone:     params.Phone,
//        Email:     params.Email,
//        Pos:       params.Pos,
//    })
//    if err != nil {
//        logx.Errorf("设置失败! err:%v", err)
//        ctx.Fail(ecode.DBErr)
//        return
//    }
//    ctx.Ok()
//}
//
//// GetUserInfo
////
////    @Tags        User
////    @Summary    获取自身信息
////    @Produce    application/json
////    @Success    200    {object}    contextx.Response{}    "成功"
////    @Router        /api/user/getUserInfo [post]
//func (slf *BaseApi) GetUserInfo(c *gin.Context) {
//    ctx, ok := contextx.NewContext(c, nil)
//    if !ok {
//        return
//    }
//
//    id := utils.GetUserID(c)
//    ReqUser, err := userService.GetUserInfo(id)
//    if err != nil {
//        logx.Errorf("获取失败! err:%v", err)
//        ctx.Fail(ecode.DBErr)
//        return
//    }
//    ctx.OkWithDetailed(response.UserResponse{
//        User: *ReqUser,
//    })
//}
//
//// ResetPassword
////
////    @Tags        User
////    @Summary    重置用户密码
////    @Produce    application/json
////    @Param        object    body        model.User            true    "查询参数"
////    @Success    200        {object}    contextx.Response{}    "成功"
////    @Router        /api/user/resetPassword [post]
//func (slf *BaseApi) ResetPassword(c *gin.Context) {
//    var params model.User
//    ctx, ok := contextx.NewContext(c, &params)
//    if !ok {
//        return
//    }
//
//    err := userService.ResetPassword(params.UUID)
//    if err != nil {
//        logx.Errorf("重置失败! err:%v", err)
//        ctx.Fail(ecode.DBErr)
//        return
//    }
//    ctx.Ok()
//}
conf/aps-crm.json
@@ -49,7 +49,7 @@
  },
  "GrpcServiceAddr": {
    "Aps": "192.168.20.120:9091",
    "Admin": "192.168.20.119:50051"
    "Admin": "192.168.20.120:50051"
  }
}
constvar/const.go
@@ -204,3 +204,7 @@
    }
    return true
}
const (
    SystemType = 2
)
go.mod
@@ -35,7 +35,10 @@
    moul.io/zapgorm2 v1.3.0
)
require google.golang.org/genproto/googleapis/api v0.0.0-20230726155614-23370e0ffb3e // indirect
require (
    github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
    google.golang.org/genproto/googleapis/api v0.0.0-20230726155614-23370e0ffb3e // indirect
)
require (
    github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible // indirect
go.sum
@@ -46,6 +46,8 @@
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/bsm/ginkgo/v2 v2.7.0 h1:ItPMPH90RbmZJt5GtkcNvIRuGEdwlBItdNVoyzaNQao=
@@ -142,6 +144,7 @@
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
@@ -229,18 +232,27 @@
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/jackc/chunkreader/v2 v2.0.1/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
github.com/jackc/pgconn v1.13.0/go.mod h1:AnowpAqO4CMIIJNZl2VJp+KrkAZciAkhEl0W0JIobpI=
github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgproto3/v2 v2.3.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgtype v1.12.0/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4=
github.com/jackc/pgx/v4 v4.17.2/go.mod h1:lcxIZN44yMIrWI78a5CpucdD14hX0SBDbNRvjDBItsw=
github.com/jackc/pgx/v5 v5.3.1 h1:Fcr8QJ1ZeLi5zsPZqQeUZhNhxfkkKBOgJuYkJHoBOtU=
github.com/jackc/pgx/v5 v5.3.1/go.mod h1:t3JDKnCBlYIc0ewLF0Q7B8MXmoIaBOZj/ic7iHozM/8=
github.com/jcmturner/aescts/v2 v2.0.0/go.mod h1:AiaICIRyfYg35RUkr8yESTqvSy7csK90qZ5xfvvsoNs=
@@ -279,6 +291,7 @@
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8=
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
@@ -304,6 +317,7 @@
github.com/montanaflynn/stats v0.7.0/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
@@ -770,6 +784,7 @@
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce h1:+JknDZhAj8YMt7GC73Ei8pv4MzjDUNPHgQWJdtMAaDU=
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
main.go
@@ -4,10 +4,10 @@
    v1 "aps_crm/api/v1"
    "aps_crm/conf"
    "aps_crm/initialize"
    "aps_crm/middleware"
    "aps_crm/model"
    "aps_crm/pkg/logx"
    "aps_crm/router"
    "aps_crm/service"
    "fmt"
    "net/http"
    "os"
@@ -47,7 +47,7 @@
    }
    go v1.InitProductServiceConn()
    go service.InitUserConn()
    go middleware.InitUserConn()
    go v1.InitCodeServiceConn()
    //c := cron.New()
@@ -63,8 +63,8 @@
    <-quit
    v1.CloseProductServiceConn()
    service.CloseUserConn()
    v1.CloseCodeServiceConn()
    middleware.CloseUserConn()
    logx.Infof("aps-crm exited...")
    os.Exit(0)
middleware/interceptor.go
New file
@@ -0,0 +1,21 @@
package middleware
import (
    "context"
    "google.golang.org/grpc"
    "google.golang.org/grpc/metadata"
)
// HeaderInterceptor 拦截器函数
func HeaderInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
    token := ctx.Value("token")
    if token != nil {
        // 创建一个自定义的 metadata
        md := metadata.Pairs("token", token.(string))
        // 将 metadata 添加到 context 中
        ctx = metadata.NewOutgoingContext(ctx, md)
    }
    // 调用实际的 gRPC 方法
    err := invoker(ctx, method, req, reply, cc, opts...)
    return err
}
middleware/jwt.go
@@ -82,10 +82,12 @@
func JWTAuth2() gin.HandlerFunc {
    return func(c *gin.Context) {
        ctx := new(contextx.Context).SetCtx(c)
        // 我们这里jwt鉴权取头部信息 Authorization 登录时回返回token信息 这里前端需要把token存储到cookie或者本地localStorage中 不过需要跟后端协商过期时间 可以约定刷新令牌或者重新登录
        token := c.Request.Header.Get("Authorization")
        if token == "" {
            c.Next()
            ctx.Fail(ecode.JWTEmpty)
            c.Abort()
            return
        }
        slices := strings.Split(token, " ")
@@ -103,8 +105,20 @@
            c.Next()
            return
        }
        userInfo := service.GetUserBaseCache(claims.UserId)
        if userInfo == nil {
            SyncUserInfo()
            userInfo = service.GetUserBaseCache(claims.UserId)
        }
        claims.CrmUserId = userInfo.UserId
        claims.NickName = userInfo.NickName
        c.Set("claims", claims)
        c.Next()
        if CheckAuth(c.Request.RequestURI, token) {
            c.Next()
        } else {
            ctx.Fail(ecode.JWTDisabled)
            c.Abort()
            return
        }
    }
}
middleware/user.go
New file
@@ -0,0 +1,78 @@
package middleware
import (
    "aps_crm/conf"
    "aps_crm/constvar"
    "aps_crm/model"
    "aps_crm/pkg/logx"
    "aps_crm/proto/user"
    "context"
    "fmt"
    "google.golang.org/grpc"
    "google.golang.org/grpc/credentials/insecure"
    "time"
)
var (
    userConn *grpc.ClientConn
)
func InitUserConn() {
    var err error
    userConn, err = grpc.Dial(conf.Conf.GrpcServiceAddr.Admin, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithUnaryInterceptor(HeaderInterceptor))
    if err != nil {
        logx.Errorf("grpc dial user service error: %v", err.Error())
        return
    }
}
func CloseUserConn() {
    if userConn != nil {
        userConn.Close()
    }
}
func CheckAuth(apiPath string, token string) bool {
    cli := user.NewUserServiceClient(userConn)
    ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    defer cancel()
    ctx = context.WithValue(ctx, "token", token)
    rsp, err := cli.UserMenuCheck(ctx, &user.CheckRequest{
        ApiPath:    apiPath,
        SystemType: constvar.SystemType,
    })
    if err != nil {
        logx.Errorf("check auth err: %v", err)
        return false
    }
    return rsp.Result
}
func SyncUserInfo() {
    cli := user.NewUserServiceClient(userConn)
    var users []*user.User
    ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    defer cancel()
    r, err := cli.SyncUser(ctx, &user.UserRequest{Users: users})
    if err != nil {
        logx.Errorf("could not sync users: %v", err)
        return
    }
    fmt.Printf("Synced: %v, Message: %s", r.List, r.Message)
    for _, member := range r.List {
        err = model.NewUserSearch(nil).FirstOrCreate(model.User{
            UUID:     member.Uuid,
            Username: member.Username,
            UserType: constvar.UserType(member.Usertype),
            NickName: member.Nickname,
        })
        if err != nil {
            logx.Errorf("sync user error: %v", err.Error())
            continue
        }
    }
}
model/client.go
@@ -113,9 +113,7 @@
                    db = db.Joins("Member").Where("Member.username LIKE ?", "%"+v+"%")
                }
            case int:
            case int64:
            case float64:
            case int, uint, int64, float64:
                if key == "id" || key == "client_type_id" || key == "client_status_id" || key == "member_id" {
                    db = db.Where(key+" = ?", v)
                }
model/contact.go
@@ -93,9 +93,10 @@
                        db = db.Where("is_first = ?", false)
                    }
                }
            case int:
            case int64:
            case float64:
            case int, uint, int64, float64:
                if key == "id" || key == "client_type_id" || key == "client_status_id" || key == "member_id" {
                    db = db.Where(key+" = ?", v)
                }
                if key == "client_id" {
                    db = db.Where("client_id = ? and is_first = true", v)
                }
model/contract.go
@@ -72,6 +72,9 @@
                    db = db.Where(key+"= ?", v)
                }
            case int:
                if key == "member_id" {
                    db = db.Where(key+"= ?", v)
                }
            }
        }
    }
model/followRecord.go
@@ -98,10 +98,8 @@
                if key == "client_status" {
                    db = db.Joins("Client").Joins("Client.ClientStatus").Where("Client__ClientStatus.name LIKE ?", "%"+v+"%")
                }
            case int:
            case int64:
            case float64:
                if key == "client_id" || key == "contact_id" || key == "sales_leads_id" || key == "sale_chance_id" {
            case int, int64, float64:
                if key == "client_id" || key == "contact_id" || key == "sales_leads_id" || key == "sale_chance_id" || key == "member_id" {
                    db = db.Where(key+" = ?", v)
                }
            }
model/masterOrder.go
@@ -64,8 +64,7 @@
                if key == "member_name" {
                    db = db.Joins("Member").Where("Member.username = ?", v)
                }
            case int:
            case float64:
            case int, float64:
                if key == "client_id" || key == "member_id" || key == "money" {
                    db = db.Where(key+" = ?", v)
                }
model/quotation.go
@@ -80,7 +80,7 @@
                }
            case float64:
                if key == "client_id" || key == "sale_chance_id" {
                if key == "client_id" || key == "sale_chance_id" || key == "member_id" {
                    db = db.Where(key+" = ?", v)
                }
            }
model/request/jwt.go
@@ -10,6 +10,8 @@
    BaseClaims
    BufferTime int64
    jwt.StandardClaims
    CrmUserId int
    NickName  string
}
type BaseClaims struct {
model/saleChance.go
@@ -114,8 +114,7 @@
                    db = db.Where("expected_time LIKE ?", "%"+v+"%")
                }
            case int:
            case float64:
            case int, float64:
                if key == "member_id" || key == "budget" || key == "projected_amount" || key == "client_id" {
                    db = db.Where(key+" = ?", v)
                }
model/salesDetails.go
@@ -52,6 +52,7 @@
        PageNum     int
        PageSize    int
        Preload     bool
        MemberIds   []int
    }
)
@@ -92,6 +93,11 @@
        db = db.Joins("left join sales_details_product sdp on sdp.sales_details_id = sales_details.id left join products on sdp.product_id = products.id").Where("products.name like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
    }
    if len(slf.MemberIds) > 0 {
        db = db.Where("member_id in ?", slf.MemberIds)
    }
    if slf.Preload {
        db = db.Preload("Products").
            Preload("Member").
@@ -175,6 +181,11 @@
    return slf
}
func (slf *SalesDetailsSearch) SetMemberIds(memberIds []int) *SalesDetailsSearch {
    slf.MemberIds = memberIds
    return slf
}
func (slf *SalesDetailsSearch) SetPreload(preload bool) *SalesDetailsSearch {
    slf.Preload = preload
    return slf
model/salesRefund.go
@@ -43,6 +43,7 @@
        PageSize    int
        Preload     bool
        Ids         []int
        MemberIds   []int
    }
)
@@ -108,6 +109,10 @@
            db = db.Where("amount_total like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
        }
    }
    if len(slf.MemberIds) > 0 {
        db = db.Where("member_id in ?", slf.MemberIds)
    }
    return db
@@ -211,3 +216,9 @@
    var db = slf.build()
    return db.Updates(data).Error
}
func (slf *SalesRefundSearch) SetMemberIds(memberIds []int) *SalesRefundSearch {
    slf.MemberIds = memberIds
    return slf
}
model/salesReturn.go
@@ -46,6 +46,7 @@
        PageSize    int
        Preload     bool
        Ids         []int
        MemberIds   []int
    }
)
@@ -108,6 +109,10 @@
            db = db.Where("amount_has_refund = ?", slf.Keyword)
        }
    }
    if len(slf.MemberIds) > 0 {
        db = db.Where("member_id in ?", slf.MemberIds)
    }
    return db
@@ -216,3 +221,8 @@
    var db = slf.build()
    return db.Updates(data).Error
}
func (slf *SalesReturnSearch) SetMemberIds(memberIds []int) *SalesReturnSearch {
    slf.MemberIds = memberIds
    return slf
}
model/serviceContract.go
@@ -56,6 +56,7 @@
        PageNum     int
        PageSize    int
        Preload     bool
        MemberIds   []int
    }
)
@@ -109,6 +110,11 @@
        db = db.Where("amount_receivable = ?", slf.Keyword)
    }
    if len(slf.MemberIds) > 0 {
        db = db.Where("member_id in ?", slf.MemberIds)
    }
    if slf.Preload {
        db = db.
            Preload("Client").
@@ -212,6 +218,11 @@
    return slf
}
func (slf *ServiceContractSearch) SetMemberIds(memberIds []int) *ServiceContractSearch {
    slf.MemberIds = memberIds
    return slf
}
func (slf *ServiceContractSearch) SetSalesDetailsId(salesDetailsId int) *ServiceContractSearch {
    slf.SalesDetailsId = salesDetailsId
    return slf
model/serviceFollowup.go
@@ -46,6 +46,7 @@
        PageNum     int
        PageSize    int
        Preload     bool
        MemberIds   []int
    }
)
@@ -95,6 +96,10 @@
            Preload("SolveRate").
            Preload("IsVisit").
            Preload("OldMember")
    }
    if len(slf.MemberIds) > 0 {
        db = db.Where("member_id in ?", slf.MemberIds)
    }
    return db
@@ -176,6 +181,11 @@
    return slf
}
func (slf *ServiceFollowupSearch) SetMemberIds(memberIds []int) *ServiceFollowupSearch {
    slf.MemberIds = memberIds
    return slf
}
func (slf *ServiceFollowupSearch) UpdateByMap(data map[string]interface{}) error {
    var db = slf.build()
    return db.Updates(data).Error
model/serviceOrder.go
@@ -61,13 +61,14 @@
    // ServiceOrderSearch 服务单搜索条件
    ServiceOrderSearch struct {
        ServiceOrder
        Orm         *gorm.DB
        QueryClass  constvar.ServiceOrderQueryClass
        KeywordType constvar.ServiceOrderKeywordType
        Keyword     string
        PageNum     int
        PageSize    int
        Preload     bool
        Orm           *gorm.DB
        QueryClass    constvar.ServiceOrderQueryClass
        KeywordType   constvar.ServiceOrderKeywordType
        Keyword       string
        PageNum       int
        PageSize      int
        Preload       bool
        ServiceManIds []int
    }
)
@@ -124,6 +125,10 @@
        db = db.Joins("ServiceType", clause.LeftJoin).Where("ServiceType.name like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
    case constvar.ServiceOrderKeywordFaultType:
        db = db.Joins("FaultType", clause.LeftJoin).Where("FaultType.name like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
    }
    if len(slf.ServiceManIds) > 0 {
        db = db.Where("service_man_id in ?", slf.ServiceManIds)
    }
    return db
@@ -192,6 +197,11 @@
    return slf
}
func (slf *ServiceOrderSearch) SetMemberIds(memberIds []int) *ServiceOrderSearch {
    slf.ServiceManIds = memberIds
    return slf
}
func (slf *ServiceOrderSearch) SetPreload(preload bool) *ServiceOrderSearch {
    slf.Preload = preload
    return slf
model/subOrder.go
@@ -75,6 +75,9 @@
                if key == "client_id" || key == "master_order_id" {
                    db = db.Where(key+" = ?", v)
                }
                if key == "member_id" {
                    db = db.Where("member_id = ?", v)
                }
            }
        }
    }
model/user.go
@@ -89,7 +89,7 @@
    var db = slf.Orm.Model(&User{}).Preload("Menus")
    if slf.UUID != "" {
        db = db.Where("id = ?", slf.UUID)
        db = db.Where("uuid = ?", slf.UUID)
    }
    if slf.Username != "" {
proto/user.proto
@@ -6,6 +6,7 @@
service UserService {
  rpc SyncUser(UserRequest) returns (UserResponse);
  rpc UserMenuCheck(CheckRequest) returns (CheckResponse);
}
message User {
@@ -13,7 +14,6 @@
  string username = 2;
  int32 usertype = 3;
  string nickname = 4;
  // ... other fields
}
@@ -26,5 +26,15 @@
  string message = 2;
  repeated User List = 3;
  int64 total = 4;
}
message CheckRequest {
  string apiPath = 1; //接口路径
  int32 SystemType = 2; //系统名称
}
message CheckResponse {
  int32 code = 1;
  string message = 2;
  bool result = 3;
}
proto/user/user.pb.go
@@ -1,7 +1,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
//     protoc-gen-go v1.26.0
//     protoc        v4.24.0
//     protoc-gen-go v1.31.0
//     protoc        v3.19.0
// source: user.proto
package user
@@ -28,7 +28,7 @@
    Uuid     string `protobuf:"bytes,1,opt,name=uuid,proto3" json:"uuid,omitempty"`
    Username string `protobuf:"bytes,2,opt,name=username,proto3" json:"username,omitempty"`
    Usertype int32  `protobuf:"varint,3,opt,name=usertype,proto3" json:"usertype,omitempty"`
    Nickname string `protobuf:"bytes,4,opt,name=nickname,proto3" json:"nickname,omitempty"`
    Nickname string `protobuf:"bytes,4,opt,name=nickname,proto3" json:"nickname,omitempty"` // ... other fields
}
func (x *User) Reset() {
@@ -209,6 +209,124 @@
    return 0
}
type CheckRequest struct {
    state         protoimpl.MessageState
    sizeCache     protoimpl.SizeCache
    unknownFields protoimpl.UnknownFields
    ApiPath    string `protobuf:"bytes,1,opt,name=apiPath,proto3" json:"apiPath,omitempty"`        //接口路径
    SystemType int32  `protobuf:"varint,2,opt,name=SystemType,proto3" json:"SystemType,omitempty"` //系统名称
}
func (x *CheckRequest) Reset() {
    *x = CheckRequest{}
    if protoimpl.UnsafeEnabled {
        mi := &file_user_proto_msgTypes[3]
        ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
        ms.StoreMessageInfo(mi)
    }
}
func (x *CheckRequest) String() string {
    return protoimpl.X.MessageStringOf(x)
}
func (*CheckRequest) ProtoMessage() {}
func (x *CheckRequest) ProtoReflect() protoreflect.Message {
    mi := &file_user_proto_msgTypes[3]
    if protoimpl.UnsafeEnabled && x != nil {
        ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
        if ms.LoadMessageInfo() == nil {
            ms.StoreMessageInfo(mi)
        }
        return ms
    }
    return mi.MessageOf(x)
}
// Deprecated: Use CheckRequest.ProtoReflect.Descriptor instead.
func (*CheckRequest) Descriptor() ([]byte, []int) {
    return file_user_proto_rawDescGZIP(), []int{3}
}
func (x *CheckRequest) GetApiPath() string {
    if x != nil {
        return x.ApiPath
    }
    return ""
}
func (x *CheckRequest) GetSystemType() int32 {
    if x != nil {
        return x.SystemType
    }
    return 0
}
type CheckResponse struct {
    state         protoimpl.MessageState
    sizeCache     protoimpl.SizeCache
    unknownFields protoimpl.UnknownFields
    Code    int32  `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"`
    Message string `protobuf:"bytes,2,opt,name=message,proto3" json:"message,omitempty"`
    Result  bool   `protobuf:"varint,3,opt,name=result,proto3" json:"result,omitempty"`
}
func (x *CheckResponse) Reset() {
    *x = CheckResponse{}
    if protoimpl.UnsafeEnabled {
        mi := &file_user_proto_msgTypes[4]
        ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
        ms.StoreMessageInfo(mi)
    }
}
func (x *CheckResponse) String() string {
    return protoimpl.X.MessageStringOf(x)
}
func (*CheckResponse) ProtoMessage() {}
func (x *CheckResponse) ProtoReflect() protoreflect.Message {
    mi := &file_user_proto_msgTypes[4]
    if protoimpl.UnsafeEnabled && x != nil {
        ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
        if ms.LoadMessageInfo() == nil {
            ms.StoreMessageInfo(mi)
        }
        return ms
    }
    return mi.MessageOf(x)
}
// Deprecated: Use CheckResponse.ProtoReflect.Descriptor instead.
func (*CheckResponse) Descriptor() ([]byte, []int) {
    return file_user_proto_rawDescGZIP(), []int{4}
}
func (x *CheckResponse) GetCode() int32 {
    if x != nil {
        return x.Code
    }
    return 0
}
func (x *CheckResponse) GetMessage() string {
    if x != nil {
        return x.Message
    }
    return ""
}
func (x *CheckResponse) GetResult() bool {
    if x != nil {
        return x.Result
    }
    return false
}
var File_user_proto protoreflect.FileDescriptor
var file_user_proto_rawDesc = []byte{
@@ -230,12 +348,26 @@
    0x65, 0x12, 0x1e, 0x0a, 0x04, 0x4c, 0x69, 0x73, 0x74, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32,
    0x0a, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x52, 0x04, 0x4c, 0x69, 0x73,
    0x74, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x18, 0x04, 0x20, 0x01, 0x28, 0x03,
    0x52, 0x05, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x32, 0x40, 0x0a, 0x0b, 0x55, 0x73, 0x65, 0x72, 0x53,
    0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x31, 0x0a, 0x08, 0x53, 0x79, 0x6e, 0x63, 0x55, 0x73,
    0x65, 0x72, 0x12, 0x11, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65,
    0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x2e, 0x55, 0x73, 0x65,
    0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x08, 0x5a, 0x06, 0x2e, 0x2f, 0x75,
    0x73, 0x65, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
    0x52, 0x05, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x22, 0x48, 0x0a, 0x0c, 0x43, 0x68, 0x65, 0x63, 0x6b,
    0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x61, 0x70, 0x69, 0x50, 0x61,
    0x74, 0x68, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x61, 0x70, 0x69, 0x50, 0x61, 0x74,
    0x68, 0x12, 0x1e, 0x0a, 0x0a, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x54, 0x79, 0x70, 0x65, 0x18,
    0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0a, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x54, 0x79, 0x70,
    0x65, 0x22, 0x55, 0x0a, 0x0d, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e,
    0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05,
    0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67,
    0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
    0x12, 0x16, 0x0a, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08,
    0x52, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x32, 0x7a, 0x0a, 0x0b, 0x55, 0x73, 0x65, 0x72,
    0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x31, 0x0a, 0x08, 0x53, 0x79, 0x6e, 0x63, 0x55,
    0x73, 0x65, 0x72, 0x12, 0x11, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x52,
    0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x2e, 0x55, 0x73,
    0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x38, 0x0a, 0x0d, 0x55, 0x73,
    0x65, 0x72, 0x4d, 0x65, 0x6e, 0x75, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x12, 0x12, 0x2e, 0x75, 0x73,
    0x65, 0x72, 0x2e, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a,
    0x13, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x2e, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x52, 0x65, 0x73, 0x70,
    0x6f, 0x6e, 0x73, 0x65, 0x42, 0x08, 0x5a, 0x06, 0x2e, 0x2f, 0x75, 0x73, 0x65, 0x72, 0x62, 0x06,
    0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
@@ -250,19 +382,23 @@
    return file_user_proto_rawDescData
}
var file_user_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
var file_user_proto_msgTypes = make([]protoimpl.MessageInfo, 5)
var file_user_proto_goTypes = []interface{}{
    (*User)(nil),         // 0: user.User
    (*UserRequest)(nil),  // 1: user.UserRequest
    (*UserResponse)(nil), // 2: user.UserResponse
    (*User)(nil),          // 0: user.User
    (*UserRequest)(nil),   // 1: user.UserRequest
    (*UserResponse)(nil),  // 2: user.UserResponse
    (*CheckRequest)(nil),  // 3: user.CheckRequest
    (*CheckResponse)(nil), // 4: user.CheckResponse
}
var file_user_proto_depIdxs = []int32{
    0, // 0: user.UserRequest.users:type_name -> user.User
    0, // 1: user.UserResponse.List:type_name -> user.User
    1, // 2: user.UserService.SyncUser:input_type -> user.UserRequest
    2, // 3: user.UserService.SyncUser:output_type -> user.UserResponse
    3, // [3:4] is the sub-list for method output_type
    2, // [2:3] is the sub-list for method input_type
    3, // 3: user.UserService.UserMenuCheck:input_type -> user.CheckRequest
    2, // 4: user.UserService.SyncUser:output_type -> user.UserResponse
    4, // 5: user.UserService.UserMenuCheck:output_type -> user.CheckResponse
    4, // [4:6] is the sub-list for method output_type
    2, // [2:4] is the sub-list for method input_type
    2, // [2:2] is the sub-list for extension type_name
    2, // [2:2] is the sub-list for extension extendee
    0, // [0:2] is the sub-list for field type_name
@@ -310,6 +446,30 @@
                return nil
            }
        }
        file_user_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
            switch v := v.(*CheckRequest); i {
            case 0:
                return &v.state
            case 1:
                return &v.sizeCache
            case 2:
                return &v.unknownFields
            default:
                return nil
            }
        }
        file_user_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} {
            switch v := v.(*CheckResponse); i {
            case 0:
                return &v.state
            case 1:
                return &v.sizeCache
            case 2:
                return &v.unknownFields
            default:
                return nil
            }
        }
    }
    type x struct{}
    out := protoimpl.TypeBuilder{
@@ -317,7 +477,7 @@
            GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
            RawDescriptor: file_user_proto_rawDesc,
            NumEnums:      0,
            NumMessages:   3,
            NumMessages:   5,
            NumExtensions: 0,
            NumServices:   1,
        },
proto/user/user_grpc.pb.go
@@ -1,4 +1,8 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.3.0
// - protoc             v3.19.0
// source: user.proto
package user
@@ -14,11 +18,17 @@
// Requires gRPC-Go v1.32.0 or later.
const _ = grpc.SupportPackageIsVersion7
const (
    UserService_SyncUser_FullMethodName      = "/user.UserService/SyncUser"
    UserService_UserMenuCheck_FullMethodName = "/user.UserService/UserMenuCheck"
)
// UserServiceClient is the client API for UserService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type UserServiceClient interface {
    SyncUser(ctx context.Context, in *UserRequest, opts ...grpc.CallOption) (*UserResponse, error)
    UserMenuCheck(ctx context.Context, in *CheckRequest, opts ...grpc.CallOption) (*CheckResponse, error)
}
type userServiceClient struct {
@@ -31,7 +41,16 @@
func (c *userServiceClient) SyncUser(ctx context.Context, in *UserRequest, opts ...grpc.CallOption) (*UserResponse, error) {
    out := new(UserResponse)
    err := c.cc.Invoke(ctx, "/user.UserService/SyncUser", in, out, opts...)
    err := c.cc.Invoke(ctx, UserService_SyncUser_FullMethodName, in, out, opts...)
    if err != nil {
        return nil, err
    }
    return out, nil
}
func (c *userServiceClient) UserMenuCheck(ctx context.Context, in *CheckRequest, opts ...grpc.CallOption) (*CheckResponse, error) {
    out := new(CheckResponse)
    err := c.cc.Invoke(ctx, UserService_UserMenuCheck_FullMethodName, in, out, opts...)
    if err != nil {
        return nil, err
    }
@@ -43,6 +62,7 @@
// for forward compatibility
type UserServiceServer interface {
    SyncUser(context.Context, *UserRequest) (*UserResponse, error)
    UserMenuCheck(context.Context, *CheckRequest) (*CheckResponse, error)
    mustEmbedUnimplementedUserServiceServer()
}
@@ -52,6 +72,9 @@
func (UnimplementedUserServiceServer) SyncUser(context.Context, *UserRequest) (*UserResponse, error) {
    return nil, status.Errorf(codes.Unimplemented, "method SyncUser not implemented")
}
func (UnimplementedUserServiceServer) UserMenuCheck(context.Context, *CheckRequest) (*CheckResponse, error) {
    return nil, status.Errorf(codes.Unimplemented, "method UserMenuCheck not implemented")
}
func (UnimplementedUserServiceServer) mustEmbedUnimplementedUserServiceServer() {}
@@ -76,10 +99,28 @@
    }
    info := &grpc.UnaryServerInfo{
        Server:     srv,
        FullMethod: "/user.UserService/SyncUser",
        FullMethod: UserService_SyncUser_FullMethodName,
    }
    handler := func(ctx context.Context, req interface{}) (interface{}, error) {
        return srv.(UserServiceServer).SyncUser(ctx, req.(*UserRequest))
    }
    return interceptor(ctx, in, info, handler)
}
func _UserService_UserMenuCheck_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
    in := new(CheckRequest)
    if err := dec(in); err != nil {
        return nil, err
    }
    if interceptor == nil {
        return srv.(UserServiceServer).UserMenuCheck(ctx, in)
    }
    info := &grpc.UnaryServerInfo{
        Server:     srv,
        FullMethod: UserService_UserMenuCheck_FullMethodName,
    }
    handler := func(ctx context.Context, req interface{}) (interface{}, error) {
        return srv.(UserServiceServer).UserMenuCheck(ctx, req.(*CheckRequest))
    }
    return interceptor(ctx, in, info, handler)
}
@@ -95,6 +136,10 @@
            MethodName: "SyncUser",
            Handler:    _UserService_SyncUser_Handler,
        },
        {
            MethodName: "UserMenuCheck",
            Handler:    _UserService_UserMenuCheck_Handler,
        },
    },
    Streams:  []grpc.StreamDesc{},
    Metadata: "user.proto",
router/base.go
@@ -1,18 +1,18 @@
package router
import (
    "aps_crm/api/v1"
    "github.com/gin-gonic/gin"
)
type BaseRouter struct{}
func (s *BaseRouter) InitBaseRouter(Router *gin.RouterGroup) (R gin.IRoutes) {
    baseRouter := Router.Group("base")
    baseApi := v1.ApiGroup.BaseApi
    {
        baseRouter.POST("login", baseApi.Login)     // 用户登录
        baseRouter.POST("captcha", baseApi.Captcha) // 获取验证码
    }
    return baseRouter
}
//import (
//    "aps_crm/api/v1"
//    "github.com/gin-gonic/gin"
//)
//
//type BaseRouter struct{}
//
//func (s *BaseRouter) InitBaseRouter(Router *gin.RouterGroup) (R gin.IRoutes) {
//    baseRouter := Router.Group("base")
//    baseApi := v1.ApiGroup.BaseApi
//    {
//        baseRouter.POST("login", baseApi.Login)     // 用户登录
//        baseRouter.POST("captcha", baseApi.Captcha) // 获取验证码
//    }
//    return baseRouter
//}
router/image.go
@@ -1,17 +1,18 @@
package router
import (
    "aps_crm/api/v1"
    "github.com/gin-gonic/gin"
)
type ImageRouter struct{}
func (s *BaseRouter) InitImageRouter(Router *gin.RouterGroup) (R gin.IRoutes) {
    imageRouter := Router.Group("image")
    imageApi := v1.ApiGroup.ImageApi
    {
        imageRouter.POST("upload", imageApi.Upload) // 上传图像
    }
    return imageRouter
}
//
//import (
//    "aps_crm/api/v1"
//    "github.com/gin-gonic/gin"
//)
//
//type ImageRouter struct{}
//
//func (s *BaseRouter) InitImageRouter(Router *gin.RouterGroup) (R gin.IRoutes) {
//    imageRouter := Router.Group("image")
//    imageApi := v1.ApiGroup.ImageApi
//    {
//        imageRouter.POST("upload", imageApi.Upload) // 上传图像
//    }
//    return imageRouter
//}
router/index.go
@@ -5,6 +5,7 @@
    "aps_crm/conf"
    _ "aps_crm/docs"
    "aps_crm/middleware"
    "fmt"
    "github.com/gin-contrib/cors"
    "github.com/gin-gonic/gin"
    swaggerFiles "github.com/swaggo/files"
@@ -27,8 +28,8 @@
    IsVisitRouter
    SolveRateRouter
    TimelyRateRouter
    BaseRouter
    UserRouter
    //BaseRouter
    //UserRouter
    JwtRouter
    CountryRouter
    ProvinceRouter
@@ -87,6 +88,14 @@
    Router.Use(cors.Default())
    Router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
    //获取所有路由 //todo
    Router.GET("getRouters", func(c *gin.Context) {
        routers := Router.Routes()
        for _, v := range routers {
            fmt.Printf("\"%v\" : 1,\n", v.Path)
        }
    })
    routerGroup := new(Group)
    PublicGroup := Router.Group("api")
@@ -96,18 +105,18 @@
            c.JSON(http.StatusOK, "ok")
        })
    }
    {
        routerGroup.InitBaseRouter(PublicGroup)  // 注册基础功能路由 不做鉴权
        routerGroup.InitImageRouter(PublicGroup) // 图像功能路由
    }
    //{
    //    routerGroup.InitBaseRouter(PublicGroup)  // 注册基础功能路由 不做鉴权
    //    routerGroup.InitImageRouter(PublicGroup) // 图像功能路由
    //}
    PrivateGroup := Router.Group("api")
    //PrivateGroup.Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
    PrivateGroup.Use(middleware.JWTAuth2())
    //PrivateGroup.Use(middleware.CasbinHandler())
    {
        routerGroup.InitJwtRouter(PrivateGroup)                  // jwt相关路由
        routerGroup.InitUserRouter(PrivateGroup)                 // 注册用户路由
        routerGroup.InitJwtRouter(PrivateGroup) // jwt相关路由
        //routerGroup.InitUserRouter(PrivateGroup)                 // 注册用户路由
        routerGroup.InitCountryRouter(PrivateGroup)              // 注册country路由
        routerGroup.InitProvinceRouter(PrivateGroup)             // 注册province路由
        routerGroup.InitCityRouter(PrivateGroup)                 // 注册city路由
router/user.go
@@ -1,26 +1,26 @@
package router
import (
    "aps_crm/api/v1"
    "github.com/gin-gonic/gin"
)
type UserRouter struct{}
func (s *UserRouter) InitUserRouter(Router *gin.RouterGroup) {
    userRouter := Router.Group("user")
    userRouterWithoutRecord := Router.Group("user")
    baseApi := v1.ApiGroup.BaseApi
    {
        userRouter.POST("register", baseApi.Register) // 注册账号
        //userRouter.POST("changePassword", baseApi.ChangePassword) // 用户修改密码
        //userRouter.POST("resetPassword", baseApi.ResetPassword) // 重置用户密码
        userRouter.DELETE("deleteUser", baseApi.DeleteUser) // 删除用户
        userRouter.PUT("setUserInfo", baseApi.SetUserInfo)  // 设置用户信息
        //userRouter.PUT("setSelfInfo", baseApi.SetSelfInfo)  // 设置自身信息
    }
    {
        userRouterWithoutRecord.POST("getUserList", baseApi.GetUserList) // 分页获取用户列表(不传分页参数,获取全部)
        userRouterWithoutRecord.GET("getUserInfo", baseApi.GetUserInfo)  // 获取自身信息
    }
}
//import (
//    "aps_crm/api/v1"
//    "github.com/gin-gonic/gin"
//)
//
//type UserRouter struct{}
//
//func (s *UserRouter) InitUserRouter(Router *gin.RouterGroup) {
//    userRouter := Router.Group("user")
//    userRouterWithoutRecord := Router.Group("user")
//    baseApi := v1.ApiGroup.BaseApi
//    {
//        userRouter.POST("register", baseApi.Register) // 注册账号
//        //userRouter.POST("changePassword", baseApi.ChangePassword) // 用户修改密码
//        //userRouter.POST("resetPassword", baseApi.ResetPassword) // 重置用户密码
//        userRouter.DELETE("deleteUser", baseApi.DeleteUser) // 删除用户
//        userRouter.PUT("setUserInfo", baseApi.SetUserInfo)  // 设置用户信息
//        //userRouter.PUT("setSelfInfo", baseApi.SetSelfInfo)  // 设置自身信息
//    }
//    {
//        userRouterWithoutRecord.POST("getUserList", baseApi.GetUserList) // 分页获取用户列表(不传分页参数,获取全部)
//        userRouterWithoutRecord.GET("getUserInfo", baseApi.GetUserInfo)  // 获取自身信息
//    }
//}
service/lru.go
New file
@@ -0,0 +1,40 @@
package service
import (
    "github.com/hashicorp/golang-lru/v2/expirable"
    "time"
)
type userBaseInfo struct {
    UserId   int
    NickName string
}
var userCache *expirable.LRU[string, *userBaseInfo]
func init() {
    //make cache with 5 minutes TTL and 100 max keys
    userCache = expirable.NewLRU[string, *userBaseInfo](100, nil, time.Minute*5)
}
func GetUserBaseCache(adminUserId string) *userBaseInfo {
    userCache, ok := userCache.Get(adminUserId)
    if !ok {
        userService := UserService{}
        userRecord, err := userService.GetUserInfo(adminUserId)
        if err != nil {
            return nil
        }
        baseInfo := &userBaseInfo{
            UserId:   userRecord.ID,
            NickName: userRecord.NickName,
        }
        SetUserBaseCache(adminUserId, baseInfo)
        return baseInfo
    }
    return userCache
}
func SetUserBaseCache(adminUserId string, user *userBaseInfo) {
    _ = userCache.Add(adminUserId, user)
}
service/salesDetails.go
@@ -114,10 +114,11 @@
    return ecode.OK
}
func (SalesDetailsService) GetSalesDetailsList(params request.GetSalesDetailsList) ([]*model.SalesDetails, int64, int) {
func (SalesDetailsService) GetSalesDetailsList(params request.GetSalesDetailsList, memberIds []int) ([]*model.SalesDetails, int64, int) {
    // get contact list
    contacts, total, err := model.NewSalesDetailsSearch().
        SetPreload(true).
        SetMemberIds(memberIds).
        SetKeywordType(params.KeywordType).
        SetSaleChanceId(params.SaleChanceId).
        SetClientId(params.ClientId).
service/salesRefund.go
@@ -168,13 +168,14 @@
    return ecode.OK
}
func (SalesRefundService) GetSalesRefundList(page, pageSize int, keywordType constvar.SalesRefundKeywordType, keyword string, sourceId int) ([]*model.SalesRefund, int64, int) {
func (SalesRefundService) GetSalesRefundList(page, pageSize int, keywordType constvar.SalesRefundKeywordType, keyword string, sourceId int, memberIds []int)  ([]*model.SalesRefund, int64, int) {
    // get contact list
    contacts, total, err := model.NewSalesRefundSearch().
        SetKeywordType(keywordType).
        SetKeyword(keyword).
        SetSourceId(sourceId).
        SetPreload(true).
        SetMemberIds(memberIds).
        SetPage(page, pageSize).FindAll()
    if err != nil {
        return nil, 0, ecode.SalesRefundListErr
service/salesReturn.go
@@ -124,7 +124,7 @@
    return ecode.OK
}
func (SalesReturnService) GetSalesReturnList(params request.GetSalesReturnList) ([]*model.SalesReturn, int64, int) {
func (SalesReturnService) GetSalesReturnList(params request.GetSalesReturnList, memberIds []int) ([]*model.SalesReturn, int64, int) {
    // get contact list
    contacts, total, err := model.NewSalesReturnSearch().
        SetKeywordType(params.KeywordType).
@@ -134,6 +134,7 @@
        SetSourceType(params.SourceType).
        SetClientId(params.ClientId).
        SetPreload(true).
        SetMemberIds(memberIds).
        FindAll()
    if err != nil {
        return nil, 0, ecode.SalesReturnListErr
service/serviceContract.go
@@ -117,7 +117,7 @@
    return ecode.OK
}
func (SContractService) GetServiceContractList(params request.GetServiceContractList) ([]*model.ServiceContract, int64, int) {
func (SContractService) GetServiceContractList(params request.GetServiceContractList, memberIds []int) ([]*model.ServiceContract, int64, int) {
    // get contact list
    contacts, total, err := model.NewServiceContractSearch().
        SetKeyword(params.Keyword).
@@ -128,6 +128,7 @@
        SetQuotationId(params.QuotationId).
        SetSaleChanceId(params.SaleChanceId).
        SetContactId(params.ContactId).
        SetMemberIds(memberIds).
        SetPreload(true).
        Find()
    if err != nil {
service/serviceFollowup.go
@@ -41,7 +41,7 @@
    return ecode.OK
}
func (FollowupService) GetServiceFollowupList(page, pageSize int, keywordType constvar.ServiceFollowupKeywordType, keyword string, serviceOrderId int) ([]*model.ServiceFollowup, int64, int) {
func (FollowupService) GetServiceFollowupList(page, pageSize int, keywordType constvar.ServiceFollowupKeywordType, keyword string, serviceOrderId int, memberIds []int) ([]*model.ServiceFollowup, int64, int) {
    // get contact list
    contacts, total, err := model.NewServiceFollowupSearch().
        SetKeywordType(keywordType).
@@ -49,6 +49,7 @@
        SetPage(page, pageSize).
        SetPreload(true).
        SetServiceOrderId(serviceOrderId).
        SetMemberIds(memberIds).
        FindAll()
    if err != nil {
        return nil, 0, ecode.ServiceFollowupListErr
service/serviceOrder.go
@@ -38,7 +38,7 @@
    return ecode.OK
}
func (ServiceOrderService) GetServiceOrderList(page, pageSize int, queryClass constvar.ServiceOrderQueryClass, keywordType constvar.ServiceOrderKeywordType, keyword string, serviceContractId, salesDetailsId int) ([]*model.ServiceOrder, int64, int) {
func (ServiceOrderService) GetServiceOrderList(page, pageSize int, queryClass constvar.ServiceOrderQueryClass, keywordType constvar.ServiceOrderKeywordType, keyword string, serviceContractId, salesDetailsId int, memberIds []int) ([]*model.ServiceOrder, int64, int) {
    list, total, err := model.NewServiceOrderSearch().
        SetPage(page, pageSize).
        SetKeyword(keyword).
@@ -47,6 +47,7 @@
        SetPreload(true).
        SetServiceContractId(serviceContractId).
        SetSalesDetailsId(salesDetailsId).
        SetMemberIds(memberIds).
        Find()
    if err != nil {
        return nil, 0, ecode.DBErr
service/user.go
@@ -1,20 +1,12 @@
package service
import (
    "aps_crm/conf"
    "aps_crm/constvar"
    "aps_crm/model"
    "aps_crm/pkg/ecode"
    "aps_crm/pkg/encrypt"
    "aps_crm/pkg/logx"
    "aps_crm/proto/user"
    "context"
    "errors"
    "fmt"
    "google.golang.org/grpc"
    "google.golang.org/grpc/credentials/insecure"
    "gorm.io/gorm"
    "time"
)
type UserService struct{}
@@ -100,51 +92,4 @@
func (userService *UserService) GetUserList() (userList []*model.User, err error) {
    return model.NewUserSearch(nil).FindAll()
}
var (
    userConn *grpc.ClientConn
)
func InitUserConn() {
    var err error
    userConn, err = grpc.Dial(conf.Conf.GrpcServiceAddr.Admin, grpc.WithTransportCredentials(insecure.NewCredentials()))
    if err != nil {
        logx.Errorf("grpc dial user service error: %v", err.Error())
        return
    }
}
func CloseUserConn() {
    if userConn != nil {
        userConn.Close()
    }
}
func SyncUserInfo() {
    cli := user.NewUserServiceClient(userConn)
    var users []*user.User
    ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    defer cancel()
    r, err := cli.SyncUser(ctx, &user.UserRequest{Users: users})
    if err != nil {
        logx.Fatalf("could not sync users: %v", err)
    }
    fmt.Printf("Synced: %v, Message: %s", r.List, r.Message)
    for _, member := range r.List {
        err = model.NewUserSearch(nil).FirstOrCreate(model.User{
            UUID:     member.Uuid,
            Username: member.Username,
            UserType: constvar.UserType(member.Usertype),
            NickName: member.Nickname,
        })
        if err != nil {
            logx.Errorf("sync user error: %v", err.Error())
            continue
        }
    }
}
utils/clamis.go
@@ -22,30 +22,18 @@
    return claims, err
}
// GetUserID 从Gin的Context中获取从jwt解析出来的用户ID
func GetUserID(c *gin.Context) string {
    if claims, exists := c.Get("claims"); !exists {
        if cl, err := GetClaims(c); err != nil {
            return ""
        } else {
            return cl.UserId
        }
    } else {
func GetUserID(c *gin.Context) int {
    if claims, exists := c.Get("claims"); exists {
        waitUse := claims.(*request.CustomClaims)
        return waitUse.UserId
        return waitUse.CrmUserId
    }
    return 0
}
// GetUserInfo 从Gin的Context中获取从jwt解析出来的用户信息
func GetUserInfo(c *gin.Context) *request.CustomClaims {
    if claims, exists := c.Get("claims"); !exists {
        if cl, err := GetClaims(c); err != nil {
            return nil
        } else {
            return cl
        }
    } else {
    if claims, exists := c.Get("claims"); exists {
        waitUse := claims.(*request.CustomClaims)
        return waitUse
    }
    return nil
}