作者 yangfu

1.认证修改

... ... @@ -44,10 +44,11 @@ func Login(header *protocol.RequestHeader, request *protocol.LoginRequest) (rsp
return
}
func SmsCode(request *protocol.SmsCodeRequest) (rsp *protocol.LoginResponse, err error) {
func SmsCode(request *protocol.SmsCodeRequest) (rsp *protocol.SmsCodeResponse, err error) {
var data map[string]interface{}
sms := svr.NewHttplibMmmSmsApiServiceGateway()
data, err = sms.SendSms(request.Phone)
rsp = &protocol.SmsCodeResponse{}
if err != nil {
if msg, ok := data["msg"]; ok {
err = protocol.NewCustomMessage(1, msg.(string))
... ... @@ -109,3 +110,15 @@ func CheckSmsCode(phone, code string) (result bool, err error) {
}
return
}
//验证手机号码(修改手机号码、重置密码的前一步)
func AuthCheckSmsCode(header *protocol.RequestHeader, request *protocol.AuthCheckSmsCodeRequest) (rsp *protocol.AuthCheckSmsCodeResponse, err error) {
var ()
if _, err = CheckSmsCode(request.Phone, request.Captcha); err != nil {
return
}
rsp = &protocol.AuthCheckSmsCodeResponse{
CaptchaCertificate: fmt.Sprintf("%v", utils.GenerateRangeNum(100000, 799999)),
}
return
}
... ...
... ... @@ -16,3 +16,7 @@ import (
func CreateOrderDao(ctx *transaction.TransactionContext) (*dao.OrderDao, error) {
return dao.NewOrderDao(ctx)
}
func CreatePartnerInfoDao(ctx *transaction.TransactionContext) (*dao.PartnerInfoDao, error) {
return dao.NewPartnerInfoDao(ctx)
}
... ...
... ... @@ -31,8 +31,8 @@ func OrderDetail(header *protocol.RequestHeader, request *protocol.OrderDetailRe
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
OrderResponsitory, _ = factory.CreateOrderRepository(transactionContext)
//PartnerInfoRepository, _ = factory.CreatePartnerInfoRepositoryIn(transactionContext)
order *domain.Order
OrderDao, _ = factory.CreateOrderDao(transactionContext)
order *domain.Order
)
if err = transactionContext.StartTransaction(); err != nil {
return nil, err
... ... @@ -47,9 +47,6 @@ func OrderDetail(header *protocol.RequestHeader, request *protocol.OrderDetailRe
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 = protocol.OrderDetail{
Id: order.Id,
OrderNo: order.OrderCode,
... ... @@ -65,6 +62,11 @@ func OrderDetail(header *protocol.RequestHeader, request *protocol.OrderDetailRe
MyDividend: order.OrderTotalBonus(),
MyDividendPercent: order.PartnerBonusPercent,
}
if header.UserId == order.PartnerId && order.UpdateAt.After(order.LastViewTime) {
if err = OrderDao.UpdateLastViewTime(order.Id, time.Now()); err != nil {
return
}
}
err = transactionContext.CommitTransaction()
return
}
... ...
... ... @@ -2,7 +2,7 @@ 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/factory"
"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"
... ... @@ -61,21 +61,37 @@ func CheckSmsCode(header *protocol.RequestHeader, request *protocol.CheckSmsCode
//修改手机号
func ChangePhone(header *protocol.RequestHeader, request *protocol.ChangePhoneRequest) (rsp *protocol.ChangePhoneResponse, err error) {
var (
PartnerInfoService = service.NewPartnerInfoService(nil)
PartnerInfoService = service.NewPartnerInfoService(nil)
transactionContext, _ = factory.CreateTransactionContext(nil)
PartnerInfoDao, _ = factory.CreatePartnerInfoDao(transactionContext)
partnerInfo *domain.PartnerInfo
)
if _, err = PartnerInfoService.GetPartnerInfo(&query.GetPartnerInfoQuery{Id: int(header.UserId)}); err != nil {
if err = transactionContext.StartTransaction(); err != nil {
return nil, err
}
defer func() {
if err != nil {
transactionContext.RollbackTransaction()
return
}
err = transactionContext.CommitTransaction()
}()
if partnerInfo, err = PartnerInfoService.GetPartnerInfo(&query.GetPartnerInfoQuery{Id: int(header.UserId)}); err != nil {
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
if _, err = PartnerInfoService.GetPartnerInfo(&query.GetPartnerInfoQuery{Account: request.Phone}); err == nil {
err = protocol.NewErrWithMessage(2029, err) //账号存在
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 {
if err = PartnerInfoDao.Update(map[string]interface{}{
"Id": partnerInfo.Id,
"Account": request.Phone,
}); err != nil {
return
}
return
... ... @@ -84,9 +100,22 @@ 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
PartnerInfoService = service.NewPartnerInfoService(nil)
partnerInfo *domain.PartnerInfo
transactionContext, _ = factory.CreateTransactionContext(nil)
PartnerInfoDao, _ = factory.CreatePartnerInfoDao(transactionContext)
)
rsp = &protocol.ResetPasswordResponse{}
if err = transactionContext.StartTransaction(); err != nil {
return nil, err
}
defer func() {
if err != nil {
transactionContext.RollbackTransaction()
return
}
err = transactionContext.CommitTransaction()
}()
if len(request.NewPwd) < 6 {
err = protocol.NewErrWithMessage(2027)
return
... ... @@ -99,11 +128,14 @@ func ResetPassword(header *protocol.RequestHeader, request *protocol.ResetPasswo
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 {
//if _, err = auth.CheckSmsCode(partnerInfo.Account, request.Captcha); err != nil {
// log.Error(err)
// return
//}
if err = PartnerInfoDao.Update(map[string]interface{}{
"Id": partnerInfo.Id,
"Password": request.ConfirmPwd,
}); err != nil {
return
}
return
... ... @@ -112,9 +144,21 @@ func ResetPassword(header *protocol.RequestHeader, request *protocol.ResetPasswo
//修改密码
func ChangePassword(header *protocol.RequestHeader, request *protocol.ChangePasswordRequest) (rsp *protocol.ChangePasswordResponse, err error) {
var (
PartnerInfoService = service.NewPartnerInfoService(nil)
partnerInfo *domain.PartnerInfo
PartnerInfoService = service.NewPartnerInfoService(nil)
partnerInfo *domain.PartnerInfo
transactionContext, _ = factory.CreateTransactionContext(nil)
PartnerInfoDao, _ = factory.CreatePartnerInfoDao(transactionContext)
)
if err = transactionContext.StartTransaction(); err != nil {
return nil, err
}
defer func() {
if err != nil {
transactionContext.RollbackTransaction()
return
}
err = transactionContext.CommitTransaction()
}()
rsp = &protocol.ChangePasswordResponse{}
if !strings.EqualFold(request.NewPwd, request.ConfirmPwd) {
err = protocol.NewErrWithMessage(2026)
... ... @@ -137,7 +181,10 @@ func ChangePassword(header *protocol.RequestHeader, request *protocol.ChangePass
err = protocol.NewErrWithMessage(2028, err) //账号不存在
return
}
if _, err = PartnerInfoService.UpdatePartnerInfo(&command.UpdatePartnerInfoCommand{Password: request.ConfirmPwd}); err == nil {
if err = PartnerInfoDao.Update(map[string]interface{}{
"Id": partnerInfo.Id,
"Password": request.ConfirmPwd,
}); err != nil {
return
}
return
... ...
package dao
//import (
// "fmt"
// "github.com/go-pg/pg/v10"
// pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
// "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
// "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg/models"
// "time"
//)
//
//type EmployeeDao struct {
// transactionContext *pgTransaction.TransactionContext
//}
//
//func (dao *EmployeeDao) BatchRemove(uids []int64) error {
// tx := dao.transactionContext.PgTx
// _, err := tx.QueryOne(
// pg.Scan(),
// "DELETE FROM employees WHERE uid IN (?)",
// pg.In(uids))
// return err
//}
//
//func (dao *EmployeeDao) BatchSetStatus(uids []int64, status int) error {
// tx := dao.transactionContext.PgTx
// _, err := tx.QueryOne(
// pg.Scan(),
// "UPDATE employees SET status=? WHERE uid IN (?)",
// status, pg.In(uids))
// return err
//}
//
//func (dao *EmployeeDao) ChangePrincipal(companyId int64, employeeAccount string) error {
// tx := dao.transactionContext.PgTx
// if _, err := tx.Query(
// pg.Scan(),
// "UPDATE employees SET is_principal=? WHERE company_id=?",
// false, companyId); err != nil {
// return err
// }
// if _, err := tx.QueryOne(
// pg.Scan(),
// "UPDATE employees SET is_principal=? WHERE company_id=? AND employee_account=?",
// true, companyId, employeeAccount); err != nil {
// return err
// }
// return nil
//}
//
//func (dao *EmployeeDao) TransferSuMoney(uid int64, suMoney float64) error {
// tx := dao.transactionContext.PgTx
// _, err := tx.QueryOne(
// pg.Scan(),
// "UPDATE employees SET su_money=su_money+? WHERE uid=?",
// suMoney, uid)
// return err
//}
//
//func (dao *EmployeeDao) CalculatePersonUnReadNotification(uid int64) (map[string]int, error) {
// var unReadSystemNotification int
// var unReadInteractionNotification int
// tx := dao.transactionContext.PgTx
// sentNotificationModel := new(models.SentNotification)
// if count, err := tx.Model(sentNotificationModel).Relation("Notification").
// Where(`sent_notification.receiver @> '{"uid":?}'`, uid).
// Where("notification.notification_type = ?", domain.NOTIFICATION_TYPE_SYSTEM).
// Where("sent_notification.is_read = ?", false).
// Count(); err != nil {
// return nil, err
// } else {
// unReadSystemNotification = count
// }
// if count, err := tx.Model(sentNotificationModel).Relation("Notification").
// Where(`sent_notification.receiver @> '{"uid":?}'`, uid).
// Where("notification.notification_type = ?", domain.NOTIFICATION_TYPE_INTERACTION).
// Where("sent_notification.is_read = ?", false).
// Count(); err != nil {
// return nil, err
// } else {
// unReadInteractionNotification = count
// }
// return map[string]int{
// "unReadSystemNotification": unReadSystemNotification,
// "unReadInteractionNotification": unReadInteractionNotification,
// }, nil
//}
//
//func (dao *EmployeeDao) CalculatePersonSuMoney(uid int64) (map[string]interface{}, error) {
// var incomeSuMoney float64
// var incomeSuMoneyOfYesterday float64
// tx := dao.transactionContext.PgTx
// suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
// yesterday := time.Now().AddDate(0, 0, -1)
// if err := tx.Model(suMoneyTransactionRecordModel).
// ColumnExpr("sum(su_money_transaction_record.su_money) AS income_su_money").
// Where(`su_money_transaction_record.employee @> '{"uid":?}'`, uid).
// Where(`su_money_transaction_record.record_type = ?`, 2).
// Where(`su_money_transaction_record.create_time > ?`, time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 0, 0, 0, 0, yesterday.Location())).
// Where(`su_money_transaction_record.create_time < ?`, time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 23, 59, 59, 0, yesterday.Location())).
// Select(&incomeSuMoneyOfYesterday); err != nil {
// return nil, err
// }
// if err := tx.Model(suMoneyTransactionRecordModel).
// ColumnExpr("sum(su_money_transaction_record.su_money) AS income_su_money").
// Where(`su_money_transaction_record.employee @> '{"uid":?}'`, uid).
// Where(`su_money_transaction_record.record_type = ?`, 2).
// Select(&incomeSuMoney); err != nil {
// return nil, err
// }
// return map[string]interface{}{
// "incomeSuMoney": incomeSuMoney,
// "incomeSuMoneyOfYesterday": incomeSuMoneyOfYesterday,
// }, nil
//}
//
//func (dao *EmployeeDao) CalculateSuMoneyTransactionRecord(uid int64, transactionStartTime time.Time, transactionEndTime time.Time) (map[string]interface{}, error) {
// var incomeSuMoney float64
// var expendSuMoney float64
// tx := dao.transactionContext.PgTx
// suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
// if err := tx.Model(suMoneyTransactionRecordModel).
// ColumnExpr("sum(su_money_transaction_record.su_money) AS income_su_money").
// Where(`su_money_transaction_record.employee @> '{"uid":?}'`, uid).
// Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3})).
// Where(`su_money_transaction_record.create_time > ?`, transactionStartTime).
// Where(`su_money_transaction_record.create_time < ?`, transactionEndTime).
// Select(&incomeSuMoney); err != nil {
// return nil, err
// }
// if err := tx.Model(suMoneyTransactionRecordModel).
// ColumnExpr("sum(su_money_transaction_record.su_money) AS expend_su_money").
// Where(`su_money_transaction_record.employee @> '{"uid":?}'`, uid).
// Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{1, 4})).
// Where(`su_money_transaction_record.create_time > ?`, transactionStartTime).
// Where(`su_money_transaction_record.create_time < ?`, transactionEndTime).
// Select(&expendSuMoney); err != nil {
// return nil, err
// }
// return map[string]interface{}{
// "incomeSuMoney": incomeSuMoney,
// "expendSuMoney": expendSuMoney,
// }, nil
//}
//
//func NewEmployeeDao(transactionContext *pgTransaction.TransactionContext) (*EmployeeDao, error) {
// if transactionContext == nil {
// return nil, fmt.Errorf("transactionContext参数不能为nil")
// } else {
// return &EmployeeDao{
// transactionContext: transactionContext,
// }, nil
// }
//}
... ... @@ -12,6 +12,7 @@ type OrderDao struct {
transactionContext *transaction.TransactionContext
}
//订单统计
func (dao *OrderDao) OrderStatics(option *domain.OrderStaticQuery) (count int, amount float64, err error) {
tx := dao.transactionContext.PgTx
order := new(models.Order)
... ... @@ -44,17 +45,17 @@ func (dao *OrderDao) DividendOrders(option *domain.DividendOrdersQueryOption) (c
q.Column(`order.id`, `order.order_code`, `order.order_actual_amount`, `order.partner_bonus_percent`)
q.Where(`"order".order_status >=?`, domain.OrderStatusDeliverSome) //已经发货
if option.StartTime > 0 {
q.Where(`"order".delivery_time >=?`, time.Unix(option.StartTime/1000, 0))
q.Where(`"order".create_at >=?`, time.Unix(option.StartTime/1000, 0))
}
if option.EndTime > 0 {
q.Where(`"order".delivery_time <?`, time.Unix(option.EndTime/1000, 0))
q.Where(`"order".create_at <?`, time.Unix(option.EndTime/1000, 0))
}
if option.DividendAction == 0 { //累计分红
if option.DetailAction == 0 { //已收明细
q.Join(`JOIN order_payment as a ON a.order_id="order".id`)
q.Where(`"a".bonus_status=?`, domain.BonusPaid)
} else if option.DetailAction == 1 { //未收明细
q.Where(`"order".order_actual_amount>"order".order_payment_amount`, domain.BonusWaitPay)
q.Where(`"order".order_actual_amount>"order".order_payment_amount`)
}
} else if option.DividendAction == 1 { //分红支出
q.Where(`"order".order_amount>"order".order_actual_amount`)
... ... @@ -69,6 +70,15 @@ func (dao *OrderDao) DividendOrders(option *domain.DividendOrdersQueryOption) (c
return
}
func (dao *OrderDao) UpdateLastViewTime(id int64, lastViewTime time.Time) (err error) {
tx := dao.transactionContext.PgTx
order := new(models.Order)
q := tx.Model(order).Set("last_view_time = ?", lastViewTime)
q.Where("id=?", id)
_, err = q.Update()
return
}
func NewOrderDao(transactionContext *transaction.TransactionContext) (*OrderDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
... ...
package dao
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
. "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
)
type PartnerInfoDao struct {
transactionContext *transaction.TransactionContext
}
func (dao *PartnerInfoDao) Update(queryOptions map[string]interface{}) error {
tx := dao.transactionContext.PgTx
m := new(models.PartnerInfo)
query := NewQuery(tx.Model(m), queryOptions)
query.SetUpdate(`account=?`, "Account")
query.SetUpdate(`password=?`, "Password")
query.SetWhere(`id=?`, "Id")
if _, ok := queryOptions["Id"]; !ok {
return fmt.Errorf(`error: miss param "Id"`)
}
_, err := query.Update()
return err
}
func NewPartnerInfoDao(transactionContext *transaction.TransactionContext) (*PartnerInfoDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &PartnerInfoDao{
transactionContext: transactionContext,
}, nil
}
}
... ...
... ... @@ -4,6 +4,7 @@ import (
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
. "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
)
type AdminUserRepository struct {
... ...
... ... @@ -4,6 +4,7 @@ import (
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
. "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
)
type OrderPaymentRepository struct {
... ...
... ... @@ -4,6 +4,7 @@ import (
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
. "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
)
type OrderRepository struct {
... ...
... ... @@ -4,6 +4,7 @@ import (
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
. "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
)
type PartnerInfoRepository struct {
... ...
package repository
import (
"bytes"
"encoding/gob"
"fmt"
"github.com/go-pg/pg/v10/orm"
"github.com/linmadan/egglib-go/utils/snowflake"
"strconv"
"time"
)
var (
ERR_EMPTY_TC = fmt.Errorf("transactionContext参数不能为nil")
)
type Query struct {
*orm.Query
queryOptions map[string]interface{}
AffectRow int
}
func NewQuery(query *orm.Query, queryOptions map[string]interface{}) *Query {
return &Query{
query,
queryOptions,
0,
}
}
func (query *Query) SetWhere(condition, key string) *Query {
if v, ok := query.queryOptions[key]; ok {
if t, e := time.Parse(time.RFC3339, fmt.Sprintf("%v", v)); e == nil {
if t.IsZero() {
return query
}
}
query.Where(condition, v)
}
return query
}
func (query *Query) SetLimit() *Query {
if offset, ok := query.queryOptions["offset"]; ok {
offset, _ := strconv.ParseInt(fmt.Sprintf("%v", offset), 10, 64)
if offset > -1 {
query.Offset(int(offset))
}
} else {
query.Offset(0)
}
if limit, ok := query.queryOptions["limit"]; ok {
limit, _ := strconv.ParseInt(fmt.Sprintf("%v", limit), 10, 64)
if limit > -1 {
query.Limit(int(limit))
} else {
query.Limit(20)
}
}
return query
}
func (query *Query) SetOrder(orderColumn string, key string) *Query {
//query.Order(condition...)
//return query
if v, ok := query.queryOptions[key]; ok {
query.Order(fmt.Sprintf("%v %v", orderColumn, v))
}
return query
}
func (query *Query) HandleError(err error, errMsg string) error {
if err.Error() == "pg: no rows in result set" {
return fmt.Errorf(errMsg)
} else {
return err
}
}
func NewSnowflakeId() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(2)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
return id, err
}
//GobModelTransform 模型转换
func GobModelTransform(dst interface{}, src interface{}) error {
var data bytes.Buffer
enc := gob.NewEncoder(&data)
if err := enc.Encode(src); err != nil {
return err
}
dec := gob.NewDecoder(&data)
if err := dec.Decode(dst); err != nil {
return err
}
return nil
}
//import (
// "bytes"
// "encoding/gob"
// "fmt"
// "github.com/go-pg/pg/v10/orm"
// "github.com/linmadan/egglib-go/utils/snowflake"
// "strconv"
// "time"
//)
//
//var (
// ERR_EMPTY_TC = fmt.Errorf("transactionContext参数不能为nil")
//)
//
//type Query struct {
// *orm.Query
// queryOptions map[string]interface{}
// AffectRow int
//}
//
//func NewQuery(query *orm.Query, queryOptions map[string]interface{}) *Query {
// return &Query{
// query,
// queryOptions,
// 0,
// }
//}
//
//func (query *Query) SetWhere(condition, key string) *Query {
// if v, ok := query.queryOptions[key]; ok {
// if t, e := time.Parse(time.RFC3339, fmt.Sprintf("%v", v)); e == nil {
// if t.IsZero() {
// return query
// }
// }
// query.Where(condition, v)
// }
// return query
//}
//
//func (query *Query) SetLimit() *Query {
// if offset, ok := query.queryOptions["offset"]; ok {
// offset, _ := strconv.ParseInt(fmt.Sprintf("%v", offset), 10, 64)
// if offset > -1 {
// query.Offset(int(offset))
// }
// } else {
// query.Offset(0)
// }
// if limit, ok := query.queryOptions["limit"]; ok {
// limit, _ := strconv.ParseInt(fmt.Sprintf("%v", limit), 10, 64)
// if limit > -1 {
// query.Limit(int(limit))
// } else {
// query.Limit(20)
// }
// }
// return query
//}
//
//func (query *Query) SetOrder(orderColumn string, key string) *Query {
// //query.Order(condition...)
// //return query
// if v, ok := query.queryOptions[key]; ok {
// query.Order(fmt.Sprintf("%v %v", orderColumn, v))
// }
// return query
//}
//
//func (query *Query) HandleError(err error, errMsg string) error {
// if err.Error() == "pg: no rows in result set" {
// return fmt.Errorf(errMsg)
// } else {
// return err
// }
//}
//
//func NewSnowflakeId() (int64, error) {
// IdWorker, err := snowflake.NewIdWorker(2)
// if err != nil {
// return 0, err
// }
// id, err := IdWorker.NextId()
// return id, err
//}
//
////GobModelTransform 模型转换
//func GobModelTransform(dst interface{}, src interface{}) error {
// var data bytes.Buffer
// enc := gob.NewEncoder(&data)
// if err := enc.Encode(src); err != nil {
// return err
// }
// dec := gob.NewDecoder(&data)
// if err := dec.Decode(dst); err != nil {
// return err
// }
// return nil
//}
... ...
... ... @@ -3,6 +3,8 @@ package utils
import (
"github.com/shopspring/decimal"
"math"
"math/rand"
"time"
)
func decimal1(value float64) float64 {
... ... @@ -23,3 +25,9 @@ func Decimal(value float64) float64 {
func DecimalToNumber(value float64) float64 {
return Round(value, 2)
}
func GenerateRangeNum(min, max int) int {
rand.Seed(time.Now().Unix())
randNum := rand.Intn(max-min) + min
return randNum
}
... ...
package utils
import (
"bytes"
"encoding/gob"
"fmt"
"github.com/go-pg/pg/v10/orm"
"github.com/linmadan/egglib-go/utils/snowflake"
"strconv"
"time"
)
var (
ERR_EMPTY_TC = fmt.Errorf("transactionContext参数不能为nil")
)
type Query struct {
*orm.Query
queryOptions map[string]interface{}
AffectRow int
}
func NewQuery(query *orm.Query, queryOptions map[string]interface{}) *Query {
return &Query{
query,
queryOptions,
0,
}
}
func (query *Query) SetWhere(condition, key string) *Query {
if v, ok := query.queryOptions[key]; ok {
if t, e := time.Parse(time.RFC3339, fmt.Sprintf("%v", v)); e == nil {
if t.IsZero() {
return query
}
}
query.Where(condition, v)
}
return query
}
func (query *Query) SetUpdate(condition, key string) *Query {
if v, ok := query.queryOptions[key]; ok {
query.Set(condition, v)
}
return query
}
func (query *Query) SetLimit() *Query {
if offset, ok := query.queryOptions["offset"]; ok {
offset, _ := strconv.ParseInt(fmt.Sprintf("%v", offset), 10, 64)
if offset > -1 {
query.Offset(int(offset))
}
} else {
query.Offset(0)
}
if limit, ok := query.queryOptions["limit"]; ok {
limit, _ := strconv.ParseInt(fmt.Sprintf("%v", limit), 10, 64)
if limit > -1 {
query.Limit(int(limit))
} else {
query.Limit(20)
}
}
return query
}
func (query *Query) SetOrder(orderColumn string, key string) *Query {
//query.Order(condition...)
//return query
if v, ok := query.queryOptions[key]; ok {
query.Order(fmt.Sprintf("%v %v", orderColumn, v))
}
return query
}
func (query *Query) HandleError(err error, errMsg string) error {
if err.Error() == "pg: no rows in result set" {
return fmt.Errorf(errMsg)
} else {
return err
}
}
func NewSnowflakeId() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(2)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
return id, err
}
//GobModelTransform 模型转换
func GobModelTransform(dst interface{}, src interface{}) error {
var data bytes.Buffer
enc := gob.NewEncoder(&data)
if err := enc.Encode(src); err != nil {
return err
}
dec := gob.NewDecoder(&data)
if err := dec.Decode(dst); err != nil {
return err
}
return nil
}
... ...
... ... @@ -3,6 +3,7 @@ package controllers
import (
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/auth"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
"time"
)
type AuthController struct {
... ... @@ -127,3 +128,25 @@ func (this *AuthController) UpdateDevice() {
//header := controllers.GetRequestHeader(this.Ctx)
//msg = protocol.NewReturnResponse(auth.UpdateDevice(header, request))
}
//AuthCheckSmsCode 验证手机号码(修改手机号码、重置密码的前一步)
//@router /checkSmsCod [post]
func (this *AuthController) AuthCheckSmsCode() {
var msg *protocol.ResponseMessage
defer func() {
this.Resp(msg)
}()
var request *protocol.AuthCheckSmsCodeRequest
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)
rsp, err := auth.AuthCheckSmsCode(header, request)
err = CacheSms.Put(request.Phone, rsp.CaptchaCertificate, 5*time.Minute)
msg = protocol.NewReturnResponse(rsp, err)
}
... ...
... ... @@ -3,6 +3,7 @@ package controllers
import (
"encoding/json"
"github.com/astaxie/beego"
"github.com/astaxie/beego/cache"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/validation"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
... ... @@ -10,6 +11,9 @@ import (
"strconv"
)
//缓存
var CacheSms = cache.NewMemoryCache()
type BaseController struct {
beego.Controller
}
... ...
package controllers
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/user"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
"strings"
)
type UserController struct {
... ... @@ -65,6 +67,17 @@ func (this *UserController) ChangePhone() {
msg = m
return
}
if !CacheSms.IsExist(request.OldPhone) {
msg = protocol.NewMesage(1009) //验证码失效
return
}
if v := CacheSms.Get(request.OldPhone); v != nil {
if !strings.EqualFold(fmt.Sprintf("%v", v), strings.TrimSpace(request.CaptchaCertificate)) {
msg = protocol.NewMesage(1012) //验证码不一致
return
}
CacheSms.Delete(request.OldPhone)
}
header := this.GetRequestHeader(this.Ctx)
msg = protocol.NewReturnResponse(user.ChangePhone(header, request))
}
... ... @@ -85,6 +98,17 @@ func (this *UserController) ResetPassword() {
msg = m
return
}
if !CacheSms.IsExist(request.Phone) {
msg = protocol.NewMesage(1009) //验证码失效
return
}
if v := CacheSms.Get(request.Phone); v != nil {
if !strings.EqualFold(fmt.Sprintf("%v", v), strings.TrimSpace(request.CaptchaCertificate)) {
msg = protocol.NewMesage(1012) //验证码不一致
return
}
CacheSms.Delete(request.Phone)
}
header := this.GetRequestHeader(this.Ctx)
msg = protocol.NewReturnResponse(user.ResetPassword(header, request))
}
... ...
... ... @@ -15,6 +15,7 @@ func init() {
nsV1.Router("auth/refreshToken", &controllers.AuthController{}, "Post:RefreshToken")
nsV1.Router("auth/revoke", &controllers.AuthController{}, "Post:Revoke")
nsV1.Router("auth/updateDevice", &controllers.AuthController{}, "Post:UpdateDevice")
nsV1.Router("auth/checkSmsCod", &controllers.AuthController{}, "Post:AuthCheckSmsCode")
nsV1.Router("user/userInfo", &controllers.UserController{}, "Post:UserInfo")
nsV1.Router("user/checkSmsCode", &controllers.UserController{}, "Post:CheckSmsCode")
... ...
... ... @@ -78,3 +78,12 @@ type RevokeRequest struct {
}
type RevokeResponse struct {
}
/*AuthCheckSmsCode */
type AuthCheckSmsCodeRequest struct {
Phone string `json:"phone" valid:"Required;"`
Captcha string `json:"captcha" valid:"Required;"`
}
type AuthCheckSmsCodeResponse struct {
CaptchaCertificate string `json:"captchaCertificate"` //短信验证码通过凭证
}
... ...
... ... @@ -46,15 +46,21 @@ type CheckSmsCodeResponse struct {
type ChangePhoneRequest struct {
Phone string `json:"phone" valid:"Mobile"`
Captcha string `json:"captcha" valid:"Required"`
OldPhone string `json:"oldPhone" valid:"Required"`
CaptchaCertificate string `json:"captchaCertificate" valid:"Required"`
}
type ChangePhoneResponse struct {
}
/*ResetPassword */
type ResetPasswordRequest struct {
Captcha string `json:"captcha" valid:"Required"`
//Captcha string `json:"captcha" valid:"Required"`
NewPwd string `json:"newPwd" valid:"Required"`
ConfirmPwd string `json:"confirmPwd" valid:"Required"`
Phone string `json:"phone" valid:"Required"`
CaptchaCertificate string `json:"captchaCertificate" valid:"Required"`
}
type ResetPasswordResponse struct {
}
... ...