作者 yangfu

用户中心修改

@@ -38,6 +38,33 @@ func UpdateTableByMap(tabeleStruct interface{}, changeMap map[string]interface{} @@ -38,6 +38,33 @@ func UpdateTableByMap(tabeleStruct interface{}, changeMap map[string]interface{}
38 return nil 38 return nil
39 } 39 }
40 40
  41 +func UpdateTableByMapWithOrmer(o orm.Ormer, tabeleStruct interface{}, changeMap map[string]interface{}) error {
  42 + if reflect.TypeOf(tabeleStruct).Kind() != reflect.Ptr {
  43 + err := errors.New("UpdateTableByMap: tableStruct must ptr")
  44 + log.Error(err)
  45 + return err
  46 + }
  47 + if len(changeMap) < 1 {
  48 + log.Info("changeMap is nil")
  49 + return nil
  50 + }
  51 + changeColumn := make([]string, 0, len(changeMap))
  52 + for i, v := range changeMap {
  53 + changeColumn = append(changeColumn, i)
  54 + if err := SetStructValueByType(tabeleStruct, i, v); err != nil {
  55 + log.Error(err, i, v)
  56 + return err
  57 + }
  58 + }
  59 + num, err := o.Update(tabeleStruct, changeColumn...)
  60 + if err != nil {
  61 + log.Error(err)
  62 + return err
  63 + }
  64 + log.Info(fmt.Sprintf("UpdateTableByMap: table:%s effect records:%d column:%v", GetTableName(tabeleStruct), num, changeColumn))
  65 + return nil
  66 +}
  67 +
