mirror of
				https://github.com/TeaOSLab/EdgeAdmin.git
				synced 2025-11-04 05:00:25 +08:00 
			
		
		
		
	
		
			
	
	
		
			113 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
		
		
			
		
	
	
			113 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| 
								 | 
							
								package log
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								import (
							 | 
						|||
| 
								 | 
							
									"bytes"
							 | 
						|||
| 
								 | 
							
									"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
							 | 
						|||
| 
								 | 
							
									"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
							 | 
						|||
| 
								 | 
							
									"github.com/iwind/TeaGo/lists"
							 | 
						|||
| 
								 | 
							
									timeutil "github.com/iwind/TeaGo/utils/time"
							 | 
						|||
| 
								 | 
							
									"github.com/tealeg/xlsx/v3"
							 | 
						|||
| 
								 | 
							
									"strconv"
							 | 
						|||
| 
								 | 
							
									"strings"
							 | 
						|||
| 
								 | 
							
								)
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								type ExportExcelAction struct {
							 | 
						|||
| 
								 | 
							
									actionutils.ParentAction
							 | 
						|||
| 
								 | 
							
								}
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								func (this *ExportExcelAction) Init() {
							 | 
						|||
| 
								 | 
							
									this.Nav("", "", "")
							 | 
						|||
| 
								 | 
							
								}
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								func (this *ExportExcelAction) RunGet(params struct {
							 | 
						|||
| 
								 | 
							
									DayFrom string
							 | 
						|||
| 
								 | 
							
									DayTo   string
							 | 
						|||
| 
								 | 
							
									Keyword string
							 | 
						|||
| 
								 | 
							
								}) {
							 | 
						|||
| 
								 | 
							
									logsResp, err := this.RPC().LogRPC().ListLogs(this.AdminContext(), &pb.ListLogsRequest{
							 | 
						|||
| 
								 | 
							
										Offset:  0,
							 | 
						|||
| 
								 | 
							
										Size:    1000, // 日志最大导出1000条,TODO 将来可以配置
							 | 
						|||
| 
								 | 
							
										DayFrom: params.DayFrom,
							 | 
						|||
| 
								 | 
							
										DayTo:   params.DayTo,
							 | 
						|||
| 
								 | 
							
										Keyword: params.Keyword,
							 | 
						|||
| 
								 | 
							
									})
							 | 
						|||
| 
								 | 
							
									if err != nil {
							 | 
						|||
| 
								 | 
							
										this.ErrorPage(err)
							 | 
						|||
| 
								 | 
							
										return
							 | 
						|||
| 
								 | 
							
									}
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
									wb := xlsx.NewFile()
							 | 
						|||
| 
								 | 
							
									sheet, err := wb.AddSheet("default")
							 | 
						|||
| 
								 | 
							
									if err != nil {
							 | 
						|||
| 
								 | 
							
										this.ErrorPage(err)
							 | 
						|||
| 
								 | 
							
										return
							 | 
						|||
| 
								 | 
							
									}
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
									// 头部
							 | 
						|||
| 
								 | 
							
									{
							 | 
						|||
| 
								 | 
							
										row := sheet.AddRow()
							 | 
						|||
| 
								 | 
							
										row.SetHeight(25)
							 | 
						|||
| 
								 | 
							
										row.AddCell().SetString("ID")
							 | 
						|||
| 
								 | 
							
										row.AddCell().SetString("日期")
							 | 
						|||
| 
								 | 
							
										row.AddCell().SetString("用户")
							 | 
						|||
| 
								 | 
							
										row.AddCell().SetString("描述")
							 | 
						|||
| 
								 | 
							
										row.AddCell().SetString("IP")
							 | 
						|||
| 
								 | 
							
										row.AddCell().SetString("区域")
							 | 
						|||
| 
								 | 
							
										row.AddCell().SetString("运营商")
							 | 
						|||
| 
								 | 
							
										row.AddCell().SetString("页面地址")
							 | 
						|||
| 
								 | 
							
									}
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
									// 数据
							 | 
						|||
| 
								 | 
							
									for _, log := range logsResp.Logs {
							 | 
						|||
| 
								 | 
							
										regionName := ""
							 | 
						|||
| 
								 | 
							
										ispName := ""
							 | 
						|||
| 
								 | 
							
										regionResp, err := this.RPC().IPLibraryRPC().LookupIPRegion(this.AdminContext(), &pb.LookupIPRegionRequest{Ip: log.Ip})
							 | 
						|||
| 
								 | 
							
										if err != nil {
							 | 
						|||
| 
								 | 
							
											this.ErrorPage(err)
							 | 
						|||
| 
								 | 
							
											return
							 | 
						|||
| 
								 | 
							
										}
							 | 
						|||
| 
								 | 
							
										if regionResp.Region != nil {
							 | 
						|||
| 
								 | 
							
											pieces := []string{}
							 | 
						|||
| 
								 | 
							
											if len(regionResp.Region.Country) > 0 {
							 | 
						|||
| 
								 | 
							
												pieces = append(pieces, regionResp.Region.Country)
							 | 
						|||
| 
								 | 
							
											}
							 | 
						|||
| 
								 | 
							
											if len(regionResp.Region.Province) > 0 && !lists.ContainsString(pieces, regionResp.Region.Province) {
							 | 
						|||
| 
								 | 
							
												pieces = append(pieces, regionResp.Region.Province)
							 | 
						|||
| 
								 | 
							
											}
							 | 
						|||
| 
								 | 
							
											if len(regionResp.Region.City) > 0 && !lists.ContainsString(pieces, regionResp.Region.City) && !lists.ContainsString(pieces, strings.TrimSuffix(regionResp.Region.Province, "市")) {
							 | 
						|||
| 
								 | 
							
												pieces = append(pieces, regionResp.Region.City)
							 | 
						|||
| 
								 | 
							
											}
							 | 
						|||
| 
								 | 
							
											regionName = strings.Join(pieces, " ")
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
											if len(regionResp.Region.Isp) > 0 {
							 | 
						|||
| 
								 | 
							
												ispName = regionResp.Region.Isp
							 | 
						|||
| 
								 | 
							
											}
							 | 
						|||
| 
								 | 
							
										}
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										row := sheet.AddRow()
							 | 
						|||
| 
								 | 
							
										row.SetHeight(25)
							 | 
						|||
| 
								 | 
							
										row.AddCell().SetInt64(log.Id)
							 | 
						|||
| 
								 | 
							
										row.AddCell().SetString(timeutil.FormatTime("Y-m-d H:i:s", log.CreatedAt))
							 | 
						|||
| 
								 | 
							
										row.AddCell().SetString(log.UserName)
							 | 
						|||
| 
								 | 
							
										row.AddCell().SetString(log.Description)
							 | 
						|||
| 
								 | 
							
										row.AddCell().SetString(log.Ip)
							 | 
						|||
| 
								 | 
							
										row.AddCell().SetString(regionName)
							 | 
						|||
| 
								 | 
							
										row.AddCell().SetString(ispName)
							 | 
						|||
| 
								 | 
							
										row.AddCell().SetString(log.Action)
							 | 
						|||
| 
								 | 
							
									}
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
									this.AddHeader("Content-Type", "application/vnd.ms-excel")
							 | 
						|||
| 
								 | 
							
									this.AddHeader("Content-Disposition", "attachment; filename=\"LOG-"+timeutil.Format("YmdHis")+".xlsx\"")
							 | 
						|||
| 
								 | 
							
									this.AddHeader("Cache-Control", "max-age=0")
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
									buf := bytes.NewBuffer([]byte{})
							 | 
						|||
| 
								 | 
							
									err = wb.Write(buf)
							 | 
						|||
| 
								 | 
							
									if err != nil {
							 | 
						|||
| 
								 | 
							
										this.ErrorPage(err)
							 | 
						|||
| 
								 | 
							
										return
							 | 
						|||
| 
								 | 
							
									}
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
									this.AddHeader("Content-Length", strconv.Itoa(buf.Len()))
							 | 
						|||
| 
								 | 
							
									this.Write(buf.Bytes())
							 | 
						|||
| 
								 | 
							
								}
							 |