123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package user
- import (
- "context"
- "encoding/json"
- "fmt"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/logger"
- "git.getensh.com/common/gopkgs/util"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- "gorm.io/gorm"
- "property-company/consts"
- "property-company/errors"
- dbmodel "property-company/model"
- pb_v1 "property-company/pb/v1"
- "time"
- )
- func checkChangePasswordParam(req *pb_v1.ChangePasswordRequest) error {
- switch {
- case req.Uid == 0:
- return status.Error(10003, "账号不能为空")
- case req.OldPassword == "":
- return status.Error(10003, "原密码不能为空")
- case req.NewPassword == "":
- return status.Error(10003, "密码不能为空")
- }
- return nil
- }
- //
- func ChangePassword(ctx context.Context, req *pb_v1.ChangePasswordRequest) (reply *pb_v1.ChangePasswordReply, err error) {
- reply = &pb_v1.ChangePasswordReply{}
- // 捕获各个task中的异常并返回给调用者
- defer func() {
- if r := recover(); r != nil {
- err = fmt.Errorf("%+v", r)
- e := &status.Status{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- logger.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- err = checkChangePasswordParam(req)
- if err != nil {
- return nil, err
- }
- user := &dbmodel.TUser{}
- where := map[string]interface{}{
- "id":req.Uid,
- }
- err = user.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound{
- return nil, errors.DataBaseError
- }
- if user.ID == 0 {
- return nil, errors.UserNotExist
- }
- cid := user.Cid
- oldUser := user.User
- oldpasswd, _ := util.GetMd5Pass(req.OldPassword, consts.CRYPTO_KEY)
- if oldpasswd != user.Password {
- return nil, errors.PasswordError
- }
- passwd, _ := util.GetMd5Pass(req.NewPassword, consts.CRYPTO_KEY)
- now := time.Now()
- values := map[string]interface{}{
- "password":passwd,
- "updated_at":now,
- }
- db := database.DB().Begin()
- err = user.Update(db, where, values)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- company := &dbmodel.TCompany{}
- where = map[string]interface{}{
- "id":cid,
- "user":oldUser,
- "approve_status":consts.ApproveStatusSuccess,
- }
- err = company.Update(db, where, values)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- db.Commit()
- return reply, nil
- }
|