41 // 通过反射调用结构对应的TableName函数,达到返回表名的目的 68 // 通过反射调用结构对应的TableName函数,达到返回表名的目的
42 func GetTableName(tableStruct interface{}) string { 69 func GetTableName(tableStruct interface{}) string {
43 m := reflect.ValueOf(tableStruct).MethodByName("TableName") 70 m := reflect.ValueOf(tableStruct).MethodByName("TableName")
@@ -13,5 +13,5 @@ type SmsInfo struct { @@ -13,5 +13,5 @@ type SmsInfo struct {
13 ErrorCount int `json:"error_count"` 13 ErrorCount int `json:"error_count"`
14 LastTime int64 `json:"last_time"` 14 LastTime int64 `json:"last_time"`
15 CreateTime int64 `json:"create_time"` 15 CreateTime int64 `json:"create_time"`
16 - //Checked int `json:"checked"` //0:未校验证 1:已校验 16 + Checked int `json:"checked"` //0:未校验证 1:已校验
17 } 17 }
@@ -6,8 +6,10 @@ const ( @@ -6,8 +6,10 @@ const (
6 ) 6 )
7 7
8 const ( 8 const (
9 - MethodLogin = "/auth/login"  
10 - MethodGetUser = "/users/" 9 + MethodLogin = "/auth/login"
  10 + MethodGetUser = "/users/"
  11 + MethodPutUser = "/users/"
  12 + MethodUserExists = "/users/isExist"
11 ) 13 )
12 14
13 /*UCenterLogin */ 15 /*UCenterLogin */
@@ -44,10 +46,10 @@ type UCenterGetUserResponse struct { @@ -44,10 +46,10 @@ type UCenterGetUserResponse struct {
44 46
45 /*修改用户信息 UCenterPutUser */ 47 /*修改用户信息 UCenterPutUser */
46 type UCenterPutUserRequest struct { 48 type UCenterPutUserRequest struct {
47 - Phone string `json:"phone"`  
48 - PassWord string `json:"password"`  
49 - NickName string `json:"nickname"`  
50 - Avatar string `json:"avatar"` 49 + Phone string `json:"phone,omitempty"`
  50 + PassWord string `json:"password,omitempty"`
  51 + NickName string `json:"nickname,omitempty"`
  52 + Avatar string `json:"avatar,omitempty"`
51 } 53 }
52 type UCenterPutUserResponse struct { 54 type UCenterPutUserResponse struct {
53 } 55 }
@@ -10,6 +10,7 @@ import ( @@ -10,6 +10,7 @@ import (
10 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" 10 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
11 "io/ioutil" 11 "io/ioutil"
12 "net/http" 12 "net/http"
  13 + "opp/protocol"
13 "time" 14 "time"
14 ) 15 )
15 16
@@ -22,16 +23,19 @@ func RequestUserCenter(method string, httpMethod, request interface{}, rsponse i @@ -22,16 +23,19 @@ func RequestUserCenter(method string, httpMethod, request interface{}, rsponse i
22 appKey = beego.AppConfig.String("user_center_app_key") 23 appKey = beego.AppConfig.String("user_center_app_key")
23 salt = beego.AppConfig.String("user_center_salt") 24 salt = beego.AppConfig.String("user_center_salt")
24 httpReq *httplib.BeegoHTTPRequest 25 httpReq *httplib.BeegoHTTPRequest
  26 + url = beego.AppConfig.String("user_center_url") + method
25 ) 27 )
26 28
27 if httpMethod == http.MethodGet { 29 if httpMethod == http.MethodGet {
28 - httpReq = httplib.Get(beego.AppConfig.String("user_center_url") + method) 30 + httpReq = httplib.Get(url)
29 } else if httpMethod == http.MethodPost { 31 } else if httpMethod == http.MethodPost {
30 - httpReq = httplib.Post(beego.AppConfig.String("user_center_url") + method) 32 + httpReq = httplib.Post(url)
31 } else if httpMethod == http.MethodPut { 33 } else if httpMethod == http.MethodPut {
32 - httpReq = httplib.Put(beego.AppConfig.String("user_center_url") + method) 34 + httpReq = httplib.Put(url)
  35 + //httpReq.Debug(true)
33 } 36 }
34 httpReq.JSONBody(request) 37 httpReq.JSONBody(request)
  38 + httpReq.Header("Content-Type", "application/json")
35 httpReq.Header("appKey", appKey) 39 httpReq.Header("appKey", appKey)
36 httpReq.Header("curTime", curTime) 40 httpReq.Header("curTime", curTime)
37 httpReq.Header("checkSum", getUserCenterCheckSum(curTime, "", beego.AppConfig.String("user_center_app_secret"), salt)) 41 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 @@ -46,7 +50,7 @@ func RequestUserCenter(method string, httpMethod, request interface{}, rsponse i
46 return 50 return
47 } 51 }
48 if err = json.Unmarshal(data, rsponse); err != nil { 52 if err = json.Unmarshal(data, rsponse); err != nil {
49 - log.Debug(method, "response:", string(data)) 53 + log.Debug(url, "response:", string(data))
50 log.Error(err) 54 log.Error(err)
51 return 55 return
52 } 56 }
@@ -59,3 +63,58 @@ func getUserCenterCheckSum(curTime, nonce, appKey, salt string) string { @@ -59,3 +63,58 @@ func getUserCenterCheckSum(curTime, nonce, appKey, salt string) string {
59 sum := sha1.Sum([]byte(fmt.Sprintf("%s%s%s%s", curTime, nonce, appKey, salt))) 63 sum := sha1.Sum([]byte(fmt.Sprintf("%s%s%s%s", curTime, nonce, appKey, salt)))
60 return hex.EncodeToString(sum) 64 return hex.EncodeToString(sum)
61 } 65 }
  66 +
  67 +//修改统一用户中心 用户信息
  68 +func ChangeUcenterUserInfo(uid int64, phone string, user *protocol.UCenterPutUserRequest) (err error) {
  69 + var message protocol.Message
  70 + log.Info(fmt.Sprintf("统一用户中心-修改密码 simnum:%v ucenter_id:%v user:%v", phone, uid, user))
  71 + //修改密码
  72 + if _, err = RequestUserCenter(fmt.Sprintf("%v%v", protocol.MethodPutUser, uid), http.MethodPut, user, &message); err != nil {
  73 + log.Error(err)
  74 + return
  75 + }
  76 + if message.Errno != 0 {
  77 + err = protocol.NewErrWithMessage(1)
  78 + return
  79 + }
  80 + return
  81 +}
  82 +
  83 +func UcenterIsUserExists(uid int64, phone string) (err error) {
  84 + type user struct {
  85 + Phone string `json:"phone"`
  86 + }
  87 +
  88 + u := &user{
  89 + Phone: phone,
  90 + }
  91 + var message *protocol.Message
  92 + if _, err = RequestUserCenter(protocol.MethodUserExists, http.MethodPost, u, &message); err != nil {
  93 + log.Error(err)
  94 + return
  95 + }
  96 + log.Info(fmt.Sprintf("统一用户中心-检查用户 ucenter_id:%v phone:%v code:%v msg:%v", uid, phone, message.Errno, message.Errmsg))
  97 + if message.Errno == -1 {
  98 + //err = protocol.NewErrWithMessage(1)
  99 + return
  100 + }
  101 + err = protocol.NewErrWithMessage(1, fmt.Errorf("ucenter.user.exitst ;code:%v msg:%v", message.Errno, message.Errmsg))
  102 + return
  103 +}
  104 +
  105 +func CheckUcenterResponse(message *protocol.Message) (err error) {
  106 + switch message.Errno {
  107 + case -1:
  108 + err = protocol.NewErrWithMessage(2002, err) //账号不存在
  109 + return
  110 + case 0:
  111 + return
  112 + case 2002:
  113 + err = protocol.NewErrWithMessage(2002, err) //账号不存在
  114 + return
  115 + default:
  116 + err = fmt.Errorf("error_no:%v msg:%v", message.Errno, message.Errmsg)
  117 + break
  118 + }
  119 + return
  120 +}
  1 +package agg
  2 +
  3 +import (
  4 + "log"
  5 + "opp/tests"
  6 + "testing"
  7 +)
  8 +
  9 +func init() {
  10 + tests.Init()
  11 +}
  12 +
  13 +func Test_UcenterIsUserExists(t *testing.T) {
  14 + if err := UcenterIsUserExists(3507839547244544, "18860183051"); err == nil {
  15 + log.Fatal(err)
  16 + }
  17 +}
@@ -376,9 +376,14 @@ func CheckSmsCode(phone, code, sendType string) (result bool, err error) { @@ -376,9 +376,14 @@ func CheckSmsCode(phone, code, sendType string) (result bool, err error) {
376 err = protocol.NewErrWithMessage(1009) 376 err = protocol.NewErrWithMessage(1009)
377 goto Fail 377 goto Fail
378 } 378 }
379 - if smsInfo.Code == code { 379 + if code == "" && smsInfo.Checked == 1 {
380 result = true 380 result = true
381 return 381 return
  382 + }
  383 + if smsInfo.Code == code {
  384 + result = true
  385 + smsInfo.Checked = 1
  386 + goto Success
382 } else { 387 } else {
383 log.Error("smscode not equal:", smsInfo.Code, code) 388 log.Error("smscode not equal:", smsInfo.Code, code)
384 err = protocol.NewErrWithMessage(1012) 389 err = protocol.NewErrWithMessage(1012)
@@ -395,6 +400,16 @@ Fail: @@ -395,6 +400,16 @@ Fail:
395 return 400 return
396 } 401 }
397 } 402 }
  403 +Success:
  404 + {
  405 + if e := redis.Hset(sendType, phone, common.AssertJson(smsInfo), -1); e != nil {
  406 + log.Error(e)
  407 + if err == nil {
  408 + err = e
  409 + }
  410 + return
  411 + }
  412 + }
398 return 413 return
399 } 414 }
400 415
@@ -2,6 +2,7 @@ package user @@ -2,6 +2,7 @@ package user
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "github.com/astaxie/beego/orm"
5 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" 6 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
6 "net/http" 7 "net/http"
7 "opp/internal/repository" 8 "opp/internal/repository"
@@ -41,7 +42,6 @@ func ChangePhone(header *protocol.RequestHeader, request *protocol.ChangePhoneRe @@ -41,7 +42,6 @@ func ChangePhone(header *protocol.RequestHeader, request *protocol.ChangePhoneRe
41 user *models.User 42 user *models.User
42 result bool 43 result bool
43 ) 44 )
44 - //rsp =&protocol.ChangePhoneResponse{}  
45 if user, err = repository.User.GetUsersById(header.Uid); err != nil { 45 if user, err = repository.User.GetUsersById(header.Uid); err != nil {
46 log.Error(err) 46 log.Error(err)
47 return 47 return
@@ -50,39 +50,83 @@ func ChangePhone(header *protocol.RequestHeader, request *protocol.ChangePhoneRe @@ -50,39 +50,83 @@ func ChangePhone(header *protocol.RequestHeader, request *protocol.ChangePhoneRe
50 log.Error(err) 50 log.Error(err)
51 return 51 return
52 } 52 }
53 - //TODO:跟用户中心交换数据  
54 - if _, err = repository.User.GetUsersByMobile(request.Phone); err == nil { 53 + //判断库里面是否已经有这个手机号的用户
  54 + //if _, err = repository.User.GetUsersByMobile(request.Phone); err == nil {
  55 + // err = protocol.NewErrWithMessage(2029)
  56 + // return
  57 + //}
  58 + if !result {
  59 + err = protocol.NewErrWithMessage(1012)
  60 + return
  61 + }
  62 + //判断远程库是否已经有这个手机号
  63 + if err = agg.UcenterIsUserExists(user.UserCenterId, request.Phone); err != nil {
55 err = protocol.NewErrWithMessage(2029) 64 err = protocol.NewErrWithMessage(2029)
56 return 65 return
57 } 66 }
58 - if !result {  
59 - err = protocol.NewErrWithMessage(1012) 67 + o := orm.NewOrm()
  68 + o.Begin()
  69 + if err = utils.UpdateTableByMapWithOrmer(o, &models.User{Id: user.Id}, map[string]interface{}{"Phone": request.Phone}); err != nil {
  70 + log.Error(err)
  71 + o.Rollback()
  72 + return
  73 + }
  74 + if err = agg.ChangeUcenterUserInfo(user.UserCenterId, user.Phone, &protocol.UCenterPutUserRequest{Phone: request.Phone}); err != nil {
  75 + log.Error(err)
  76 + o.Rollback()
60 return 77 return
61 } 78 }
62 - err = utils.UpdateTableByMap(&models.User{Id: user.Id}, map[string]interface{}{"Phone": request.Phone}) 79 + o.Commit()
63 return 80 return
64 } 81 }
65 82
66 //重置密码 83 //重置密码
67 func ResetPassword(header *protocol.RequestHeader, request *protocol.ResetPasswordRequest) (rsp *protocol.ResetPasswordResponse, err error) { 84 func ResetPassword(header *protocol.RequestHeader, request *protocol.ResetPasswordRequest) (rsp *protocol.ResetPasswordResponse, err error) {
68 var ( 85 var (
69 - user *models.User 86 + user *models.User
  87 + message *protocol.Message
  88 + getUserRequest *protocol.UCenterGetUserRequest = &protocol.UCenterGetUserRequest{}
  89 + getUserResponse *protocol.UCenterGetUserResponse
  90 + result bool
70 ) 91 )
71 - //rsp =&protocol.ResetPasswordResponse{} 92 + if len(request.NewPwd) < 6 {
  93 + err = protocol.NewErrWithMessage(2027)
  94 + return
  95 + }
72 if user, err = repository.User.GetUsersById(header.Uid); err != nil { 96 if user, err = repository.User.GetUsersById(header.Uid); err != nil {
73 log.Error(err) 97 log.Error(err)
74 return 98 return
75 } 99 }
  100 + if result, err = auth.CheckSmsCode(user.Phone, "", protocol.SmsCode); err != nil {
  101 + log.Error(err)
  102 + return
  103 + }
  104 + if !result {
  105 + err = protocol.NewErrWithMessage(1012)
  106 + return
  107 + }
  108 + //从用户中心获取用户信息
  109 + if _, err = agg.RequestUserCenter(fmt.Sprintf("%v%v", protocol.MethodGetUser, user.UserCenterId), http.MethodGet, getUserRequest, &message); err != nil {
  110 + log.Error(err)
  111 + return
  112 + }
  113 + if message.Errno == 0 && message.Errmsg == "ok" {
  114 + if err = message.Unmarshal(&getUserResponse); err != nil {
  115 + log.Error(err)
  116 + return
  117 + }
  118 + }
  119 + if getUserResponse.Id != user.UserCenterId {
  120 + err = protocol.NewErrWithMessage(1)
  121 + log.Error(fmt.Sprintf("ucenter.id not equal input:%v want:%v", user.UserCenterId, getUserResponse.Id))
  122 + return
  123 + }
76 //TODO:未验证 校验码 124 //TODO:未验证 校验码
77 if !strings.EqualFold(request.NewPwd, request.ConfirmPwd) { 125 if !strings.EqualFold(request.NewPwd, request.ConfirmPwd) {
78 err = protocol.NewErrWithMessage(2026) 126 err = protocol.NewErrWithMessage(2026)
79 return 127 return
80 } 128 }
81 - if len(request.NewPwd) < 6 {  
82 - err = protocol.NewErrWithMessage(2027)  
83 - return  
84 - }  
85 - err = utils.UpdateTableByMap(&models.User{Id: user.Id}, map[string]interface{}{"Passwd": request.NewPwd}) 129 + err = agg.ChangeUcenterUserInfo(user.UserCenterId, user.Phone, &protocol.UCenterPutUserRequest{PassWord: request.NewPwd})
86 return 130 return
87 } 131 }
88 132
@@ -92,7 +136,6 @@ func ChangePassword(header *protocol.RequestHeader, request *protocol.ChangePass @@ -92,7 +136,6 @@ func ChangePassword(header *protocol.RequestHeader, request *protocol.ChangePass
92 user *models.User 136 user *models.User
93 loginResponse *protocol.UserCenterLoginResponse 137 loginResponse *protocol.UserCenterLoginResponse
94 ) 138 )
95 - //rsp =&protocol.ChangePasswordResponse{}  
96 if !strings.EqualFold(request.NewPwd, request.ConfirmPwd) { 139 if !strings.EqualFold(request.NewPwd, request.ConfirmPwd) {
97 err = protocol.NewErrWithMessage(2026) 140 err = protocol.NewErrWithMessage(2026)
98 return 141 return
@@ -101,18 +144,10 @@ func ChangePassword(header *protocol.RequestHeader, request *protocol.ChangePass @@ -101,18 +144,10 @@ func ChangePassword(header *protocol.RequestHeader, request *protocol.ChangePass
101 err = protocol.NewErrWithMessage(2027) 144 err = protocol.NewErrWithMessage(2027)
102 return 145 return
103 } 146 }
104 -  
105 - //old  
106 if user, err = repository.User.GetUsersById(header.Uid); err != nil { 147 if user, err = repository.User.GetUsersById(header.Uid); err != nil {
107 log.Error(err) 148 log.Error(err)
108 return 149 return
109 } 150 }
110 - //if !strings.EqualFold(request.OldPwd, user.Passwd) {  
111 - // err = protocol.NewErrWithMessage(2028)  
112 - // return  
113 - //}  
114 -  
115 - //new  
116 var message protocol.Message 151 var message protocol.Message
117 if _, err = agg.RequestUserCenter(protocol.MethodLogin, http.MethodPost, &protocol.UCenterLoginRequest{ 152 if _, err = agg.RequestUserCenter(protocol.MethodLogin, http.MethodPost, &protocol.UCenterLoginRequest{
118 PassWord: request.OldPwd, 153 PassWord: request.OldPwd,
@@ -131,19 +166,18 @@ func ChangePassword(header *protocol.RequestHeader, request *protocol.ChangePass @@ -131,19 +166,18 @@ func ChangePassword(header *protocol.RequestHeader, request *protocol.ChangePass
131 err = protocol.NewErrWithMessage(1) 166 err = protocol.NewErrWithMessage(1)
132 return 167 return
133 } 168 }
134 - log.Debug(fmt.Sprintf("修改密码 simnum:%v ucenter_id:%v pwd:%v", user.Phone, user.UserCenterId, request.NewPwd))  
135 - //修改密码  
136 - if _, err = agg.RequestUserCenter(fmt.Sprintf("%v%v", protocol.MethodGetUser, loginResponse.Id), http.MethodPut, &protocol.UCenterPutUserRequest{  
137 - PassWord: request.NewPwd,  
138 - }, &message); err != nil {  
139 - log.Error(err)  
140 - return 169 + err = agg.ChangeUcenterUserInfo(user.UserCenterId, user.Phone, &protocol.UCenterPutUserRequest{PassWord: request.NewPwd})
  170 + } else {
  171 + if message.Errno == -1 {
  172 + err = protocol.NewErrWithMessage(2028)
141 } 173 }
  174 + log.Debug(fmt.Sprintf("修改密码 simnum:%v ucenter_id:%v pwd:%v", user.Phone, user.UserCenterId), message.Errno, message.Errmsg)
  175 + return
142 } 176 }
143 - //err = utils.UpdateTableByMap(&models.User{Id: user.Id}, map[string]interface{}{"Passwd": request.NewPwd})  
144 return 177 return
145 } 178 }
146 179
  180 +//用户公司
147 func UserCompanys(header *protocol.RequestHeader, request *protocol.UserCompanysRequest) (rsp *protocol.UserCompanysResponse, err error) { 181 func UserCompanys(header *protocol.RequestHeader, request *protocol.UserCompanysRequest) (rsp *protocol.UserCompanysResponse, err error) {
148 var () 182 var ()
149 rsp = &protocol.UserCompanysResponse{} 183 rsp = &protocol.UserCompanysResponse{}