Beispiel #1
0
func UpdateMyInfo(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
	repo, ok := middleware.RepoFromContext(ctx)
	if !ok {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误"})
		return
	}
	var info struct {
		ID          uint
		Password    string
		Name        string
		PhoneNumber string
		Permission  string
		Status      string
		Role        string
		AccessToken string
	}

	if err := r.DecodeJSONPayload(&info); err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()})
		return
	}

	info.Password = strings.TrimSpace(info.Password)
	info.Name = strings.TrimSpace(info.Name)
	info.PhoneNumber = strings.TrimSpace(info.PhoneNumber)
	info.AccessToken = strings.TrimSpace(info.AccessToken)

	_, errAccessToken := VerifyAccessToken(info.AccessToken, ctx, false)
	if errAccessToken != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": errAccessToken.Error()})
		return
	}

	user, err := repo.GetUserById(info.ID)
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}
	if info.Password == "" {
		info.Password = user.Password
	} else {
		encodePassword, err := util.EncodePassword(info.Password)
		if err != nil {
			w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
			return
		}
		info.Password = encodePassword
	}
	info.Permission = user.Permission
	info.Status = user.Status
	info.Role = user.Role

	mod, err := repo.UpdateUserById(info.ID, info.Password, info.Name, info.PhoneNumber, info.Permission, info.Status, info.Role)
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}

	w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "操作成功", "Content": mod})
}
Beispiel #2
0
func AddUser(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
	repo, ok := middleware.RepoFromContext(ctx)
	if !ok {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误"})
		return
	}
	var info struct {
		Username    string
		Password    string
		Name        string
		PhoneNumber string
		Permission  string
		Status      string
		Role        string
		AccessToken string
	}

	if err := r.DecodeJSONPayload(&info); err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误"})
		return
	}

	info.Username = strings.TrimSpace(info.Username)
	info.Password = strings.TrimSpace(info.Password)
	info.Name = strings.TrimSpace(info.Name)
	info.PhoneNumber = strings.TrimSpace(info.PhoneNumber)
	info.Permission = strings.TrimSpace(info.Permission)
	info.Status = strings.TrimSpace(info.Status)
	info.Role = strings.TrimSpace(info.Role)
	info.AccessToken = strings.TrimSpace(info.AccessToken)

	_, errAccessToken := VerifyAccessToken(info.AccessToken, ctx, true)
	if errAccessToken != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": errAccessToken.Error()})
		return
	}

	if info.Username == "" || info.Password == "" || info.Status == "" || info.Role == "" {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "请将信息填写完整!"})
		return
	}

	count, err := repo.CountUserByUsername(info.Username)
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}

	if count > 0 {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "该用户名已存在!"})
		return
	}

	encodePassword, err := util.EncodePassword(info.Password)
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}
	info.Password = encodePassword

	_, errAdd := repo.AddUser(info.Username, info.Password, info.Name, info.PhoneNumber, info.Permission, info.Status, info.Role)
	if errAdd != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": errAdd.Error()})
		return
	}

	w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "操作成功"})
}
Beispiel #3
0
func Login(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
	repo, ok := middleware.RepoFromContext(ctx)
	if !ok {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误"})
		return
	}
	var info struct {
		Username string
		Password string
	}

	if err := r.DecodeJSONPayload(&info); err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()})
		return
	}

	info.Username = strings.TrimSpace(info.Username)
	info.Password = strings.TrimSpace(info.Password)

	if info.Username == "" || info.Password == "" {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "用户名和密码不能为空"})
		return
	}

	count, err := repo.CountUserByUsername(info.Username)
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}

	if count <= 0 {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "用户名或密码错误!"})
		return
	}

	user, err := repo.GetUserByUsername(info.Username)
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}

	if user.Status != "Enable" {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "该账号已被锁定,请联系管理员解封!"})
		return
	}

	encodePassword, err := util.EncodePassword(info.Password)
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}
	if encodePassword != user.Password {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "用户名或密码错误"})
		return
	}

	session, err := store.Get(r.Request, "user-authentication")
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}

	token := fmt.Sprintf("%d", user.ID) + "_" + time.Now().String()
	accessToken, err := util.EncodePassword(token)
	accessToken = strings.ToUpper(accessToken)
	if err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()})
		return
	}
	_, errToken := repo.AddUserAccessToken(user.ID, accessToken)
	if errToken != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": errToken.Error()})
		return
	}

	session.Values["ID"] = user.ID
	session.Values["Username"] = user.Username
	session.Values["Name"] = user.Name
	session.Values["Role"] = user.Role
	session.Values["AccessToken"] = accessToken
	session.Save(r.Request, w)

	type userInfo struct {
		ID          uint
		Username    string
		Name        string
		Role        string
		AccessToken string
	}
	var userinfo userInfo
	userinfo.ID = user.ID
	userinfo.Username = user.Username
	userinfo.Name = user.Name
	userinfo.Role = user.Role
	userinfo.AccessToken = accessToken

	w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "登录成功", "Content": userinfo})
	return
}