作者 yangfu

用户中心修改

... ... @@ -38,6 +38,33 @@ func UpdateTableByMap(tabeleStruct interface{}, changeMap map[string]interface{}
return nil
}
func UpdateTableByMapWithOrmer(o orm.Ormer, tabeleStruct interface{}, changeMap map[string]interface{}) error {
if reflect.TypeOf(tabeleStruct).Kind() != reflect.Ptr {
err := errors.New("UpdateTableByMap: tableStruct must ptr")
log.Error(err)
return err
}
if len(changeMap) < 1 {
log.Info("changeMap is nil")
return nil
}
changeColumn := make([]string, 0, len(changeMap))
for i, v := range changeMap {
changeColumn = append(changeColumn, i)
if err := SetStructValueByType(tabeleStruct, i, v); err != nil {
log.Error(err, i, v)
return err
}
}
num, err := o.Update(tabeleStruct, changeColumn...)
if err != nil {
log.Error(err)
return err
}
log.Info(fmt.Sprintf("UpdateTableByMap: table:%s effect records:%d column:%v", GetTableName(tabeleStruct), num, changeColumn))
return nil
}
// 通过反射调用结构对应的TableName函数,达到返回表名的目的
func GetTableName(tableStruct interface{}) string {
m := reflect.ValueOf(tableStruct).MethodByName("TableName")
... ...
... ... @@ -13,5 +13,5 @@ type SmsInfo struct {
ErrorCount int `json:"error_count"`
LastTime int64 `json:"last_time"`
CreateTime int64 `json:"create_time"`
//Checked int `json:"checked"` //0:未校验证 1:已校验
Checked int `json:"checked"` //0:未校验证 1:已校验
}
... ...
... ... @@ -6,8 +6,10 @@ const (
)
const (
MethodLogin = "/auth/login"
MethodGetUser = "/users/"
MethodLogin = "/auth/login"
MethodGetUser = "/users/"
MethodPutUser = "/users/"
MethodUserExists = "/users/isExist"
)
/*UCenterLogin */
... ... @@ -44,10 +46,10 @@ type UCenterGetUserResponse struct {
/*修改用户信息 UCenterPutUser */
type UCenterPutUserRequest struct {
Phone string `json:"phone"`
PassWord string `json:"password"`
NickName string `json:"nickname"`
Avatar string `json:"avatar"`
Phone string `json:"phone,omitempty"`
PassWord string `json:"password,omitempty"`
NickName string `json:"nickname,omitempty"`
Avatar string `json:"avatar,omitempty"`
}
type UCenterPutUserResponse struct {
}
... ...
... ... @@ -10,6 +10,7 @@ import (
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"io/ioutil"
"net/http"
"opp/protocol"
"time"
)
... ... @@ -22,16 +23,19 @@ func RequestUserCenter(method string, httpMethod, request interface{}, rsponse i
appKey = beego.AppConfig.String("user_center_app_key")
salt = beego.AppConfig.String("user_center_salt")
httpReq *httplib.BeegoHTTPRequest
url = beego.AppConfig.String("user_center_url") + method
)
if httpMethod == http.MethodGet {
httpReq = httplib.Get(beego.AppConfig.String("user_center_url") + method)
httpReq = httplib.Get(url)
} else if httpMethod == http.MethodPost {
httpReq = httplib.Post(beego.AppConfig.String("user_center_url") + method)
httpReq = httplib.Post(url)
} else if httpMethod == http.MethodPut {
httpReq = httplib.Put(beego.AppConfig.String("user_center_url") + method)
httpReq = httplib.Put(url)
//httpReq.Debug(true)
}
httpReq.JSONBody(request)
httpReq.Header("Content-Type", "application/json")
httpReq.Header("appKey", appKey)
httpReq.Header("curTime", curTime)
httpReq.Header("checkSum", getUserCenterCheckSum(curTime, "", beego.AppConfig.String("user_center_app_secret"), salt))
... ... @@ -46,7 +50,7 @@ func RequestUserCenter(method string, httpMethod, request interface{}, rsponse i
return
}
if err = json.Unmarshal(data, rsponse); err != nil {
log.Debug(method, "response:", string(data))
log.Debug(url, "response:", string(data))
log.Error(err)
return
}
... ... @@ -59,3 +63,58 @@ func getUserCenterCheckSum(curTime, nonce, appKey, salt string) string {
sum := sha1.Sum([]byte(fmt.Sprintf("%s%s%s%s", curTime, nonce, appKey, salt)))
return hex.EncodeToString(sum)
}
//修改统一用户中心 用户信息
func ChangeUcenterUserInfo(uid int64, phone string, user *protocol.UCenterPutUserRequest) (err error) {
var message protocol.Message
log.Info(fmt.Sprintf("统一用户中心-修改密码 simnum:%v ucenter_id:%v user:%v", phone, uid, user))
//修改密码
if _, err = RequestUserCenter(fmt.Sprintf("%v%v", protocol.MethodPutUser, uid), http.MethodPut, user, &message); err != nil {
log.Error(err)
return
}
if message.Errno != 0 {
err = protocol.NewErrWithMessage(1)
return
}
return
}
func UcenterIsUserExists(uid int64, phone string) (err error) {
type user struct {
Phone string `json:"phone"`
}
u := &user{
Phone: phone,
}
var message *protocol.Message
if _, err = RequestUserCenter(protocol.MethodUserExists, http.MethodPost, u, &message); err != nil {
log.Error(err)
return
}
log.Info(fmt.Sprintf("统一用户中心-检查用户 ucenter_id:%v phone:%v code:%v msg:%v", uid, phone, message.Errno, message.Errmsg))
if message.Errno == -1 {
//err = protocol.NewErrWithMessage(1)
return
}
err = protocol.NewErrWithMessage(1, fmt.Errorf("ucenter.user.exitst ;code:%v msg:%v", message.Errno, message.Errmsg))
return
}
func CheckUcenterResponse(message *protocol.Message) (err error) {
switch message.Errno {
case -1:
err = protocol.NewErrWithMessage(2002, err) //账号不存在
return
case 0:
return
case 2002:
err = protocol.NewErrWithMessage(2002, err) //账号不存在
return
default:
err = fmt.Errorf("error_no:%v msg:%v", message.Errno, message.Errmsg)
break
}
return
}
... ...
package agg
import (
"log"
"opp/tests"
"testing"
)
func init() {
tests.Init()
}
func Test_UcenterIsUserExists(t *testing.T) {
if err := UcenterIsUserExists(3507839547244544, "18860183051"); err == nil {
log.Fatal(err)
}
}
... ...
... ... @@ -376,9 +376,14 @@ func CheckSmsCode(phone, code, sendType string) (result bool, err error) {
err = protocol.NewErrWithMessage(1009)
goto Fail
}
if smsInfo.Code == code {
if code == "" && smsInfo.Checked == 1 {
result = true
return
}
if smsInfo.Code == code {
result = true
smsInfo.Checked = 1
goto Success
} else {
log.Error("smscode not equal:", smsInfo.Code, code)
err = protocol.NewErrWithMessage(1012)
... ... @@ -395,6 +400,16 @@ Fail:
return
}
}
Success:
{
if e := redis.Hset(sendType, phone, common.AssertJson(smsInfo), -1); e != nil {
log.Error(e)
if err == nil {
err = e
}
return
}
}
return
}
... ...
... ... @@ -2,6 +2,7 @@ package user
import (
"fmt"
"github.com/astaxie/beego/orm"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"net/http"
"opp/internal/repository"
... ... @@ -41,7 +42,6 @@ func ChangePhone(header *protocol.RequestHeader, request *protocol.ChangePhoneRe
user *models.User
result bool
)
//rsp =&protocol.ChangePhoneResponse{}
if user, err = repository.User.GetUsersById(header.Uid); err != nil {
log.Error(err)
return
... ... @@ -50,39 +50,83 @@ func ChangePhone(header *protocol.RequestHeader, request *protocol.ChangePhoneRe
log.Error(err)
return
}
//TODO:跟用户中心交换数据
if _, err = repository.User.GetUsersByMobile(request.Phone); err == nil {
//判断库里面是否已经有这个手机号的用户
//if _, err = repository.User.GetUsersByMobile(request.Phone); err == nil {
// err = protocol.NewErrWithMessage(2029)
// return
//}
if !result {
err = protocol.NewErrWithMessage(1012)
return
}
//判断远程库是否已经有这个手机号
if err = agg.UcenterIsUserExists(user.UserCenterId, request.Phone); err != nil {
err = protocol.NewErrWithMessage(2029)
return
}
if !result {
err = protocol.NewErrWithMessage(1012)
o := orm.NewOrm()
o.Begin()
if err = utils.UpdateTableByMapWithOrmer(o, &models.User{Id: user.Id}, map[string]interface{}{"Phone": request.Phone}); err != nil {
log.Error(err)
o.Rollback()
return
}
if err = agg.ChangeUcenterUserInfo(user.UserCenterId, user.Phone, &protocol.UCenterPutUserRequest{Phone: request.Phone}); err != nil {
log.Error(err)
o.Rollback()
return
}
err = utils.UpdateTableByMap(&models.User{Id: user.Id}, map[string]interface{}{"Phone": request.Phone})
o.Commit()
return
}
//重置密码
func ResetPassword(header *protocol.RequestHeader, request *protocol.ResetPasswordRequest) (rsp *protocol.ResetPasswordResponse, err error) {
var (
user *models.User
user *models.User
message *protocol.Message
getUserRequest *protocol.UCenterGetUserRequest = &protocol.UCenterGetUserRequest{}
getUserResponse *protocol.UCenterGetUserResponse
result bool
)
//rsp =&protocol.ResetPasswordResponse{}
if len(request.NewPwd) < 6 {
err = protocol.NewErrWithMessage(2027)
return
}
if user, err = repository.User.GetUsersById(header.Uid); err != nil {
log.Error(err)
return
}
if result, err = auth.CheckSmsCode(user.Phone, "", protocol.SmsCode); err != nil {
log.Error(err)
return
}
if !result {
err = protocol.NewErrWithMessage(1012)
return
}
//从用户中心获取用户信息
if _, err = agg.RequestUserCenter(fmt.Sprintf("%v%v", protocol.MethodGetUser, user.UserCenterId), http.MethodGet, getUserRequest, &message); err != nil {
log.Error(err)
return
}
if message.Errno == 0 && message.Errmsg == "ok" {
if err = message.Unmarshal(&getUserResponse); err != nil {
log.Error(err)
return
}
}
if getUserResponse.Id != user.UserCenterId {
err = protocol.NewErrWithMessage(1)
log.Error(fmt.Sprintf("ucenter.id not equal input:%v want:%v", user.UserCenterId, getUserResponse.Id))
return
}
//TODO:未验证 校验码
if !strings.EqualFold(request.NewPwd, request.ConfirmPwd) {
err = protocol.NewErrWithMessage(2026)
return
}
if len(request.NewPwd) < 6 {
err = protocol.NewErrWithMessage(2027)
return
}
err = utils.UpdateTableByMap(&models.User{Id: user.Id}, map[string]interface{}{"Passwd": request.NewPwd})
err = agg.ChangeUcenterUserInfo(user.UserCenterId, user.Phone, &protocol.UCenterPutUserRequest{PassWord: request.NewPwd})
return
}
... ... @@ -92,7 +136,6 @@ func ChangePassword(header *protocol.RequestHeader, request *protocol.ChangePass
user *models.User
loginResponse *protocol.UserCenterLoginResponse
)
//rsp =&protocol.ChangePasswordResponse{}
if !strings.EqualFold(request.NewPwd, request.ConfirmPwd) {
err = protocol.NewErrWithMessage(2026)
return
... ... @@ -101,18 +144,10 @@ func ChangePassword(header *protocol.RequestHeader, request *protocol.ChangePass
err = protocol.NewErrWithMessage(2027)
return
}
//old
if user, err = repository.User.GetUsersById(header.Uid); err != nil {
log.Error(err)
return
}
//if !strings.EqualFold(request.OldPwd, user.Passwd) {
// err = protocol.NewErrWithMessage(2028)
// return
//}
//new
var message protocol.Message
if _, err = agg.RequestUserCenter(protocol.MethodLogin, http.MethodPost, &protocol.UCenterLoginRequest{
PassWord: request.OldPwd,
... ... @@ -131,19 +166,18 @@ func ChangePassword(header *protocol.RequestHeader, request *protocol.ChangePass
err = protocol.NewErrWithMessage(1)
return
}
log.Debug(fmt.Sprintf("修改密码 simnum:%v ucenter_id:%v pwd:%v", user.Phone, user.UserCenterId, request.NewPwd))
//修改密码
if _, err = agg.RequestUserCenter(fmt.Sprintf("%v%v", protocol.MethodGetUser, loginResponse.Id), http.MethodPut, &protocol.UCenterPutUserRequest{
PassWord: request.NewPwd,
}, &message); err != nil {
log.Error(err)
return
err = agg.ChangeUcenterUserInfo(user.UserCenterId, user.Phone, &protocol.UCenterPutUserRequest{PassWord: request.NewPwd})
} else {
if message.Errno == -1 {
err = protocol.NewErrWithMessage(2028)
}
log.Debug(fmt.Sprintf("修改密码 simnum:%v ucenter_id:%v pwd:%v", user.Phone, user.UserCenterId), message.Errno, message.Errmsg)
return
}
//err = utils.UpdateTableByMap(&models.User{Id: user.Id}, map[string]interface{}{"Passwd": request.NewPwd})
return
}
//用户公司
func UserCompanys(header *protocol.RequestHeader, request *protocol.UserCompanysRequest) (rsp *protocol.UserCompanysResponse, err error) {
var ()
rsp = &protocol.UserCompanysResponse{}
... ...