作者 yangfu

1.短信 权限认证修改

... ... @@ -7,7 +7,7 @@ import (
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/service_gateway"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/protocol"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
"strings"
"time"
)
... ... @@ -23,18 +23,21 @@ func Login(header *protocol.RequestHeader, request *protocol.LoginRequest) (rsp
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
switch request.Type {
switch request.GrantType {
case protocol.LoginByPassword:
if len(partnerInfo.Password) == 0 {
err = protocol.NewCustomMessage(1, "密码不能为空!")
return
}
if result = strings.EqualFold(partnerInfo.Password, request.Password); result {
if result = strings.EqualFold(partnerInfo.Password, request.Password); !result {
err = protocol.NewCustomMessage(1, "密码输入有误!")
return
}
break
case protocol.LoginBySmsCode:
if _, err = CheckSmsCode(request.Phone, request.Captcha); err != nil {
return
}
break
}
rsp.AuthCode, _ = utils.GenerateToken(partnerInfo.Id, protocol.AuthCodeExpire*time.Second)
... ... @@ -96,8 +99,8 @@ func RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshT
//验证短信验证码 T
func CheckSmsCode(phone, code string) (result bool, err error) {
sms := service_gateway.NewHttplibMmmSmsApiServiceGateway()
_, err = sms.CheckSmsCode(phone, code)
var data map[string]interface{}
data, err = sms.CheckSmsCode(phone, code)
if err != nil {
if msg, ok := data["msg"]; ok {
err = protocol.NewCustomMessage(1, msg.(string))
... ...
... ... @@ -25,19 +25,15 @@ func CreateAdminUserRepository(options map[string]interface{}) (domain.AdminUser
}
//CreateOrderRepository 订单
func CreateOrderRepository(options map[string]interface{}) (domain.OrderRepository, error) {
var transactionContext *transaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*transaction.TransactionContext)
}
func CreateOrderRepository(transactionContext *transaction.TransactionContext) (domain.OrderRepository, error) {
return repository.NewOrderRepository(transactionContext)
}
//CreateOrderRepository 订单
func CreateOrderPaymentRepository(options map[string]interface{}) (domain.OrderPaymentRepository, error) {
var transactionContext *transaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*transaction.TransactionContext)
}
func CreateOrderPaymentRepository(transactionContext *transaction.TransactionContext) (domain.OrderPaymentRepository, error) {
return repository.NewOrderPaymentRepository(transactionContext)
}
func CreatePartnerInfoRepositoryIn(transactionContext *transaction.TransactionContext) (domain.PartnerInfoRepository, error) {
return repository.NewPartnerInfoRepository(transactionContext)
}
... ...
package factory
import (
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
)
func CreateTransactionContext(options map[string]interface{}) (application.TransactionContext, error) {
func CreateTransactionContext(options map[string]interface{}) (*transaction.TransactionContext, error) {
return &transaction.TransactionContext{
PgDd: pg.DB,
}, nil
... ...
package order
import (
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
)
func Template(header *protocol.RequestHeader, request *protocol.OrderDetailRequest) (rsp *protocol.OrderDetailResponse, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
//OrderResponsitory,_ =factory.CreateOrderRepository(transactionContext)
)
if err = transactionContext.StartTransaction(); err != nil {
return nil, err
}
defer func() {
if err != nil {
transactionContext.RollbackTransaction()
}
}()
err = transactionContext.CommitTransaction()
return
}
func OrderDetail(header *protocol.RequestHeader, request *protocol.OrderDetailRequest) (rsp *protocol.OrderDetailResponse, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
OrderResponsitory, _ = factory.CreateOrderRepository(transactionContext)
PartnerInfoRepository, _ = factory.CreatePartnerInfoRepositoryIn(transactionContext)
order *domain.Order
partner *domain.PartnerInfo
)
if err = transactionContext.StartTransaction(); err != nil {
return nil, err
}
defer func() {
if err != nil {
transactionContext.RollbackTransaction()
}
}()
rsp = &protocol.OrderDetailResponse{}
if order, err = OrderResponsitory.FindOne(utils.ObjectJsonToMap(request)); err != nil {
return
}
if partner, err = PartnerInfoRepository.FindOne(map[string]interface{}{"partnerId": order.PartnerId}); err != nil {
return
}
rsp.Order = order
rsp.Partner = partner
err = transactionContext.CommitTransaction()
return
}
... ...
package user
import (
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/auth"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/command"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/query"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/service"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/protocol"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/services/auth"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
"strings"
)
... ... @@ -23,14 +23,16 @@ func UserInfo(header *protocol.RequestHeader, request *protocol.UserInfoRequest)
return
}
rsp.User = protocol.User{
Id: partnerInfo.Id,
PartnerCategory: partnerInfo.PartnerCategory,
PartnerName: partnerInfo.PartnerName,
RegionInfo: partnerInfo.RegionInfo,
Id: partnerInfo.Id,
PartnerName: partnerInfo.PartnerName,
Phone: partnerInfo.Account,
CooperateCompany: protocol.Company{
Id: 1,
Name: "福州素天下有限公司",
},
JoinWay: partnerInfo.PartnerCategoryInfo(),
District: partnerInfo.RegionInfo,
SerialNo: partnerInfo.Id,
CooperateTime: partnerInfo.CooperateTime.Unix() * 1000,
}
if len(partnerInfo.Salesman) > 0 {
... ... @@ -44,19 +46,15 @@ func CheckSmsCode(header *protocol.RequestHeader, request *protocol.CheckSmsCode
var (
PartnerInfoService = service.NewPartnerInfoService(nil)
partnerInfo *domain.PartnerInfo
result bool
)
if partnerInfo, err = PartnerInfoService.GetPartnerInfo(&query.GetPartnerInfoQuery{Id: int(header.UserId)}); err != nil {
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
if result, err = auth.CheckSmsCode(strings.TrimSpace(partnerInfo.Account), request.Captcha); err != nil {
log.Error(err)
if _, err = auth.CheckSmsCode(strings.TrimSpace(partnerInfo.Account), request.Captcha); err != nil {
log.Error(err, partnerInfo.Account, request.Captcha)
return
}
if !result {
err = protocol.NewErrWithMessage(1012)
}
return
}
... ... @@ -69,12 +67,15 @@ func ChangePhone(header *protocol.RequestHeader, request *protocol.ChangePhoneRe
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
if _, err = PartnerInfoService.GetPartnerInfo(&query.GetPartnerInfoQuery{Account: request.Phone}); err == nil {
err = protocol.NewErrWithMessage(2029, err) //账号不存在
return
}
if _, err = auth.CheckSmsCode(request.Phone, request.Captcha); err != nil {
log.Error(err)
return
}
if _, err = PartnerInfoService.UpdatePartnerInfo(&command.UpdatePartnerInfoCommand{Account: request.Phone}); err == nil {
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
return
... ... @@ -84,7 +85,7 @@ func ChangePhone(header *protocol.RequestHeader, request *protocol.ChangePhoneRe
func ResetPassword(header *protocol.RequestHeader, request *protocol.ResetPasswordRequest) (rsp *protocol.ResetPasswordResponse, err error) {
var (
PartnerInfoService = service.NewPartnerInfoService(nil)
//partnerInfo *domain.PartnerInfo
partnerInfo *domain.PartnerInfo
)
if len(request.NewPwd) < 6 {
err = protocol.NewErrWithMessage(2027)
... ... @@ -94,10 +95,14 @@ func ResetPassword(header *protocol.RequestHeader, request *protocol.ResetPasswo
err = protocol.NewErrWithMessage(2026)
return
}
if _, err = PartnerInfoService.GetPartnerInfo(&query.GetPartnerInfoQuery{Id: int(header.UserId)}); err != nil {
if partnerInfo, err = PartnerInfoService.GetPartnerInfo(&query.GetPartnerInfoQuery{Id: int(header.UserId)}); err != nil {
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
if _, err = auth.CheckSmsCode(partnerInfo.Account, request.Captcha); err != nil {
log.Error(err)
return
}
if _, err = PartnerInfoService.UpdatePartnerInfo(&command.UpdatePartnerInfoCommand{Password: request.ConfirmPwd}); err == nil {
return
}
... ...
... ... @@ -50,7 +50,7 @@ func (m *PartnerInfo) Update(data map[string]interface{}) error {
if account, ok := data["account"]; ok && account != "" {
m.Account = account.(string)
}
if password, ok := data["account"]; ok && password != "" {
if password, ok := data["password"]; ok && password != "" {
m.Password = password.(string)
}
if status, ok := data["status"]; ok && status != 0 {
... ... @@ -65,3 +65,30 @@ func (m *PartnerInfo) Update(data map[string]interface{}) error {
m.UpdateAt = time.Now()
return nil
}
func (m *PartnerInfo) PartnerCategoryInfo() *PartnerCategoryInfo {
var name string
switch m.PartnerCategory {
case 1:
name = "事业合伙人"
break
case 2:
name = "业务合伙人"
break
case 3:
name = "研发合伙人"
break
default:
name = "事业合伙人"
break
}
return &PartnerCategoryInfo{
Id: m.PartnerCategory,
Name: name,
}
}
type PartnerCategoryInfo struct {
Id int `json:"id"`
Name string `json:"name"`
}
... ...
package dao
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
)
type OrderDao struct {
transactionContext *transaction.TransactionContext
}
func NewEmployeeDao(transactionContext *transaction.TransactionContext) (*OrderDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &OrderDao{
transactionContext: transactionContext,
}, nil
}
}
... ...
... ... @@ -50,8 +50,8 @@ func (repository *OrderRepository) FindOne(queryOptions map[string]interface{})
tx := repository.transactionContext.PgTx
OrderModel := new(models.Order)
query := NewQuery(tx.Model(OrderModel), queryOptions)
query.SetWhere("order.id = ?", "id")
query.SetWhere("order.order_code = ?", "orderCode")
query.SetWhere(`"order".id = ?`, "id")
query.SetWhere(`"order".order_code = ?`, "orderCode")
if err := query.First(); err != nil {
return nil, query.HandleError(err, "没有此订单")
}
... ...
package controllers
import (
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/protocol"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/services/auth"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/auth"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
)
type AuthController struct {
... ...
... ... @@ -5,10 +5,8 @@ import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/validation"
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/web/beego/utils"
comm "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/protocol"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
"strconv"
)
... ... @@ -18,46 +16,15 @@ type BaseController struct {
func (controller BaseController) JsonUnmarshal(v interface{}) error {
body := controller.Ctx.Input.GetData("requestBody").([]byte)
//fmt.Println("【RequestBody】 ",controller.Ctx.Input.Method(),controller.Ctx.Input.URL(),string(body))
return json.Unmarshal(body, v)
}
func (controller BaseController) HandlerResponse(data interface{}, err error) {
var response utils.JsonResponse
defer func() {
controller.Data["json"] = response
controller.ServeJSON()
}()
if err == nil {
response = utils.ResponseData(controller.Ctx, data)
return
}
if _, ok := err.(*application.ServiceError); !ok {
err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if x, ok := err.(protocol.CustomErrParse); ok {
m := x.ParseToMessage()
response = ResponseCustomData(controller.Ctx, data, m)
return
}
response = utils.ResponseError(controller.Ctx, err)
}
func (controller BaseController) GetLimitInfo() (offset int, limit int) {
offset, _ = controller.GetInt("offset")
limit, _ = controller.GetInt("limit")
return
}
func ResponseCustomData(ctx *context.Context, data interface{}, msg *protocol.ResponseMessage) utils.JsonResponse {
jsonResponse := utils.JsonResponse{}
jsonResponse["code"] = 0
jsonResponse["msg"] = "ok"
jsonResponse["data"] = data
ctx.Input.SetData("outputData", jsonResponse)
return jsonResponse
}
//Valid valid struct
func (controller *BaseController) Valid(obj interface{}) (result bool, msg *protocol.ResponseMessage) {
/*校验*/
... ... @@ -102,7 +69,7 @@ func (this *BaseController) GetRequestHeader(ctx *context.Context) *protocol.Req
h.UserId, _ = strconv.ParseInt(ctx.Input.Header("x-mmm-id"), 10, 64)
if len(h.AccessToken) > 0 {
if claim, err := comm.ParseJWTToken(h.AccessToken); err == nil {
if claim, err := utils.ParseJWTToken(h.AccessToken); err == nil {
h.UserId = claim.UserId
}
}
... ...
package controllers
import (
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/order"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
)
type OrderController struct {
BaseController
}
//OrderDetail 订单详情
// @router /orderDetail [post]
func (this *OrderController) OrderDetail() {
var msg *protocol.ResponseMessage
defer func() {
this.Resp(msg)
}()
var request *protocol.OrderDetailRequest
if err := this.JsonUnmarshal(&request); err != nil {
msg = protocol.BadRequestParam(1)
return
}
if b, m := this.Valid(request); !b {
msg = m
return
}
header := this.GetRequestHeader(this.Ctx)
msg = protocol.NewReturnResponse(order.OrderDetail(header, request))
}
... ...
package controllers
import (
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/command"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/query"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/service"
)
type PartnerInfoController struct {
BaseController
}
//CreatePartnerInfo 创建合伙人
func (controller *PartnerInfoController) CreatePartnerInfo() {
var (
service = service.NewPartnerInfoService(nil)
command = &command.CreatePartnerInfoCommand{}
)
controller.JsonUnmarshal(command)
controller.HandlerResponse(service.CreatePartnerInfo(command))
}
//UpdatePartnerInfo 更新合伙人
func (controller *PartnerInfoController) UpdatePartnerInfo() {
var (
service = service.NewPartnerInfoService(nil)
command = &command.UpdatePartnerInfoCommand{}
)
controller.JsonUnmarshal(command)
id, _ := controller.GetInt(":id")
command.Id = id
controller.HandlerResponse(service.UpdatePartnerInfo(command))
}
//GetPartnerInfo 获取合伙人
func (controller *PartnerInfoController) GetPartnerInfo() {
var (
service = service.NewPartnerInfoService(nil)
command = &query.GetPartnerInfoQuery{}
)
uid, _ := controller.GetInt(":id")
command.Id = uid
controller.HandlerResponse(service.GetPartnerInfo(command))
}
//GetPartnerInfo 移除合伙人
func (controller *PartnerInfoController) RemovePartnerInfo() {
var (
service = service.NewPartnerInfoService(nil)
command = &command.RemovePartnerInfoCommand{}
)
uid, _ := controller.GetInt(":id")
command.Id = uid
controller.HandlerResponse(service.RemovePartnerInfo(command))
}
//ListPartnerInfo 合伙人列表
func (controller *PartnerInfoController) ListPartnerInfo() {
var (
service = service.NewPartnerInfoService(nil)
command = &query.ListPartnerInfoQuery{}
)
command.PartnerCategory, _ = controller.GetInt("partnerCategory")
command.Status, _ = controller.GetInt("status")
command.SortByCreateTime = controller.GetString("sortByCreateTime")
command.SortByUpdateTime = controller.GetString("sortByUpdateTime")
command.Offset, command.Limit = controller.GetLimitInfo()
controller.HandlerResponse(service.ListPartnerInfo(command))
}
package controllers
import (
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/protocol"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/services/user"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/user"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
)
type UserController struct {
... ...
... ... @@ -3,7 +3,7 @@ package middleware
import (
"github.com/astaxie/beego/context"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/protocol"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
"strings"
)
... ...
... ... @@ -18,8 +18,10 @@ func init() {
nsV1.Router("user/userInfo", &controllers.UserController{}, "Post:UserInfo")
nsV1.Router("user/checkSmsCode", &controllers.UserController{}, "Post:CheckSmsCode")
nsV1.Router("user/ChangePhone", &controllers.UserController{}, "Post:ChangePhone")
nsV1.Router("user/changePhone", &controllers.UserController{}, "Post:ChangePhone")
nsV1.Router("user/resetPassword", &controllers.UserController{}, "Post:ResetPassword")
nsV1.Router("user/changePassword", &controllers.UserController{}, "Post:ChangePassword")
nsV1.Router("order/details", &controllers.OrderController{}, "Post:OrderDetail")
beego.AddNamespace(nsV1)
}
... ...
package protocol
const (
LoginByPassword = iota + 1
LoginBySmsCode
LoginByPassword = "signInPassword"
LoginBySmsCode = " signInCaptcha"
)
const AuthCodeExpire = 60 * 15
... ... @@ -11,10 +11,11 @@ const RefreshTokenExipre = 3600 * 24 * 30 * 3 //刷新token 三个月过期
/*Login */
type LoginRequest struct {
Phone string `json:"phone" valid:"Required;"`
Password string `json:"password"`
SmsCode string `json:"smsCode"`
Type int `json:"type"` // 登录类型 1:密码 2:验证码
Phone string `json:"phone" valid:"Required;"`
Password string `json:"password"`
Captcha string `json:"captcha"`
GrantType string `json:"grantType"` // 登录类型 1:密码 2:验证码
ClientId string `json:"clientId"`
}
type LoginResponse struct {
AuthCode string `json:"authCode"`
... ...
package protocol
/*OrderDetail */
type OrderDetailRequest struct {
Id int64 `json:"id"`
}
type OrderDetailResponse struct {
Order interface{} `json:"order"`
Partner interface{} `json:"partner"`
}
... ...
... ... @@ -12,15 +12,22 @@ type UserInfoResponse struct {
}
type User struct {
Id int64 `json:"id"`
Id int64 `json:"uid"`
//用户名称
PartnerName string `json:"uname"`
//手机号
Phone string `json:"phone"`
//合作公司
CooperateCompany Company `json:"company"`
// 合伙类别 (1.研发合伙人 2.业务合伙人 3.事业)
PartnerCategory int `json:"partnerCategory"`
PartnerName string `json:"partnerName"`
//所属区域信息
RegionInfo *domain.RegionInfo `json:"regionInfo"`
CooperateCompany Company `json:"cooperateCompany"`
CooperateTime int64 `json:"cooperateTime"`
Salesman *domain.Salesman `json:"salesman"`
JoinWay *domain.PartnerCategoryInfo `json:"joinWay"`
//合作区域
District *domain.RegionInfo `json:"district"`
//合作编码
SerialNo int64 `json:"serialNo"`
//合作时间
CooperateTime int64 `json:"cooperateTime"`
Salesman *domain.Salesman `json:"salesman"`
}
type Company struct {
... ... @@ -45,6 +52,7 @@ type ChangePhoneResponse struct {
/*ResetPassword */
type ResetPasswordRequest struct {
Captcha string `json:"captcha" valid:"Required"`
NewPwd string `json:"newPwd" valid:"Required"`
ConfirmPwd string `json:"confirmPwd" valid:"Required"`
}
... ...