123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- // 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-system/consts"
- "property-system/errors"
- dbmodel "property-system/model"
- "property-system/pb"
- pb_v1 "property-system/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 resetPasswordWithUid(req *pb_v1.ResetPasswordRequest) (reply *pb_v1.ResetPasswordReply, err error) {
- reply = &pb_v1.ResetPasswordReply{}
- user := &dbmodel.TUser{}
- where := map[string]interface{}{
- "id":req.Uid,
- "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.UserNotExist
- }
- if err = checkVcode(req.Phone, req.Vcode); err != nil {
- return nil, err
- }
- item := &pb_v1.ResetPasswordData{
- Uid:req.Uid,
- Username:user.User,
- GardenName:user.GardenName,
- }
- reply.List = make([]*pb_v1.ResetPasswordData, 1)
- reply.List[0] = item
- passwd, _ := util.GetMd5Pass(req.Password, consts.CRYPTO_KEY)
- now := time.Now()
- values := map[string]interface{}{
- "password":passwd,
- "updated_at":now,
- }
- err = user.Update(database.DB(), where, values)
- if err != nil {
- return nil, errors.DataBaseError
- }
- return reply, nil
- }
- //
- 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
- }
- if req.Uid > 0 {
- return resetPasswordWithUid(req)
- }
- user := &dbmodel.TUser{}
- where := map[string]interface{}{
- "phone":req.Phone,
- }
- list, err := user.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return nil, errors.DataBaseError
- }
- if len(list) == 0 {
- return nil, errors.ErrRecordNotFound
- }
- if err = checkVcode(req.Phone, req.Vcode); err != nil {
- return nil, err
- }
- reply.List = make([]*pb_v1.ResetPasswordData, len(list))
- for i, v := range list {
- item := &pb_v1.ResetPasswordData{
- Uid:v.ID,
- Username:v.User,
- GardenName:v.GardenName,
- }
- reply.List[i] = item
- }
- if len(list) == 1 {
- user := &dbmodel.TUser{}
- where := map[string]interface{}{
- "id":list[0].ID,
- }
- passwd, _ := util.GetMd5Pass(req.Password, consts.CRYPTO_KEY)
- now := time.Now()
- values := map[string]interface{}{
- "password":passwd,
- "updated_at":now,
- }
- err = user.Update(database.DB(), where, values)
- if err != nil {
- return nil, errors.DataBaseError
- }
- }
- return reply, nil
- }
|