// 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" "property-company/pb" pb_v1 "property-company/pb/v1" "time" ) func checkResetPasswordParam(req *pb_v1.ResetPasswordRequest) error { switch { case req.Phone == "": return status.Error(10003, "联系人电话不能为空") case req.Vcode < 1: return status.Error(10003, "短信验证码不能为空") case req.Password == "": return status.Error(10003, "密码不能为空") } return nil } func checkVcode(phone string, vcode uint32) error { mreq := &pb_v1.CheckVcodeRequest{Vcode: vcode, PhoneNumber: phone} _, err := pb.Thirdparty.CheckVcode(context.Background(), mreq) return err } // func ResetPassword(ctx context.Context, req *pb_v1.ResetPasswordRequest) (reply *pb_v1.ResetPasswordReply, err error) { reply = &pb_v1.ResetPasswordReply{} // 捕获各个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 = checkResetPasswordParam(req) if err != nil { return nil, err } user := &dbmodel.TUser{} where := map[string]interface{}{ "phone": req.Phone, } err = user.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if user.ID == 0 { return nil, errors.ErrRecordNotFound } company := &dbmodel.TCompany{} where = map[string]interface{}{ "id":user.Cid, } err = company.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound{ return nil, errors.DataBaseError } if company.ID == 0 { return reply, errors.ErrRecordNotFound } if company.ApproveStatus != consts.ApproveStatusSuccess { return nil, errors.UserStatusError } cid := user.Cid oldUser := user.User if err = checkVcode(req.Phone, req.Vcode); err != nil { return nil, err } p := &dbmodel.TUser{} where = map[string]interface{}{ "id": user.ID, } passwd, _ := util.GetMd5Pass(req.Password, consts.CRYPTO_KEY) now := time.Now() values := map[string]interface{}{ "password": passwd, "updated_at": now, } db := database.DB().Begin() err = p.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 }