forked from free-lancers/electricity_bill_calc_service
		
	feat(init):完成创建最初始用户功能。
This commit is contained in:
		
							
								
								
									
										40
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										40
									
								
								main.go
									
									
									
									
									
								
							| @@ -5,6 +5,7 @@ import ( | ||||
| 	"electricity_bill_calc/global" | ||||
| 	"electricity_bill_calc/model" | ||||
| 	"electricity_bill_calc/router" | ||||
| 	"electricity_bill_calc/service" | ||||
| 	"electricity_bill_calc/utils" | ||||
| 	"encoding/csv" | ||||
| 	"fmt" | ||||
| @@ -15,6 +16,7 @@ import ( | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/gin-gonic/gin" | ||||
| 	"github.com/shopspring/decimal" | ||||
| ) | ||||
|  | ||||
| func init() { | ||||
| @@ -58,6 +60,12 @@ func init() { | ||||
| 		log.Fatalf("Regions initialize failed: %v", err) | ||||
| 	} | ||||
| 	log.Println("Regions synchronized.") | ||||
|  | ||||
| 	err = intializeSingularity() | ||||
| 	if err != nil { | ||||
| 		log.Fatalf("Singularity account intialize failed: %v", err) | ||||
| 	} | ||||
| 	log.Println("Singularity account intialized.") | ||||
| } | ||||
|  | ||||
| func initializeRegions() error { | ||||
| @@ -102,6 +110,38 @@ func initializeRegions() error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func intializeSingularity() error { | ||||
| 	singularityExists, err := service.UserService.IsUserExists("000") | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("singularity detect failed: %w", err) | ||||
| 	} | ||||
| 	if singularityExists { | ||||
| 		return nil | ||||
| 	} | ||||
| 	singularity := &model.User{ | ||||
| 		Id:       "000", | ||||
| 		Username: "singularity", | ||||
| 		Type:     2, | ||||
| 		Enabled:  true, | ||||
| 	} | ||||
| 	singularityName := "Singularity" | ||||
| 	singularityExpires, err := time.Parse("2006-01-02 15:04:05", "2099-12-31 23:59:59") | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("singularity expires time parse failed: %w", err) | ||||
| 	} | ||||
| 	singularityDetail := &model.UserDetail{ | ||||
| 		Name:              &singularityName, | ||||
| 		UnitServiceFee:    decimal.Zero, | ||||
| 		ServiceExpiration: singularityExpires, | ||||
| 	} | ||||
| 	verifyCode, err := service.UserService.CreateUser(singularity, singularityDetail) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("singularity account failed to create: %w", err) | ||||
| 	} | ||||
| 	log.Printf("Singularity account created, use %s as verify code to reset password.", verifyCode) | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func DBConnectionKeepLive() { | ||||
| 	for range time.Tick(30 * time.Second) { | ||||
| 		err := global.DBConn.Ping() | ||||
|   | ||||
| @@ -10,6 +10,7 @@ import ( | ||||
| 	"electricity_bill_calc/repository" | ||||
| 	"electricity_bill_calc/utils" | ||||
| 	"fmt" | ||||
| 	"log" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/google/uuid" | ||||
| @@ -45,6 +46,10 @@ func (_UserService) ProcessEnterpriseUserLogin(username, password string) (*mode | ||||
| 		authErr.NeedReset = true | ||||
| 		return nil, authErr | ||||
| 	} | ||||
| 	userDetial, _ := repository.UserRepo.RetreiveUserDetail(user.Id) | ||||
| 	if userDetial.ServiceExpiration.Before(time.Now()) { | ||||
| 		return nil, exceptions.NewAuthenticationError(401, "用户服务期限已过。") | ||||
| 	} | ||||
| 	session := &model.Session{ | ||||
| 		Token:     uuid.New().String(), | ||||
| 		Uid:       user.Id, | ||||
| @@ -52,7 +57,6 @@ func (_UserService) ProcessEnterpriseUserLogin(username, password string) (*mode | ||||
| 		Name:      user.Username, | ||||
| 		ExpiresAt: time.Now().Add(config.ServiceSettings.MaxSessionLife), | ||||
| 	} | ||||
| 	userDetial, _ := repository.UserRepo.RetreiveUserDetail(user.Id) | ||||
| 	if userDetial != nil { | ||||
| 		session.Name = *userDetial.Name | ||||
| 	} | ||||
| @@ -109,7 +113,7 @@ func (_UserService) InvalidUserPassword(uid string) (string, error) { | ||||
| 	verifyCode := utils.RandStr(10) | ||||
| 	hash := sha512.New512_256() | ||||
| 	hash.Write([]byte(verifyCode)) | ||||
| 	user.Password = string(hash.Sum(nil)) | ||||
| 	user.Password = fmt.Sprintf("%x", hash.Sum(nil)) | ||||
| 	user.ResetNeeded = true | ||||
| 	affected, err := global.DBConn.ID(uid).Cols("password", "reset_needed").Update(user) | ||||
| 	if err != nil { | ||||
| @@ -132,7 +136,7 @@ func (_UserService) VerifyUserPassword(username, verifyCode string) (bool, error | ||||
| 	} | ||||
| 	hash := sha512.New512_256() | ||||
| 	hash.Write([]byte(verifyCode)) | ||||
| 	hashedVerifyCode := string(hash.Sum(nil)) | ||||
| 	hashedVerifyCode := fmt.Sprintf("%x", hash.Sum(nil)) | ||||
| 	if hashedVerifyCode != user.Password { | ||||
| 		return false, nil | ||||
| 	} else { | ||||
| @@ -147,7 +151,7 @@ func (_UserService) ResetUserPassword(username, password string) (bool, error) { | ||||
| 	} | ||||
| 	hash := sha512.New512_256() | ||||
| 	hash.Write([]byte(password)) | ||||
| 	user.Password = string(hash.Sum(nil)) | ||||
| 	user.Password = fmt.Sprintf("%x", hash.Sum(nil)) | ||||
| 	user.ResetNeeded = false | ||||
| 	affected, err := global.DBConn.ID(user.Id).Cols("password", "reset_needed").Update(user) | ||||
| 	if err != nil { | ||||
| @@ -161,3 +165,43 @@ func (_UserService) ResetUserPassword(username, password string) (bool, error) { | ||||
| 		return false, nil | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (_UserService) IsUserExists(uid string) (bool, error) { | ||||
| 	return global.DBConn.ID(uid).Exist(&model.User{}) | ||||
| } | ||||
|  | ||||
| func (_UserService) CreateUser(user *model.User, detail *model.UserDetail) (string, error) { | ||||
| 	if len(user.Id) == 0 { | ||||
| 		user.Id = uuid.New().String() | ||||
| 	} | ||||
| 	detail.Id = user.Id | ||||
|  | ||||
| 	verifyCode := utils.RandStr(10) | ||||
| 	hash := sha512.New512_256() | ||||
| 	hash.Write([]byte(verifyCode)) | ||||
| 	user.Password = fmt.Sprintf("%x", hash.Sum(nil)) | ||||
| 	user.ResetNeeded = true | ||||
|  | ||||
| 	tx := global.DBConn.NewSession() | ||||
| 	defer tx.Close() | ||||
| 	if err := tx.Begin(); err != nil { | ||||
| 		return "", err | ||||
| 	} | ||||
| 	log.Printf("[debug]user: %v", user) | ||||
| 	_, err := tx.Insert(user) | ||||
| 	if err != nil { | ||||
| 		tx.Rollback() | ||||
| 		return "", fmt.Errorf("user create failed: %w", err) | ||||
| 	} | ||||
| 	_, err = tx.Insert(detail) | ||||
| 	if err != nil { | ||||
| 		tx.Rollback() | ||||
| 		return "", fmt.Errorf("user Detail create failed: %w", err) | ||||
| 	} | ||||
| 	err = tx.Commit() | ||||
| 	if err != nil { | ||||
| 		tx.Rollback() | ||||
| 		return "", fmt.Errorf("transaction commit unsuccessful: %w", err) | ||||
| 	} | ||||
| 	return verifyCode, nil | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user