Przeglądaj źródła

用户信息更新

shenzekai 2 lat temu
rodzic
commit
5a9dedc37a

+ 12 - 1
db/enterprise.go

@@ -2,6 +2,7 @@ package db
 
 import (
 	"context"
+	"fmt"
 	"github.com/issue9/conv"
 	"time"
 	"youngee_b_api/model/gorm_model"
@@ -35,7 +36,7 @@ func GetEnterpriseByUID(ctx context.Context, userID int64) (*gorm_model.Enterpri
 	return &enterprise, nil
 }
 
-//企业ID查找
+// GetEnterpriseByEnterpriseID 企业ID查找
 func GetEnterpriseByEnterpriseID(ctx context.Context, EnterpriseID int64) (*gorm_model.Enterprise, error) {
 	db := GetReadDB(ctx)
 	enterprise := gorm_model.Enterprise{}
@@ -148,3 +149,13 @@ func RechargeAmount(ctx context.Context, EnterpriseID int64, Amount float64) err
 	}
 	return nil
 }
+
+func UpdateEnterprise(ctx context.Context, EnterpriseID int64, BusinessName string) error {
+	db := GetReadDB(ctx)
+	err := db.Model(gorm_model.Enterprise{}).Where("enterprise_id=?", EnterpriseID).Update("business_name", BusinessName).Error
+	if err != nil {
+		fmt.Println("Update Enterprise Failed!")
+		return err
+	}
+	return nil
+}

+ 12 - 1
db/user.go

@@ -32,7 +32,7 @@ func GetUserByPhone(ctx context.Context, phone string) (*gorm_model.YounggeeUser
 	return user, nil
 }
 
-//GetUserByPhone 查不到返回空
+// GetUserByID 查不到返回空
 func GetUserByID(ctx context.Context, ID int64) (*gorm_model.YounggeeUser, error) {
 	db := GetReadDB(ctx)
 	user := &gorm_model.YounggeeUser{}
@@ -46,3 +46,14 @@ func GetUserByID(ctx context.Context, ID int64) (*gorm_model.YounggeeUser, error
 	}
 	return user, nil
 }
+
+func UpdateUser(ctx context.Context, UserID int64, UserName string, Email string) (*int64, error) {
+	db := GetReadDB(ctx)
+	user := &gorm_model.YounggeeUser{}
+	err := db.Model(user).Where("id = ?", UserID).Updates(map[string]interface{}{"UserName": UserName, "Email": Email}).Error
+	if err != nil {
+		fmt.Println("Update User Failed!")
+		return nil, err
+	}
+	return &UserID, nil
+}

+ 58 - 0
handler/userinfo_find.go

@@ -0,0 +1,58 @@
+package handler
+
+import (
+	"youngee_b_api/consts"
+	"youngee_b_api/middleware"
+	"youngee_b_api/model/http_model"
+	"youngee_b_api/service"
+	"youngee_b_api/util"
+
+	"github.com/gin-gonic/gin"
+	"github.com/sirupsen/logrus"
+	log "github.com/sirupsen/logrus"
+)
+
+func WrapFindUserInfoHandler(ctx *gin.Context) {
+	handler := newFindUserInfoHandler(ctx)
+	baseRun(handler)
+}
+
+func newFindUserInfoHandler(ctx *gin.Context) *FindUserInfoHandler {
+	return &FindUserInfoHandler{
+		req:  http_model.NewFindUserInfoRequest(),
+		resp: http_model.NewFindUserInfoResponse(),
+		ctx:  ctx,
+	}
+}
+
+type FindUserInfoHandler struct {
+	req  *http_model.FindUserInfoRequest
+	resp *http_model.CommonResponse
+	ctx  *gin.Context
+}
+
+func (h *FindUserInfoHandler) getRequest() interface{} {
+	return h.req
+}
+func (h *FindUserInfoHandler) getContext() *gin.Context {
+	return h.ctx
+}
+func (h *FindUserInfoHandler) getResponse() interface{} {
+	return h.resp
+}
+func (h *FindUserInfoHandler) run() {
+	auth := middleware.GetSessionAuth(h.ctx)
+	//fmt.Printf("auth test %+v", auth)
+	res, err := service.User.Find(h.ctx, auth.EnterpriseID, auth.Phone, auth.ID, auth.Username, auth.Email)
+	if err != nil {
+		// 数据库查询失败,返回5001
+		logrus.Errorf("[FindUserInfoHandler] call FindByID err:%+v\n", err)
+		util.HandlerPackErrorResp(h.resp, consts.ErrorInternal, "")
+		log.Info("FindUserInfo fail,req:%+v", h.req)
+		return
+	}
+	h.resp.Data = res
+}
+func (h *FindUserInfoHandler) checkParam() error {
+	return nil
+}

+ 62 - 0
handler/userinfo_update.go

