作者 yangfu

修改用户信息

### 接口完成进度
|功能|时间|接口路径|人员
|---|----|----|---
|修改手机号-校验验证码|2019.11.20|/v1/user/checkSmsCode|
|修改手机号|2019.11.20|/v1/user/changePhone|
|修改密码|2019.11.20|v1/user/changePassword|
|忘记密码|2019.11.20|v1/user/resetPassword|
\ No newline at end of file
... ...
appname = opportunity
httpport = 8080
runmode = "${RUN_MODE||prod}"
runmode = "${RUN_MODE||dev}"
#没设置的话 this.Ctx.Input.RequestBody 没有值
copyrequestbody = true
... ...
package v1
import (
"encoding/json"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"opp/controllers"
"opp/protocol"
"opp/services/user"
)
type UserController struct {
controllers.BaseController
}
//CheckSmsCode
// @router /checkSmsCode [post]
func (this *UserController) CheckSmsCode() {
var msg *protocol.ResponseMessage
defer func() {
this.Resp(msg)
}()
var request *protocol.CheckSmsCodeRequest
if err := json.Unmarshal(this.ByteBody, &request); err != nil {
log.Error(err)
msg = protocol.BadRequestParam(1)
return
}
if b, m := this.Valid(request); !b {
msg = m
return
}
header := controllers.GetRequestHeader(this.Ctx)
msg = protocol.NewReturnResponse(user.CheckSmsCode(header, request))
}
//ChangePhone
// @router /changePhone [post]
func (this *UserController) ChangePhone() {
var msg *protocol.ResponseMessage
defer func() {
this.Resp(msg)
}()
var request *protocol.ChangePhoneRequest
if err := json.Unmarshal(this.ByteBody, &request); err != nil {
log.Error(err)
msg = protocol.BadRequestParam(1)
return
}
if b, m := this.Valid(request); !b {
msg = m
return
}
header := controllers.GetRequestHeader(this.Ctx)
msg = protocol.NewReturnResponse(user.ChangePhone(header, request))
}
//ResetPassword
// @router /resetPassword [post]
func (this *UserController) ResetPassword() {
var msg *protocol.ResponseMessage
defer func() {
this.Resp(msg)
}()
var request *protocol.ResetPasswordRequest
if err := json.Unmarshal(this.ByteBody, &request); err != nil {
log.Error(err)
msg = protocol.BadRequestParam(1)
return
}
if b, m := this.Valid(request); !b {
msg = m
return
}
header := controllers.GetRequestHeader(this.Ctx)
msg = protocol.NewReturnResponse(user.ResetPassword(header, request))
}
//ChangePassword
// @router /changePassword [post]
func (this *UserController) ChangePassword() {
var msg *protocol.ResponseMessage
defer func() {
this.Resp(msg)
}()
var request *protocol.ChangePasswordRequest
if err := json.Unmarshal(this.ByteBody, &request); err != nil {
log.Error(err)
msg = protocol.BadRequestParam(1)
return
}
if b, m := this.Valid(request); !b {
msg = m
return
}
header := controllers.GetRequestHeader(this.Ctx)
msg = protocol.NewReturnResponse(user.ChangePassword(header, request))
}
... ...
package utils
import (
"errors"
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"reflect"
)
// 更新指定表的几个列
func UpdateTableByMap(tabeleStruct interface{}, changeMap map[string]interface{}) error {
if reflect.TypeOf(tabeleStruct).Kind() != reflect.Ptr {
err := errors.New("UpdateTableByMap: tableStruct must ptr")
beego.Error(err)
return err
}
if len(changeMap) < 1 {
beego.Info("changeMap is nil")
return nil
}
o := orm.NewOrm()
changeColumn := make([]string, 0, len(changeMap))
for i, v := range changeMap {
changeColumn = append(changeColumn, i)
if err := SetStructValueByType(tabeleStruct, i, v); err != nil {
beego.Error(err, i, v)
return err
}
}
num, err := o.Update(tabeleStruct, changeColumn...)
if err != nil {
beego.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")
if m.IsValid() && m.Kind() == reflect.Func {
re := m.Call(nil)
for _, v := range re {
if v.IsValid() {
return v.String()
}
}
}
return "unknown"
}
// 通用事物提交sql结构体
type SqlData struct {
Sql string
Param []interface{}
}
func ExecuteSqlByRoll(isCheck bool, sqlSlice ...*SqlData) bool {
o := orm.NewOrm()
var someError bool = false
o.Begin()
for i := range sqlSlice {
if sqlSlice[i].Sql == "" {
continue
}
log.Info("execute sql:", sqlSlice[i])
if ret, err := o.Raw(sqlSlice[i].Sql, sqlSlice[i].Param...).Exec(); err != nil {
log.Error(err)
someError = true
} else {
num, _ := ret.RowsAffected()
log.Debug("num:", num)
if isCheck && num < 1 {
someError = true
}
}
}
if someError {
log.Error("出错,回滚事物")
o.Rollback()
return false
} else {
log.Info("成功,提交事物")
o.Commit()
return true
}
return true
}
... ...
package utils
import (
"errors"
"reflect"
)
// 此函数将指定的结构体成员值更新到结构体中
func SetStructValueByType(s interface{}, columnType string, columnValue interface{}) error {
columnValueV := reflect.ValueOf(columnValue)
var setValue reflect.Value
var flag = false
v := reflect.ValueOf(s)
for i, n := 0, v.Elem().NumField(); i < n; i++ {
if v.Elem().Type().Field(i).Name == columnType {
setValue = v.Elem().Field(i)
flag = true
break
}
}
if !flag {
return errors.New("struct is not type:")
} else if !setValue.CanSet() {
return errors.New("setValue.CanSet is false")
} else if setValue.Kind() != columnValueV.Kind() {
return errors.New("struct field and value of type is error")
}
switch columnValueV.Kind() {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int, reflect.Int64:
setValue.SetInt(int64(columnValueV.Int()))
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
setValue.SetUint(uint64(columnValueV.Uint()))
case reflect.Float32, reflect.Float64:
setValue.SetFloat(float64(columnValueV.Float()))
case reflect.String:
setValue.SetString(columnValueV.String())
default:
return errors.New("columnValue err for:" + columnType)
}
return nil
}
... ...
... ... @@ -12,7 +12,6 @@ import (
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/websocket"
"opp/controllers"
"opp/internal/utils"
"opp/protocol"
_ "opp/routers"
"opp/services/im"
"time"
... ... @@ -65,7 +64,6 @@ func main() {
log.Info("app on stop!")
}()
beego.InsertFilter("/*", beego.BeforeRouter, controllers.FilterComm)
protocol.InitMessageCode()
log.Info("app on start!")
log.Info("Beego Run Mode:", beego.BConfig.RunMode)
... ...
... ... @@ -20,7 +20,7 @@ func (m ErrorMap) Search(code int) ErrorCode {
Errmsg: v,
}
}
return ErrorCode{}
return ErrorCode{Errno: code, Errmsg: "错误码未定义"}
}
//ErrorCode 统一错误结构
... ...
package protocol
var errmessge ErrorMap = map[int]string{
0: "",
1: "系统异常",
2: "参数错误",
101: "clientId或clientSecret无效",
113: "签名验证失败",
1009: "验证码已超时,登录失败",
... ... @@ -19,35 +21,3 @@ var errmessge ErrorMap = map[int]string{
4141: "accessToken过期或无效,需要进行重新获取令牌",
4142: "Uuid已存在,请求失败",
}
func InitMessageCode() {
// messages := []struct {
// Code int
// Msg string
// }{
// {101, "clientId或clientSecret无效"},
// {113, "签名验证失败"},
// {1009, "验证码已超时,登录失败"},
// {1011, "短信验证码次数超过限制,请稍后重试"},
// {1012, "验证码错误"},
// {2001, "请输入正确的手机号码"},
// {2002, "后台未配置账号信息,请联系管理员配置"},
// {2009, "上传的文件流为空"},
// {2020, "帐号不存在,请联系管理员"},
// {2021, "登录失败,手机号或密码错误"},
// {2025, "短信验证码验证失败"},
// {2026, "两次输入的密码不一致"},
// {4139, "authCode无效或过期"},
// {4140, "refreshToken过期,需要重新登录授权"},
// {4141, "accessToken过期或无效,需要进行重新获取令牌"},
// {4142, "Uuid已存在,请求失败"},
// }
// for i := range messages {
// mybeego.SetMessage(messages[i].Code, messages[i].Msg)
// }
////
}
... ...
... ... @@ -2,6 +2,7 @@ package protocol
//短信类型
const (
SmsCode = "sms_code"
SmsLoginCode = "sms_login_code"
SmsChangeMobile = "sms_change_mobile"
)
... ... @@ -12,4 +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:已校验
}
... ...
package protocol
/*修改手机号-验证旧手机验证码 */
type CheckSmsCodeRequest struct {
Captcha string `json:"captcha" valid:"Required"`
}
type CheckSmsCodeResponse struct {
}
/*修改手机号*/
type ChangePhoneRequest struct {
Phone string `json:"phone" valid:"Required"`
Captcha string `json:"captcha" valid:"Required"`
}
type ChangePhoneResponse struct {
}
/*ResetPassword */
type ResetPasswordRequest struct {
NewPwd string `json:"newPwd" valid:"Required"`
ConfirmPwd string `json:"confirmPwd" valid:"Required"`
}
type ResetPasswordResponse struct {
}
/*ChangePassword */
type ChangePasswordRequest struct {
NewPwd string `json:"newPwd" valid:"Required"`
ConfirmPwd string `json:"confirmPwd" valid:"Required"`
OldPwd string `json:"oldPwd" valid:"Required"`
}
type ChangePasswordResponse struct {
}
... ...
... ... @@ -9,66 +9,98 @@ func init() {
beego.GlobalControllerRouter["opp/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:AuthController"],
beego.ControllerComments{
Method: "AccessToken",
Router: `/accessToken`,
Method: "AccessToken",
Router: `/accessToken`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["opp/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:AuthController"],
beego.ControllerComments{
Method: "Login",
Router: `/login`,
Method: "Login",
Router: `/login`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["opp/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:AuthController"],
beego.ControllerComments{
Method: "RefreshToken",
Router: `/refreshToken`,
Method: "RefreshToken",
Router: `/refreshToken`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["opp/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:AuthController"],
beego.ControllerComments{
Method: "SmsCode",
Router: `/smsCode`,
Method: "SmsCode",
Router: `/smsCode`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["opp/controllers/v1:AuthController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:AuthController"],
beego.ControllerComments{
Method: "UpdateDevice",
Router: `/updateDevice`,
Method: "UpdateDevice",
Router: `/updateDevice`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["opp/controllers/v1:CommendController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:CommendController"],
beego.ControllerComments{
Method: "Company",
Router: `/company`,
Method: "Company",
Router: `/company`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["opp/controllers/v1:UploadController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:UploadController"],
beego.ControllerComments{
Method: "Image",
Router: `/image`,
Method: "Image",
Router: `/image`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["opp/controllers/v1:UploadController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:UploadController"],
beego.ControllerComments{
Method: "Voice",
Router: `/voice`,
Method: "Voice",
Router: `/voice`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["opp/controllers/v1:UserController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:UserController"],
beego.ControllerComments{
Method: "ChangePassword",
Router: `/changePassword`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["opp/controllers/v1:UserController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:UserController"],
beego.ControllerComments{
Method: "ChangePhone",
Router: `/changePhone`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["opp/controllers/v1:UserController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:UserController"],
beego.ControllerComments{
Method: "CheckSmsCode",
Router: `/checkSmsCode`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["opp/controllers/v1:UserController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:UserController"],
beego.ControllerComments{
Method: "ResetPassword",
Router: `/resetPassword`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
}
... ...
... ... @@ -16,6 +16,7 @@ func init() {
beego.NSNamespace("upload", beego.NSInclude(&v1.UploadController{})),
beego.NSNamespace("version", beego.NSInclude(&v1.VersionController{})),
beego.NSNamespace("commend", beego.NSInclude(&v1.CommendController{})),
beego.NSNamespace("user", beego.NSInclude(&v1.UserController{})),
)
beego.AddNamespace(nsV1)
beego.SetStaticPath("/file/ab", beego.AppConfig.String("source_path"))
... ...
... ... @@ -72,7 +72,7 @@ func (s *AuthService) Login(request *protocol.LoginRequest) (rsp *protocol.Login
}
break
case protocol.LoginTypeSmdcode:
if result, err = CheckSmsCode(request.Phone, request.Code, protocol.SmsLoginCode); result && err == nil {
if result, err = CheckSmsCode(request.Phone, request.Code, protocol.SmsCode); result && err == nil {
goto Success
} else {
return
... ... @@ -239,8 +239,9 @@ func (s *AuthService) SmsCode(request *protocol.SmsCodeRequest) (rsp *protocol.S
case protocol.SmsLoginCode:
case protocol.SmsChangeMobile:
default:
err = common.NewErrorWithMsg(2, "send_type error.")
return
request.SendType = protocol.SmsCode
//err = common.NewErrorWithMsg(2, "send_type error.")
//return
}
key = request.SendType
//check user phone exists
... ...
package user
//import
//(
// "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
// "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
// "opp/models"
//)
//
//func GetUserList(mobile string)*protocol.ResponseMessage{
// u,err:=models.GetUserByMobile(mobile)
// if err!=nil{
// log.Error(err)
// log.Error(mobile)
// return mybeego.NewErrMessage(1)
// }
// msg :=protocol.ReturnResponse(0)
// msg.Data = u
// return msg
//}
import (
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"opp/internal/repository"
"opp/internal/utils"
"opp/models"
"opp/protocol"
"opp/services/auth"
"strings"
)
//验证smscode
func CheckSmsCode(header *protocol.RequestHeader, request *protocol.CheckSmsCodeRequest) (rsp *protocol.CheckSmsCodeResponse, err error) {
var (
user *models.User
result bool
)
//rsp =&protocol.CheckSmsCodeResponse{}
if user, err = repository.User.GetUsersById(header.Uid); err != nil {
log.Error(err)
return
}
if result, err = auth.CheckSmsCode(user.Phone, request.Captcha, protocol.SmsCode); err != nil {
log.Error(err)
return
}
if !result {
err = protocol.NewErrWithMessage(1012)
}
return
}
//修改手机号
func ChangePhone(header *protocol.RequestHeader, request *protocol.ChangePhoneRequest) (rsp *protocol.ChangePhoneResponse, err error) {
var (
user *models.User
result bool
)
//rsp =&protocol.ChangePhoneResponse{}
if user, err = repository.User.GetUsersById(header.Uid); err != nil {
log.Error(err)
return
}
if result, err = auth.CheckSmsCode(user.Phone, request.Captcha, protocol.SmsCode); err != nil {
log.Error(err)
return
}
if !result {
err = protocol.NewErrWithMessage(1012)
}
err = utils.UpdateTableByMap(&models.User{Id: user.Id}, map[string]interface{}{"Phone": request.Phone})
return
}
//重置密码
func ResetPassword(header *protocol.RequestHeader, request *protocol.ResetPasswordRequest) (rsp *protocol.ResetPasswordResponse, err error) {
var (
user *models.User
)
//rsp =&protocol.ResetPasswordResponse{}
if user, err = repository.User.GetUsersById(header.Uid); err != nil {
log.Error(err)
return
}
//TODO:未验证 校验码
if !strings.EqualFold(request.NewPwd, request.ConfirmPwd) {
err = protocol.NewErrWithMessage(2026)
}
err = utils.UpdateTableByMap(&models.User{Id: user.Id}, map[string]interface{}{"Passwd": request.NewPwd})
return
}
//修改密码
func ChangePassword(header *protocol.RequestHeader, request *protocol.ChangePasswordRequest) (rsp *protocol.ChangePasswordResponse, err error) {
var (
user *models.User
)
//rsp =&protocol.ChangePasswordResponse{}
if user, err = repository.User.GetUsersById(header.Uid); err != nil {
log.Error(err)
return
}
if strings.EqualFold(request.OldPwd, user.Passwd) {
err = protocol.NewErrWithMessage(1)
}
if !strings.EqualFold(request.NewPwd, request.ConfirmPwd) {
err = protocol.NewErrWithMessage(2026)
}
err = utils.UpdateTableByMap(&models.User{Id: user.Id}, map[string]interface{}{"Passwd": request.NewPwd})
return
}
... ...
package tests
import (
"fmt"
"github.com/astaxie/beego"
_ "github.com/go-sql-driver/mysql"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/config"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/orm"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/redis"
"opp/protocol"
"os"
"path/filepath"
"runtime"
... ... @@ -34,11 +34,18 @@ func Init() {
log.Fatal(err)
panic(err)
}
dataSource := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?loc=Asia%%2FShanghai",
beego.AppConfig.String("mysql_user"),
beego.AppConfig.String("mysql_password"),
beego.AppConfig.String("mysql_host"),
beego.AppConfig.String("mysql_port"),
beego.AppConfig.String("mysql_db_name"),
)
orm.NewBeeormEngine(config.Mysql{
DataSource: beego.AppConfig.String("data_source"),
DataSource: dataSource,
MaxIdle: 100,
MaxOpen: 100,
})
protocol.InitMessageCode()
//protocol.InitMessageCode()
})
}
... ...