Browse Source

Merge branch 'feature/dashboard'

* feature/dashboard:
  看板增加账号纬度展示 单位改为由接口返回 优化账号统计与展示
  增加微信后选项接口
boweniac 4 tháng trước cách đây
mục cha
commit
f7e8f8fd64

+ 4 - 1
crontask/compute_statistic.go

@@ -2,6 +2,7 @@ package crontask
 
 import (
 	"strconv"
+	"strings"
 	"time"
 	"wechat-api/ent"
 	"wechat-api/ent/contact"
@@ -26,7 +27,9 @@ func (l *CronTask) computeStatistic() {
 
 	wxbotsSet := make(map[uint64][]*ent.Wx)
 	for _, bot := range wxbots {
-		wxbotsSet[bot.OrganizationID] = append(wxbotsSet[bot.OrganizationID], bot)
+		if !strings.HasPrefix(bot.Wxid, "temp-") {
+			wxbotsSet[bot.OrganizationID] = append(wxbotsSet[bot.OrganizationID], bot)
+		}
 	}
 	LabelsCountSet := make(map[uint64][]custom_types.LabelDist)
 

+ 4 - 0
desc/wechat/dashboard.api

@@ -12,6 +12,8 @@ type (
 
         // 租户id
         OrganizationId  *uint64 `json:"organizationId,optional"`
+
+        Wxid  *string `json:"wxid,optional"`
     }
     ChartsResp {
         BaseDataInfo
@@ -36,6 +38,7 @@ type (
         Rate float32 `json:"rate"`
         Label []string `json:"label"`
         Val []uint64 `json:"val"`
+        LabelText string `json:"labelText"`
     }
 
     ChartsInt {
@@ -43,6 +46,7 @@ type (
         Rate float32 `json:"rate"`
         Label []string `json:"label"`
         Val []int64 `json:"val"`
+        LabelText string `json:"labelText"`
     }
 
     LabelsData {

+ 28 - 0
desc/wechat/wx.api

@@ -129,6 +129,30 @@ type (
         Callback  *string `json:"callback,optional"`
     }
 
+    // Get wx list request params | Wx列表请求参数
+    WxSelectListReq {
+        // 租户id
+        OrganizationId  *uint64 `json:"organizationId,optional"`
+    }
+
+    // The response data of wx list | Wx列表数据
+    WxSelectListResp {
+        BaseDataInfo
+
+        // Wx list data | Wx列表数据
+        Data []WxSelectListInfo `json:"data"`
+    }
+
+    WxSelectListInfo {
+        BaseIDInfo
+
+        // 微信id
+        Wxid  *string `json:"wxid,optional"`
+
+        // 微信昵称
+        Nickname  *string `json:"nickname,optional"`
+    }
+
     // Wx information response | Wx信息返回体
     WxInfoResp {
         BaseDataInfo
@@ -201,6 +225,10 @@ service Wechat {
     @handler getWxList
     post /wx/list (WxListReq) returns (WxListResp)
 
+    // Get wx list | 获取Wx列表
+    @handler getSelectWxList
+    post /wx/selectList (WxSelectListReq) returns (WxSelectListResp)
+
     // Get wx allow and block list | 获取黑白名单列表
     @handler getWxAllowBlockList
     post /wx/getWxAllowBlockList (IDReq) returns (AllowBlockListResp)

+ 44 - 0
internal/handler/Wx/get_select_wx_list_handler.go

@@ -0,0 +1,44 @@
+package Wx
+
+import (
+	"net/http"
+
+	"github.com/zeromicro/go-zero/rest/httpx"
+
+	"wechat-api/internal/logic/Wx"
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+)
+
+// swagger:route post /wx/selectList Wx GetSelectWxList
+//
+// Get wx list | 获取Wx列表
+//
+// Get wx list | 获取Wx列表
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: WxSelectListReq
+//
+// Responses:
+//  200: WxSelectListResp
+
+func GetSelectWxListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.WxSelectListReq
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := Wx.NewGetSelectWxListLogic(r.Context(), svcCtx)
+		resp, err := l.GetSelectWxList(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 5 - 0
internal/handler/routes.go

@@ -127,6 +127,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 				},
 				{
 					Method:  http.MethodPost,
+					Path:    "/wx/selectList",
+					Handler: Wx.GetSelectWxListHandler(serverCtx),
+				},
+				{
+					Method:  http.MethodPost,
 					Path:    "/wx/getWxAllowBlockList",
 					Handler: Wx.GetWxAllowBlockListHandler(serverCtx),
 				},

+ 65 - 0
internal/logic/Wx/get_select_wx_list_logic.go

@@ -0,0 +1,65 @@
+package Wx
+
+import (
+	"context"
+	"github.com/suyuan32/simple-admin-common/msg/errormsg"
+	"strings"
+	"wechat-api/ent"
+	"wechat-api/ent/predicate"
+	"wechat-api/ent/wx"
+	"wechat-api/internal/utils/dberrorhandler"
+
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type GetSelectWxListLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewGetSelectWxListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSelectWxListLogic {
+	return &GetSelectWxListLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *GetSelectWxListLogic) GetSelectWxList(req *types.WxSelectListReq) (resp *types.WxSelectListResp, err error) {
+	organizationId := l.ctx.Value("organizationId").(uint64)
+	isAdmin := l.ctx.Value("isAdmin").(bool)
+	var predicates []predicate.Wx
+	if !isAdmin {
+		predicates = append(predicates, wx.OrganizationIDEQ(organizationId))
+	} else {
+		if req.OrganizationId != nil && *req.OrganizationId != 0 {
+			predicates = append(predicates, wx.OrganizationIDEQ(*req.OrganizationId))
+		}
+	}
+	data, err := l.svcCtx.DB.Wx.Query().Where(predicates...).Order(ent.Desc(wx.FieldOrganizationID)).All(l.ctx)
+	if err != nil {
+		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+	}
+
+	resp = &types.WxSelectListResp{}
+	resp.Msg = errormsg.Success
+
+	for _, v := range data {
+		wxid := v.Wxid
+		if !strings.HasPrefix(wxid, "temp-") {
+			resp.Data = append(resp.Data,
+				types.WxSelectListInfo{
+					BaseIDInfo: types.BaseIDInfo{
+						Id: &v.ID,
+					},
+					Wxid:     &wxid,
+					Nickname: &v.Nickname,
+				})
+		}
+	}
+
+	return resp, nil
+}

+ 1 - 1
internal/logic/Wxhook/logout_logic.go

@@ -48,7 +48,7 @@ func (l *LogoutLogic) Logout(req *types.IDReq) (resp *types.BaseMsgResp, err err
 	tmpWxid := rand.New(rand.NewSource(time.Now().UnixNano())).Intn(1000000)
 	l.svcCtx.DB.Wx.UpdateOne(wxInfo).
 		SetStatus(0).
-		SetWxid(strconv.Itoa(tmpWxid)).
+		SetWxid("temp-" + strconv.Itoa(tmpWxid)).
 		Save(l.ctx)
 
 	resp = &types.BaseMsgResp{

+ 37 - 3
internal/logic/dashboard/get_charts_logic.go

@@ -101,6 +101,7 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 	totalFriend := types.ChartsUint{}
 	totalGroup := types.ChartsUint{}
 	accountBalance := types.ChartsUint{}
+	accountBalance.LabelText = "积分"
 	consumeToken := types.ChartsUint{}
 	activeUser := types.ChartsUint{}
 	newUser := types.ChartsInt{}
@@ -123,7 +124,11 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 
 		var predicates []predicate.UsageStatisticHour
 		predicates = append(predicates, usagestatistichour.OrganizationID(organizationId))
-		predicates = append(predicates, usagestatistichour.BotID(""))
+		if req.Wxid != nil {
+			predicates = append(predicates, usagestatistichour.BotID(*req.Wxid))
+		} else {
+			predicates = append(predicates, usagestatistichour.BotID(""))
+		}
 		predicates = append(predicates, usagestatistichour.AddtimeGTE(startAddTime))
 		predicates = append(predicates, usagestatistichour.AddtimeLTE(nextDayAddTime))
 		usageStatisticHour, err := l.svcCtx.DB.UsageStatisticHour.Query().Where(predicates...).All(l.ctx)
@@ -135,30 +140,37 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 			aiResponse.Count += hourData.AiResponse
 			aiResponse.Val = append(aiResponse.Val, hourData.AiResponse)
 			aiResponse.Label = append(aiResponse.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits))
+			aiResponse.LabelText = "次"
 
 			sopRun.Count += hourData.SopRun
 			sopRun.Val = append(sopRun.Val, hourData.SopRun)
 			sopRun.Label = append(sopRun.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits))
+			sopRun.LabelText = "次"
 
 			totalFriend.Count = hourData.TotalFriend
 			totalFriend.Val = append(totalFriend.Val, hourData.TotalFriend)
 			totalFriend.Label = append(totalFriend.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits))
+			totalFriend.LabelText = "个"
 
 			totalGroup.Count = hourData.TotalGroup
 			totalGroup.Val = append(totalGroup.Val, hourData.TotalGroup)
 			totalGroup.Label = append(totalGroup.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits))
+			totalGroup.LabelText = "个"
 
 			consumeToken.Count += hourData.ConsumeToken
 			consumeToken.Val = append(consumeToken.Val, hourData.ConsumeToken)
 			consumeToken.Label = append(consumeToken.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits))
+			consumeToken.LabelText = "token"
 
 			activeUser.Count = hourData.ActiveUser
 			activeUser.Val = append(activeUser.Val, hourData.ActiveUser)
 			activeUser.Label = append(activeUser.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits))
+			activeUser.LabelText = "个"
 
 			newUser.Count += hourData.NewUser
 			newUser.Val = append(newUser.Val, hourData.NewUser)
 			newUser.Label = append(newUser.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits))
+			newUser.LabelText = "个"
 		}
 		hourLen := len(usageStatisticHour)
 		if hourLen > 0 {
@@ -193,7 +205,11 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 
 		var predicatesDay []predicate.UsageStatisticDay
 		predicatesDay = append(predicatesDay, usagestatisticday.OrganizationID(organizationId))
-		predicatesDay = append(predicatesDay, usagestatisticday.BotID(""))
+		if req.Wxid != nil {
+			predicatesDay = append(predicatesDay, usagestatisticday.BotID(*req.Wxid))
+		} else {
+			predicatesDay = append(predicatesDay, usagestatisticday.BotID(""))
+		}
 		predicatesDay = append(predicatesDay, usagestatisticday.AddtimeGTE(startAddTime))
 		predicatesDay = append(predicatesDay, usagestatisticday.AddtimeLTE(endAddTime))
 		usageStatisticDay, err := l.svcCtx.DB.UsageStatisticDay.Query().Where(predicatesDay...).All(l.ctx)
@@ -204,30 +220,37 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 			aiResponse.Count = aiResponse.Count + dayData.AiResponse
 			aiResponse.Val = append(aiResponse.Val, dayData.AiResponse)
 			aiResponse.Label = append(aiResponse.Label, fmt.Sprintf("%d", dayData.Addtime))
+			aiResponse.LabelText = "次"
 
 			sopRun.Count += dayData.SopRun
 			sopRun.Val = append(sopRun.Val, dayData.SopRun)
 			sopRun.Label = append(sopRun.Label, fmt.Sprintf("%d", dayData.Addtime))
+			sopRun.LabelText = "次"
 
 			totalFriend.Count = dayData.TotalFriend
 			totalFriend.Val = append(totalFriend.Val, dayData.TotalFriend)
 			totalFriend.Label = append(totalFriend.Label, fmt.Sprintf("%d", dayData.Addtime))
+			totalFriend.LabelText = "个"
 
 			totalGroup.Count = dayData.TotalGroup
 			totalGroup.Val = append(totalGroup.Val, dayData.TotalGroup)
 			totalGroup.Label = append(totalGroup.Label, fmt.Sprintf("%d", dayData.Addtime))
+			totalGroup.LabelText = "个"
 
 			consumeToken.Count += dayData.ConsumeToken
 			consumeToken.Val = append(consumeToken.Val, dayData.ConsumeToken)
 			consumeToken.Label = append(consumeToken.Label, fmt.Sprintf("%d", dayData.Addtime))
+			consumeToken.LabelText = "token"
 
 			activeUser.Count = dayData.ActiveUser
 			activeUser.Val = append(activeUser.Val, dayData.ActiveUser)
 			activeUser.Label = append(activeUser.Label, fmt.Sprintf("%d", dayData.Addtime))
+			activeUser.LabelText = "个"
 
 			newUser.Count += dayData.NewUser
 			newUser.Val = append(newUser.Val, dayData.NewUser)
 			newUser.Label = append(newUser.Label, fmt.Sprintf("%d", dayData.Addtime))
+			newUser.LabelText = "个"
 		}
 		dayLen := len(usageStatisticDay)
 		if dayLen > 0 {
@@ -258,7 +281,11 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 
 			var predicates []predicate.UsageStatisticHour
 			predicates = append(predicates, usagestatistichour.OrganizationID(organizationId))
-			predicates = append(predicates, usagestatistichour.BotID(""))
+			if req.Wxid != nil {
+				predicates = append(predicates, usagestatistichour.BotID(*req.Wxid))
+			} else {
+				predicates = append(predicates, usagestatistichour.BotID(""))
+			}
 			predicates = append(predicates, usagestatistichour.AddtimeGTE(startOfDayAddTime))
 			usageStatisticHour, err := l.svcCtx.DB.UsageStatisticHour.Query().Where(predicates...).All(l.ctx)
 			if err != nil {
@@ -287,10 +314,12 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 				aiResponse.Count = aiResponse.Count + aiResponseOfDay
 				aiResponse.Val = append(aiResponse.Val, aiResponseOfDay)
 				aiResponse.Label = append(aiResponse.Label, "今日")
+				aiResponse.LabelText = "次"
 
 				sopRun.Count = sopRun.Count + sopRunOfDay
 				sopRun.Val = append(sopRun.Val, sopRunOfDay)
 				sopRun.Label = append(sopRun.Label, "今日")
+				sopRun.LabelText = "次"
 
 				totalFriend.Count = totalFriendOfDay
 				totalFriend.Val = append(totalFriend.Val, totalFriendOfDay)
@@ -299,6 +328,7 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 				if tfLen > 0 && totalFriend.Val[0] > 0 {
 					totalFriend.Rate = float32((totalFriend.Val[tfLen-1] - totalFriend.Val[0]) / totalFriend.Val[0])
 				}
+				totalFriend.LabelText = "个"
 
 				totalGroup.Count = totalGroupOfDay
 				totalGroup.Val = append(totalGroup.Val, totalGroupOfDay)
@@ -307,10 +337,12 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 				if tgLen > 0 && totalGroup.Val[0] > 0 {
 					totalGroup.Rate = float32((totalGroup.Val[tgLen-1] - totalGroup.Val[0]) / totalGroup.Val[0])
 				}
+				totalGroup.LabelText = "个"
 
 				consumeToken.Count = consumeToken.Count + consumeTokenOfDay
 				consumeToken.Val = append(consumeToken.Val, consumeTokenOfDay)
 				consumeToken.Label = append(consumeToken.Label, "今日")
+				consumeToken.LabelText = "token"
 
 				activeUser.Count = activeUserOfDay
 				activeUser.Val = append(activeUser.Val, activeUserOfDay)
@@ -319,6 +351,7 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 				if auLen > 0 && activeUser.Val[0] > 0 {
 					activeUser.Rate = float32((activeUser.Val[auLen-1] - activeUser.Val[0]) / activeUser.Val[0])
 				}
+				activeUser.LabelText = "个"
 
 				newUser.Count = newUserOfDay
 				newUser.Val = append(newUser.Val, newUserOfDay)
@@ -327,6 +360,7 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 				if nuLen > 0 && newUser.Val[0] > 0 {
 					newUser.Rate = float32((newUser.Val[nuLen-1] - newUser.Val[0]) / newUser.Val[0])
 				}
+				newUser.LabelText = "个"
 
 				labelDists = usageStatisticHour[hourLen-1].LabelDist
 			}

+ 5 - 2
internal/logic/dashboard/get_wxs_logic.go

@@ -5,6 +5,7 @@ import (
 	"fmt"
 	"github.com/suyuan32/simple-admin-common/msg/errormsg"
 	"strconv"
+	"strings"
 	"time"
 	"wechat-api/ent"
 	"wechat-api/ent/predicate"
@@ -90,8 +91,10 @@ func (l *GetWxsLogic) GetWxs(req *types.WxReq) (resp *types.WxResp, err error) {
 	var wxIds []string
 	wxSet := make(map[string]*ent.Wx)
 	for _, w := range wxs {
-		wxIds = append(wxIds, w.Wxid)
-		wxSet[w.Wxid] = w
+		if !strings.HasPrefix(w.Wxid, "temp-") {
+			wxIds = append(wxIds, w.Wxid)
+			wxSet[w.Wxid] = w
+		}
 	}
 
 	var wxList []types.WxData

+ 40 - 8
internal/types/types.go

@@ -389,6 +389,30 @@ type WxListReq struct {
 	Callback *string `json:"callback,optional"`
 }
 
+// Get wx list request params | Wx列表请求参数
+// swagger:model WxSelectListReq
+type WxSelectListReq struct {
+	// 租户id
+	OrganizationId *uint64 `json:"organizationId,optional"`
+}
+
+// The response data of wx list | Wx列表数据
+// swagger:model WxSelectListResp
+type WxSelectListResp struct {
+	BaseDataInfo
+	// Wx list data | Wx列表数据
+	Data []WxSelectListInfo `json:"data"`
+}
+
+// swagger:model WxSelectListInfo
+type WxSelectListInfo struct {
+	BaseIDInfo
+	// 微信id
+	Wxid *string `json:"wxid,optional"`
+	// 微信昵称
+	Nickname *string `json:"nickname,optional"`
+}
+
 // Wx information response | Wx信息返回体
 // swagger:model WxInfoResp
 type WxInfoResp struct {
@@ -2905,12 +2929,18 @@ type MessageReq struct {
 	Text   *string `json:"text"`
 }
 
+// swagger:model DashboardInfo
+type DashboardInfo struct {
+	BaseIDInfo
+}
+
 // swagger:model ChartsReq
 type ChartsReq struct {
 	StartDate *string `json:"start_date"`
 	EndDate   *string `json:"end_date"`
 	// 租户id
 	OrganizationId *uint64 `json:"organizationId,optional"`
+	Wxid           *string `json:"wxid,optional"`
 }
 
 // swagger:model ChartsResp
@@ -2932,17 +2962,19 @@ type ChartsData struct {
 }
 
 type ChartsUint struct {
-	Count uint64   `json:"count"`
-	Rate  float32  `json:"rate"`
-	Label []string `json:"label"`
-	Val   []uint64 `json:"val"`
+	Count     uint64   `json:"count"`
+	Rate      float32  `json:"rate"`
+	Label     []string `json:"label"`
+	Val       []uint64 `json:"val"`
+	LabelText string   `json:"labelText"`
 }
 
 type ChartsInt struct {
-	Count int64    `json:"count"`
-	Rate  float32  `json:"rate"`
-	Label []string `json:"label"`
-	Val   []int64  `json:"val"`
+	Count     int64    `json:"count"`
+	Rate      float32  `json:"rate"`
+	Label     []string `json:"label"`
+	Val       []int64  `json:"val"`
+	LabelText string   `json:"labelText"`
 }
 
 type LabelsData struct {