forked from free-lancers/electricity_bill_calc_service
		
	refactor(deps):将一些工具函数用工具库替换。
This commit is contained in:
		| @@ -6,7 +6,6 @@ import ( | ||||
| 	"electricity_bill_calc/response" | ||||
| 	"electricity_bill_calc/security" | ||||
| 	"electricity_bill_calc/service" | ||||
| 	"electricity_bill_calc/utils" | ||||
| 	"fmt" | ||||
| 	"log" | ||||
| 	"net/http" | ||||
| @@ -14,6 +13,7 @@ import ( | ||||
|  | ||||
| 	"github.com/gin-gonic/gin" | ||||
| 	"github.com/jinzhu/copier" | ||||
| 	"github.com/samber/lo" | ||||
| 	"github.com/shopspring/decimal" | ||||
| ) | ||||
|  | ||||
| @@ -187,7 +187,7 @@ func batchImport04kVMeterArchive(c *gin.Context) { | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	mergedMeters := utils.Map(records, func(meter model.Meter04KV) model.Meter04KV { | ||||
| 	mergedMeters := lo.Map(records, func(meter model.Meter04KV, index int) model.Meter04KV { | ||||
| 		meter.ParkId = requestParkId | ||||
| 		meter.Enabled = true | ||||
| 		return meter | ||||
|   | ||||
| @@ -5,7 +5,7 @@ import ( | ||||
| 	"electricity_bill_calc/response" | ||||
| 	"electricity_bill_calc/security" | ||||
| 	"electricity_bill_calc/service" | ||||
| 	"electricity_bill_calc/utils" | ||||
| 	"electricity_bill_calc/tools" | ||||
| 	"net/http" | ||||
|  | ||||
| 	"github.com/gin-gonic/gin" | ||||
| @@ -94,7 +94,7 @@ func createNewPark(c *gin.Context) { | ||||
| 	copier.Copy(newPark, formData) | ||||
| 	newPark.Id = uuid.New().String() | ||||
| 	newPark.UserId = userSession.Uid | ||||
| 	nameAbbr := utils.PinyinAbbr(newPark.Name) | ||||
| 	nameAbbr := tools.PinyinAbbr(newPark.Name) | ||||
| 	newPark.Abbr = &nameAbbr | ||||
| 	newPark.Enabled = true | ||||
| 	err = service.ParkService.SaveNewPark(*newPark) | ||||
| @@ -125,7 +125,7 @@ func modifyPark(c *gin.Context) { | ||||
| 		return | ||||
| 	} | ||||
| 	copier.Copy(park, formData) | ||||
| 	nameAbbr := utils.PinyinAbbr(formData.Name) | ||||
| 	nameAbbr := tools.PinyinAbbr(formData.Name) | ||||
| 	park.Abbr = &nameAbbr | ||||
| 	err = service.ParkService.UpdateParkInfo(park) | ||||
| 	if err != nil { | ||||
|   | ||||
| @@ -1,7 +1,7 @@ | ||||
| package excel | ||||
|  | ||||
| import ( | ||||
| 	"electricity_bill_calc/utils" | ||||
| 	"electricity_bill_calc/tools" | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| @@ -9,6 +9,7 @@ import ( | ||||
| 	"strconv" | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/samber/lo" | ||||
| 	"github.com/shopspring/decimal" | ||||
| 	"github.com/xuri/excelize/v2" | ||||
| ) | ||||
| @@ -52,9 +53,9 @@ func (r *ColumnRecognizer) Recognize(cellValue string) bool { | ||||
| 	for _, p := range r.Pattern { | ||||
| 		matches = append(matches, strings.Contains(cellValue, p)) | ||||
| 	} | ||||
| 	return utils.Reduce(matches, true, func(acc, elem bool) bool { | ||||
| 	return lo.Reduce(matches, func(acc, elem bool, index int) bool { | ||||
| 		return acc && elem | ||||
| 	}) | ||||
| 	}, true) | ||||
| } | ||||
|  | ||||
| func NewExcelAnalyzer[T any](file io.Reader, recognizers []*ColumnRecognizer) (*ExcelAnalyzer[T], error) { | ||||
| @@ -154,7 +155,7 @@ func (a *ExcelAnalyzer[T]) Analysis(bean T) ([]T, []ExcelAnalysisError) { | ||||
| 								} | ||||
| 							} | ||||
| 						case "bool": | ||||
| 							if utils.ContainsInsensitive(matchValue, []string{"是", "yes", "y", "true", "t"}) { | ||||
| 							if tools.ContainsInsensitive(matchValue, []string{"是", "yes", "y", "true", "t"}) { | ||||
| 								actualField.SetBool(true) | ||||
| 							} else { | ||||
| 								actualField.SetBool(false) | ||||
|   | ||||
							
								
								
									
										3
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								go.mod
									
									
									
									
									
								
							| @@ -23,6 +23,7 @@ require ( | ||||
| 	github.com/cespare/xxhash/v2 v2.1.2 // indirect | ||||
| 	github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect | ||||
| 	github.com/fsnotify/fsnotify v1.5.4 // indirect | ||||
| 	github.com/fufuok/utils v0.7.13 // indirect | ||||
| 	github.com/gin-contrib/sse v0.1.0 // indirect | ||||
| 	github.com/go-playground/locales v0.14.0 // indirect | ||||
| 	github.com/go-playground/universal-translator v0.18.0 // indirect | ||||
| @@ -44,6 +45,7 @@ require ( | ||||
| 	github.com/pelletier/go-toml/v2 v2.0.2 // indirect | ||||
| 	github.com/richardlehane/mscfb v1.0.4 // indirect | ||||
| 	github.com/richardlehane/msoleps v1.0.1 // indirect | ||||
| 	github.com/samber/lo v1.27.0 // indirect | ||||
| 	github.com/spf13/afero v1.8.2 // indirect | ||||
| 	github.com/spf13/cast v1.5.0 // indirect | ||||
| 	github.com/spf13/jwalterweatherman v1.1.0 // indirect | ||||
| @@ -55,6 +57,7 @@ require ( | ||||
| 	github.com/xuri/efp v0.0.0-20220407160117-ad0f7a785be8 // indirect | ||||
| 	github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 // indirect | ||||
| 	golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect | ||||
| 	golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect | ||||
| 	golang.org/x/net v0.0.0-20220809012201-f428fae20770 // indirect | ||||
| 	golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664 // indirect | ||||
| 	golang.org/x/text v0.3.7 // indirect | ||||
|   | ||||
							
								
								
									
										6
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								go.sum
									
									
									
									
									
								
							| @@ -118,6 +118,8 @@ github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3 | ||||
| github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= | ||||
| github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI= | ||||
| github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= | ||||
| github.com/fufuok/utils v0.7.13 h1:FGx8Mnfg0ZB8HdVz1X60JJ2kFu1rtcsFDYUxUTzNKkU= | ||||
| github.com/fufuok/utils v0.7.13/go.mod h1:ztIaorPqZGdbvmW3YlwQp80K8rKJmEy6xa1KwpJSsmk= | ||||
| github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= | ||||
| github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= | ||||
| github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= | ||||
| @@ -471,6 +473,8 @@ github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OK | ||||
| github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= | ||||
| github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= | ||||
| github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= | ||||
| github.com/samber/lo v1.27.0 h1:GOyDWxsblvqYobqsmUuMddPa2/mMzkKyojlXol4+LaQ= | ||||
| github.com/samber/lo v1.27.0/go.mod h1:it33p9UtPMS7z72fP4gw/EIfQB2eI8ke7GR2wc6+Rhg= | ||||
| github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E= | ||||
| github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= | ||||
| github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= | ||||
| @@ -593,6 +597,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0 | ||||
| golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= | ||||
| golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= | ||||
| golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= | ||||
| golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM= | ||||
| golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE= | ||||
| golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= | ||||
| golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= | ||||
| golang.org/x/image v0.0.0-20211028202545-6944b10bf410 h1:hTftEOvwiOq2+O8k2D5/Q7COC7k5Qcrgc2TFURJYnvQ= | ||||
|   | ||||
							
								
								
									
										4
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								main.go
									
									
									
									
									
								
							| @@ -6,7 +6,6 @@ import ( | ||||
| 	"electricity_bill_calc/model" | ||||
| 	"electricity_bill_calc/router" | ||||
| 	"electricity_bill_calc/service" | ||||
| 	"electricity_bill_calc/utils" | ||||
| 	"encoding/csv" | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| @@ -17,6 +16,7 @@ import ( | ||||
|  | ||||
| 	"github.com/gin-gonic/gin" | ||||
| 	jsontime "github.com/liamylian/jsontime/v2/v2" | ||||
| 	"github.com/samber/lo" | ||||
| 	"github.com/shopspring/decimal" | ||||
| ) | ||||
|  | ||||
| @@ -99,7 +99,7 @@ func initializeRegions() error { | ||||
| 		if err == io.EOF { | ||||
| 			break | ||||
| 		} | ||||
| 		if utils.Contains(record[0], existRegions) { | ||||
| 		if lo.Contains(existRegions, record[0]) { | ||||
| 			continue | ||||
| 		} | ||||
| 		level, err := strconv.Atoi(record[2]) | ||||
|   | ||||
| @@ -5,9 +5,10 @@ import ( | ||||
| 	"electricity_bill_calc/exceptions" | ||||
| 	"electricity_bill_calc/global" | ||||
| 	"electricity_bill_calc/model" | ||||
| 	"electricity_bill_calc/utils" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/fufuok/utils" | ||||
| 	"github.com/samber/lo" | ||||
| 	"xorm.io/builder" | ||||
| 	"xorm.io/xorm" | ||||
| ) | ||||
| @@ -176,7 +177,7 @@ func (_ChargeService) ListPagedChargeRecord(keyword, beginDate, endDate string, | ||||
| 	} | ||||
| 	if len(beginDate) != 0 { | ||||
| 		beginTime, err := time.ParseInLocation("2006-01-02", beginDate, time.Local) | ||||
| 		beginTime = utils.VeryBeginOfDate(beginTime) | ||||
| 		beginTime = utils.BeginOfDay(beginTime) | ||||
| 		if err != nil { | ||||
| 			return make([]model.ChargeWithName, 0), 0, err | ||||
| 		} | ||||
| @@ -184,7 +185,7 @@ func (_ChargeService) ListPagedChargeRecord(keyword, beginDate, endDate string, | ||||
| 	} | ||||
| 	if len(endDate) != 0 { | ||||
| 		endTime, err := time.ParseInLocation("2006-01-02", endDate, time.Local) | ||||
| 		endTime = utils.VeryEndOfDate(endTime) | ||||
| 		endTime = utils.EndOfDay(endTime) | ||||
| 		if err != nil { | ||||
| 			return make([]model.ChargeWithName, 0), 0, err | ||||
| 		} | ||||
| @@ -222,16 +223,16 @@ func (_ChargeService) lastValidChargeTo(uid string) (time.Time, error) { | ||||
| 	if err != nil { | ||||
| 		return veryBlankTime, nil | ||||
| 	} | ||||
| 	mappedRecords := utils.Map(records, func(elem string) time.Time { | ||||
| 	mappedRecords := lo.Map(records, func(elem string, index int) time.Time { | ||||
| 		t, _ := time.Parse(time.RFC3339, elem) | ||||
| 		return utils.VeryBeginOfDate(t) | ||||
| 		return utils.BeginOfDay(t) | ||||
| 	}) | ||||
| 	lastValid := utils.Reduce(mappedRecords, veryBlankTime, func(acc, elem time.Time) time.Time { | ||||
| 	lastValid := lo.Reduce(mappedRecords, func(acc, elem time.Time, index int) time.Time { | ||||
| 		if elem.After(acc) { | ||||
| 			return elem | ||||
| 		} else { | ||||
| 			return acc | ||||
| 		} | ||||
| 	}) | ||||
| 	}, veryBlankTime) | ||||
| 	return lastValid, nil | ||||
| } | ||||
|   | ||||
| @@ -5,10 +5,10 @@ import ( | ||||
| 	"electricity_bill_calc/excel" | ||||
| 	"electricity_bill_calc/global" | ||||
| 	"electricity_bill_calc/model" | ||||
| 	"electricity_bill_calc/utils" | ||||
| 	"fmt" | ||||
|  | ||||
| 	mapset "github.com/deckarep/golang-set/v2" | ||||
| 	"github.com/samber/lo" | ||||
| 	"xorm.io/builder" | ||||
| 	"xorm.io/xorm" | ||||
| ) | ||||
| @@ -123,10 +123,10 @@ func (_Meter04kVService) DuplicateMeterCodeValidate(meters []model.Meter04KV) [] | ||||
| } | ||||
|  | ||||
| func (m _Meter04kVService) BatchCreateMeter(meters []model.Meter04KV) error { | ||||
| 	parkIds := utils.Reduce(meters, mapset.NewSet[string](), func(acc mapset.Set[string], elem model.Meter04KV) mapset.Set[string] { | ||||
| 	parkIds := lo.Reduce(meters, func(acc mapset.Set[string], elem model.Meter04KV, index int) mapset.Set[string] { | ||||
| 		acc.Add(elem.ParkId) | ||||
| 		return acc | ||||
| 	}) | ||||
| 	}, mapset.NewSet[string]()) | ||||
| 	if parkIds.Cardinality() > 1 { | ||||
| 		return fmt.Errorf("一次只能向同一个园区中添加0.4kV表计。") | ||||
| 	} | ||||
|   | ||||
| @@ -3,10 +3,11 @@ package service | ||||
| import ( | ||||
| 	"electricity_bill_calc/global" | ||||
| 	"electricity_bill_calc/model" | ||||
| 	"electricity_bill_calc/utils" | ||||
| 	"electricity_bill_calc/tools" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/google/uuid" | ||||
| 	"github.com/samber/lo" | ||||
| 	"xorm.io/builder" | ||||
| ) | ||||
|  | ||||
| @@ -24,10 +25,9 @@ func (_ReportService) FetchParksWithNewestReport(uid string) ([]model.ParkNewest | ||||
| 	if err != nil { | ||||
| 		return make([]model.ParkNewestReport, 0), err | ||||
| 	} | ||||
| 	reducedParks := utils.Reduce( | ||||
| 	reducedParks := lo.Reduce( | ||||
| 		parks, | ||||
| 		make(map[string]model.ParkNewestReport, 0), | ||||
| 		func(acc map[string]model.ParkNewestReport, elem model.ParkNewestReport) map[string]model.ParkNewestReport { | ||||
| 		func(acc map[string]model.ParkNewestReport, elem model.ParkNewestReport, index int) map[string]model.ParkNewestReport { | ||||
| 			if v, ok := acc[elem.Park.Id]; ok { | ||||
| 				if elem.Report != nil { | ||||
| 					if v.Report == nil || (elem.Report.Period.After(v.Report.Period)) { | ||||
| @@ -39,8 +39,9 @@ func (_ReportService) FetchParksWithNewestReport(uid string) ([]model.ParkNewest | ||||
| 			} | ||||
| 			return acc | ||||
| 		}, | ||||
| 		make(map[string]model.ParkNewestReport, 0), | ||||
| 	) | ||||
| 	return utils.Values(reducedParks), nil | ||||
| 	return lo.Values(reducedParks), nil | ||||
| } | ||||
|  | ||||
| func (_ReportService) IsNewPeriodValid(uid string, period time.Time) (bool, error) { | ||||
| @@ -54,25 +55,24 @@ func (_ReportService) IsNewPeriodValid(uid string, period time.Time) (bool, erro | ||||
| 		return false, nil | ||||
| 	} | ||||
| 	// 检查给定的期数在目前的记录中是否已经存在 | ||||
| 	exists := utils.Reduce( | ||||
| 	exists := lo.Reduce( | ||||
| 		reports, | ||||
| 		false, | ||||
| 		func(acc bool, elem model.Report) bool { | ||||
| 		func(acc bool, elem model.Report, index int) bool { | ||||
| 			if elem.Period.Equal(period) { | ||||
| 				return acc || true | ||||
| 			} else { | ||||
| 				return acc || false | ||||
| 			} | ||||
| 		}, | ||||
| 		false, | ||||
| 	) | ||||
| 	if exists { | ||||
| 		return false, nil | ||||
| 	} | ||||
| 	// 检查给定的期数与目前已发布的最大期数的关系 | ||||
| 	maxPublished := utils.Reduce( | ||||
| 	maxPublished := lo.Reduce( | ||||
| 		reports, | ||||
| 		nil, | ||||
| 		func(acc *time.Time, elem model.Report) *time.Time { | ||||
| 		func(acc *time.Time, elem model.Report, index int) *time.Time { | ||||
| 			if elem.Published { | ||||
| 				if acc == nil || (acc != nil && elem.Period.After(*acc)) { | ||||
| 					return &elem.Period | ||||
| @@ -80,24 +80,25 @@ func (_ReportService) IsNewPeriodValid(uid string, period time.Time) (bool, erro | ||||
| 			} | ||||
| 			return acc | ||||
| 		}, | ||||
| 		nil, | ||||
| 	) | ||||
| 	// 检查给定的期数与目前未发布的最大期数的关系 | ||||
| 	maxUnpublished := utils.Reduce( | ||||
| 	maxUnpublished := lo.Reduce( | ||||
| 		reports, | ||||
| 		nil, | ||||
| 		func(acc *time.Time, elem model.Report) *time.Time { | ||||
| 		func(acc *time.Time, elem model.Report, index int) *time.Time { | ||||
| 			if acc == nil || (acc != nil && elem.Period.After(*acc)) { | ||||
| 				return &elem.Period | ||||
| 			} | ||||
| 			return acc | ||||
| 		}, | ||||
| 		nil, | ||||
| 	) | ||||
| 	if maxUnpublished == nil { | ||||
| 		return true, nil | ||||
| 	} | ||||
| 	if maxPublished != nil && maxUnpublished.Equal(*maxPublished) { | ||||
| 		// 此时不存在未发布的报表 | ||||
| 		return utils.IsNextMonth(*maxPublished, period), nil | ||||
| 		return tools.IsNextMonth(*maxPublished, period), nil | ||||
| 	} else { | ||||
| 		// 存在未发布的报表 | ||||
| 		return false, nil | ||||
|   | ||||
| @@ -1,16 +1,16 @@ | ||||
| package service | ||||
|  | ||||
| import ( | ||||
| 	"crypto/sha512" | ||||
| 	"electricity_bill_calc/cache" | ||||
| 	"electricity_bill_calc/config" | ||||
| 	"electricity_bill_calc/exceptions" | ||||
| 	"electricity_bill_calc/global" | ||||
| 	"electricity_bill_calc/model" | ||||
| 	"electricity_bill_calc/utils" | ||||
| 	"electricity_bill_calc/tools" | ||||
| 	"fmt" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/fufuok/utils" | ||||
| 	"github.com/google/uuid" | ||||
| 	"xorm.io/builder" | ||||
| ) | ||||
| @@ -34,9 +34,7 @@ func (u _UserService) ProcessEnterpriseUserLogin(username, password string) (*mo | ||||
| 	if !user.Enabled { | ||||
| 		return nil, exceptions.NewAuthenticationError(401, "用户已被禁用。") | ||||
| 	} | ||||
| 	hash := sha512.New512_256() | ||||
| 	hash.Write([]byte(password)) | ||||
| 	hashedPassword := fmt.Sprintf("%x", hash.Sum(nil)) | ||||
| 	hashedPassword := utils.Sha512Hex(password) | ||||
| 	if hashedPassword != user.Password { | ||||
| 		return nil, exceptions.NewAuthenticationError(401, "用户凭据不正确。") | ||||
| 	} | ||||
| @@ -78,9 +76,7 @@ func (u _UserService) ProcessManagementUserLogin(username, password string) (*mo | ||||
| 	if !user.Enabled { | ||||
| 		return nil, exceptions.NewAuthenticationError(401, "用户已被禁用。") | ||||
| 	} | ||||
| 	hash := sha512.New512_256() | ||||
| 	hash.Write([]byte(password)) | ||||
| 	hashedPassword := fmt.Sprintf("%x", hash.Sum(nil)) | ||||
| 	hashedPassword := utils.Sha512Hex(password) | ||||
| 	if hashedPassword != user.Password { | ||||
| 		return nil, exceptions.NewAuthenticationError(401, "用户凭据不正确。") | ||||
| 	} | ||||
| @@ -109,10 +105,8 @@ func (u _UserService) InvalidUserPassword(uid string) (string, error) { | ||||
| 	if user == nil && err != nil { | ||||
| 		return "", exceptions.NewNotFoundError("指定的用户不存在。") | ||||
| 	} | ||||
| 	verifyCode := utils.RandStr(10) | ||||
| 	hash := sha512.New512_256() | ||||
| 	hash.Write([]byte(verifyCode)) | ||||
| 	user.Password = fmt.Sprintf("%x", hash.Sum(nil)) | ||||
| 	verifyCode := tools.RandStr(10) | ||||
| 	user.Password = utils.Sha512Hex(verifyCode) | ||||
| 	user.ResetNeeded = true | ||||
| 	affected, err := global.DBConn.ID(uid).Cols("password", "reset_needed").Update(user) | ||||
| 	if err != nil { | ||||
| @@ -133,9 +127,7 @@ func (u _UserService) VerifyUserPassword(username, verifyCode string) (bool, err | ||||
| 	if user == nil || err != nil { | ||||
| 		return false, exceptions.NewNotFoundError("指定的用户不存在。") | ||||
| 	} | ||||
| 	hash := sha512.New512_256() | ||||
| 	hash.Write([]byte(verifyCode)) | ||||
| 	hashedVerifyCode := fmt.Sprintf("%x", hash.Sum(nil)) | ||||
| 	hashedVerifyCode := utils.Sha512Hex(verifyCode) | ||||
| 	if hashedVerifyCode != user.Password { | ||||
| 		return false, nil | ||||
| 	} else { | ||||
| @@ -148,9 +140,7 @@ func (u _UserService) ResetUserPassword(username, password string) (bool, error) | ||||
| 	if user == nil || err != nil { | ||||
| 		return false, exceptions.NewNotFoundError("指定的用户不存在。") | ||||
| 	} | ||||
| 	hash := sha512.New512_256() | ||||
| 	hash.Write([]byte(password)) | ||||
| 	user.Password = fmt.Sprintf("%x", hash.Sum(nil)) | ||||
| 	user.Password = utils.Sha512Hex(password) | ||||
| 	user.ResetNeeded = false | ||||
| 	affected, err := global.DBConn.ID(user.Id).Cols("password", "reset_needed").Update(user) | ||||
| 	if err != nil { | ||||
| @@ -186,14 +176,12 @@ func (u _UserService) CreateUser(user *model.User, detail *model.UserDetail) (st | ||||
| 	} | ||||
| 	detail.Id = user.Id | ||||
|  | ||||
| 	verifyCode := utils.RandStr(10) | ||||
| 	hash := sha512.New512_256() | ||||
| 	hash.Write([]byte(verifyCode)) | ||||
| 	user.Password = fmt.Sprintf("%x", hash.Sum(nil)) | ||||
| 	verifyCode := tools.RandStr(10) | ||||
| 	user.Password = utils.Sha512Hex(verifyCode) | ||||
| 	user.ResetNeeded = true | ||||
|  | ||||
| 	if detail.Name != nil { | ||||
| 		finalAbbr := utils.PinyinAbbr(*detail.Name) | ||||
| 		finalAbbr := tools.PinyinAbbr(*detail.Name) | ||||
| 		detail.Abbr = &finalAbbr | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -1,17 +1,9 @@ | ||||
| package utils | ||||
| package tools | ||||
| 
 | ||||
| import ( | ||||
| 	"time" | ||||
| ) | ||||
| 
 | ||||
| func VeryBeginOfDate(date time.Time) time.Time { | ||||
| 	return time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, date.Location()) | ||||
| } | ||||
| 
 | ||||
| func VeryEndOfDate(date time.Time) time.Time { | ||||
| 	return time.Date(date.Year(), date.Month(), date.Day(), 23, 59, 59, 999999999, date.Location()) | ||||
| } | ||||
| 
 | ||||
| func DifferenceInMonth(t1, t2 time.Time) int { | ||||
| 	var differYear, differMonth int | ||||
| 	differYear = t1.Year() - t2.Year() | ||||
							
								
								
									
										23
									
								
								tools/utils.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								tools/utils.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | ||||
| package tools | ||||
|  | ||||
| import ( | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/mozillazg/go-pinyin" | ||||
| 	"github.com/samber/lo" | ||||
| ) | ||||
|  | ||||
| func ContainsInsensitive(element string, slice []string) bool { | ||||
| 	lowercasedElement := strings.TrimSpace(strings.ToLower(element)) | ||||
| 	return lo.Contains(slice, lowercasedElement) | ||||
| } | ||||
|  | ||||
| func PinyinAbbr(source string) string { | ||||
| 	abbr := pinyin.Pinyin(source, pinyin.NewArgs()) | ||||
| 	var abbrCollect = make([]string, 0) | ||||
| 	for _, a := range abbr { | ||||
| 		abbrCollect = append(abbrCollect, a[0][0:1]) | ||||
| 	} | ||||
| 	finalAbbr := strings.Join(abbrCollect, "") | ||||
| 	return finalAbbr | ||||
| } | ||||
| @@ -1,4 +1,4 @@ | ||||
| package utils | ||||
| package tools | ||||
| 
 | ||||
| import ( | ||||
| 	"math/rand" | ||||
| @@ -1,35 +0,0 @@ | ||||
| package utils | ||||
|  | ||||
| func Map[S, T any](source []S, f func(S) T) []T { | ||||
| 	dest := make([]T, len(source)) | ||||
| 	for i, elem := range source { | ||||
| 		dest[i] = f(elem) | ||||
| 	} | ||||
| 	return dest | ||||
| } | ||||
|  | ||||
| func Reduce[S, T any](source []S, initialValue T, f func(T, S) T) T { | ||||
| 	acc := initialValue | ||||
| 	for _, elem := range source { | ||||
| 		acc = f(acc, elem) | ||||
| 	} | ||||
| 	return acc | ||||
| } | ||||
|  | ||||
| func ReduceIndexed[S, T any](source []S, initialValue T, f func(T, S, int, []S) T) T { | ||||
| 	acc := initialValue | ||||
| 	for index, elem := range source { | ||||
| 		acc = f(acc, elem, index, source[:]) | ||||
| 	} | ||||
| 	return acc | ||||
| } | ||||
|  | ||||
| func Filter[T any](source []T, f func(T) bool) []T { | ||||
| 	var dest []T | ||||
| 	for _, elem := range source { | ||||
| 		if f(elem) { | ||||
| 			dest = append(dest, elem) | ||||
| 		} | ||||
| 	} | ||||
| 	return dest | ||||
| } | ||||
| @@ -1,47 +0,0 @@ | ||||
| package utils | ||||
|  | ||||
| import ( | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/mozillazg/go-pinyin" | ||||
| ) | ||||
|  | ||||
| func Contains[T string | int | uint](element T, slice []T) bool { | ||||
| 	for _, v := range slice { | ||||
| 		if v == element { | ||||
| 			return true | ||||
| 		} | ||||
| 	} | ||||
| 	return false | ||||
| } | ||||
|  | ||||
| func ContainsInsensitive(element string, slice []string) bool { | ||||
| 	lowercasedElement := strings.TrimSpace(strings.ToLower(element)) | ||||
| 	return Contains(lowercasedElement, slice) | ||||
| } | ||||
|  | ||||
| func PinyinAbbr(source string) string { | ||||
| 	abbr := pinyin.Pinyin(source, pinyin.NewArgs()) | ||||
| 	var abbrCollect = make([]string, 0) | ||||
| 	for _, a := range abbr { | ||||
| 		abbrCollect = append(abbrCollect, a[0][0:1]) | ||||
| 	} | ||||
| 	finalAbbr := strings.Join(abbrCollect, "") | ||||
| 	return finalAbbr | ||||
| } | ||||
|  | ||||
| func Keys[T comparable, V any](m map[T]V) []T { | ||||
| 	keys := make([]T, 0, len(m)) | ||||
| 	for key := range m { | ||||
| 		keys = append(keys, key) | ||||
| 	} | ||||
| 	return keys | ||||
| } | ||||
|  | ||||
| func Values[K comparable, V any](m map[K]V) []V { | ||||
| 	values := make([]V, 0, len(m)) | ||||
| 	for _, v := range m { | ||||
| 		values = append(values, v) | ||||
| 	} | ||||
| 	return values | ||||
| } | ||||
		Reference in New Issue
	
	Block a user