@@ -0,0 +1,62 @@
+package handler
+
+import (
+	"fmt"
+	"youngee_b_api/consts"
+	"youngee_b_api/middleware"
+	"youngee_b_api/model/http_model"
+	"youngee_b_api/service"
+	"youngee_b_api/util"
+
+	"github.com/gin-gonic/gin"
+	"github.com/sirupsen/logrus"
+	log "github.com/sirupsen/logrus"
+)
+
+func WrapUpdateUserInfoHandler(ctx *gin.Context) {
+	handler := newUpdateUserInfoHandler(ctx)
+	baseRun(handler)
+}
+
+func newUpdateUserInfoHandler(ctx *gin.Context) *UpdateUserInfoHandler {
+	return &UpdateUserInfoHandler{
+		req:  http_model.NewUpdateUserInfoRequest(),
+		resp: http_model.NewUpdateUserInfoResponse(),
+		ctx:  ctx,
+	}
+}
+
+type UpdateUserInfoHandler struct {
+	req  *http_model.UpdateUserInfoRequest
+	resp *http_model.CommonResponse
+	ctx  *gin.Context
+}
+
+func (h *UpdateUserInfoHandler) getRequest() interface{} {
+	return h.req
+}
+func (h *UpdateUserInfoHandler) getContext() *gin.Context {
+	return h.ctx
+}
+func (h *UpdateUserInfoHandler) getResponse() interface{} {
+	return h.resp
+}
+func (h *UpdateUserInfoHandler) run() {
+	auth := middleware.GetSessionAuth(h.ctx)
+	data := *&http_model.UpdateUserInfoRequest{}
+	data = *h.req
+	fmt.Printf("update auth %+v %+v ", auth, data)
+	err := service.User.Update(h.ctx, data, auth.ID, auth.EnterpriseID)
+	if err != nil {
+		// 数据库查询失败,返回5001
+		logrus.Errorf("[UpdateUserInfoHandler] call FindByID err:%+v\n", err)
+		util.HandlerPackErrorResp(h.resp, consts.ErrorInternal, "")
+		log.Info("UpdateUserInfo fail,req:%+v", h.req)
+		return
+	}
+
+	h.resp.Message = "更新用户信息成功!"
+}
+func (h *UpdateUserInfoHandler) checkParam() error {
+	return nil
+}

+ 21 - 0
model/http_model/userinfo_find.go

@@ -0,0 +1,21 @@
+package http_model
+
+type FindUserInfoRequest struct {
+}
+
+type FindUserInfoData struct {
+	ID           int64  `json:"id"`            // 用户表id
+	Phone        string `json:"phone"`         // 绑定手机
+	Username     string `json:"username"`      // 用户名称
+	BusinessName string `json:"business_name"` // 企业名称
+	Email        string `json:"email"`         // 电子邮件
+}
+
+func NewFindUserInfoRequest() *FindUserInfoRequest {
+	return new(FindUserInfoRequest)
+}
+func NewFindUserInfoResponse() *CommonResponse {
+	resp := new(CommonResponse)
+	resp.Data = new(FindUserInfoData)
+	return resp
+}

+ 19 - 0
model/http_model/userinfo_update.go

@@ -0,0 +1,19 @@
+package http_model
+
+type UpdateUserInfoRequest struct {
+	Username     string `json:"username"`      // 用户名称
+	BusinessName string `json:"business_name"` // 企业名称
+	Email        string `json:"email"`         // 电子邮件
+}
+
+type UpdateUserInfoData struct {
+}
+
+func NewUpdateUserInfoRequest() *UpdateUserInfoRequest {
+	return new(UpdateUserInfoRequest)
+}
+func NewUpdateUserInfoResponse() *CommonResponse {
+	resp := new(CommonResponse)
+	resp.Data = new(UpdateUserInfoData)
+	return resp
+}

+ 2 - 0
route/init.go

@@ -98,5 +98,7 @@ func InitRoute(r *gin.Engine) {
 		m.POST("/project/getsketchinfo", handler.WrapGetSketchInfoHandler)                 // 获取初稿
 		m.POST("/project/taskfinishlist", handler.WrapTaskFinishListHandler)               // 查询违约列表-数据违约
 		m.POST("/project/getfinishnumberinfo", handler.WrapGetFinishNumberInfoHandler)
+		m.GET("/project/finduserinfo", handler.WrapFindUserInfoHandler)      //获取账户信息
+		m.POST("/project/updatauserinfo", handler.WrapUpdateUserInfoHandler) //更新账户信息
 	}
 }

+ 44 - 0
service/user.go

@@ -0,0 +1,44 @@
+package service
+
+import (
+	"context"
+	"youngee_b_api/db"
+	"youngee_b_api/model/http_model"
+)
+
+var User *user
+
+type user struct {
+	Phone        string
+	User         string // 账号
+	Username     string // 后台用户名
+	Email        string // 电子邮件
+	EnterpriseID int64  //企业ID
+}
+
+func (*user) Update(ctx context.Context, newUser http_model.UpdateUserInfoRequest, ID int64, enterpriseID int64) error {
+	_, err := db.UpdateUser(ctx, ID, newUser.Username, newUser.Email)
+	if err != nil {
+		return err
+	}
+	err = db.UpdateEnterprise(ctx, enterpriseID, newUser.BusinessName)
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
+func (*user) Find(ctx context.Context, EnterpriseID int64, Phone string, ID int64, Username string, Email string) (*http_model.FindUserInfoData, error) {
+	enterprise, err := db.GetEnterpriseByEnterpriseID(ctx, EnterpriseID)
+	if err != nil {
+		return nil, err
+	}
+	UserInfo := http_model.FindUserInfoData{
+		ID:           ID,
+		Username:     Username,
+		Email:        Email,
+		Phone:        Phone,
+		BusinessName: enterprise.BusinessName,
+	}
+	return &UserInfo, nil
+}