作者 yangfu

增加:密码修改,用户权限认证

... ... @@ -140,9 +140,11 @@ func RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshT
transactionContext, _ = factory.CreateTransactionContext(nil)
PartnerInfoService, _ = factory.CreatePartnerInfoRepositoryIn(transactionContext)
UsersRepository, _ = factory.CreateUsersRepository(transactionContext)
CompanyRepository, _ = factory.CreateCompanyRepository(transactionContext)
partnerInfo *domain.PartnerInfo
user *domain.Users
company *domain.Company
userId int64
)
... ... @@ -162,6 +164,11 @@ func RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshT
err = protocol.NewErrWithMessage(1, fmt.Errorf("jwt refrshToken (%v) valid", request.RefreshToken))
return
}
if company, err = CompanyRepository.FindOne(map[string]interface{}{"id": claim.CompanyId, "status": 1, "enable": 1}); err != nil || company == nil {
log.Error(err)
err = protocol.NewErrWithMessage(4140, err)
return
}
switch claim.AdminType {
case int(protocolx.AdminTypePartner):
... ...
... ... @@ -211,7 +211,11 @@ func getPartners(userId int64, request *protocolx.PartnersRequest, transactionCo
queryOption["districts"] = districts
}
if len(request.JoinWays) > 0 {
queryOption["joinWays"] = request.JoinWays
var joinWays []int64
for i := 0; i < len(request.JoinWays); i++ {
joinWays = append(joinWays, request.JoinWays[i].Type)
}
queryOption["joinWays"] = joinWays
}
if request.StartTime > 0 {
queryOption["startTime"] = request.StartTime / 1000
... ...
... ... @@ -12,8 +12,9 @@ import (
// 分红统计
func Statistics(header *protocol.RequestHeader, request *protocol.DividendStatisticsRequest) (rsp *protocol.DividendStatisticsResponse, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
OrderBaseResponsitory, _ = factory.CreateOrderBaseRepository(transactionContext)
transactionContext, _ = factory.CreateTransactionContext(nil)
OrderBaseResponsitory, _ = factory.CreateOrderBaseRepository(transactionContext)
BusinessBonusRepository, _ = factory.CreateBusinessBonusRepository(transactionContext)
)
if err = transactionContext.StartTransaction(); err != nil {
return nil, err
... ... @@ -33,6 +34,9 @@ func Statistics(header *protocol.RequestHeader, request *protocol.DividendStatis
}
bonusAll := AllBonusStatics(orderAll, 0)
bonusQuarters := QuartersBonusStatics(orderBetween, 0)
if bonus, e := BusinessBonusRepository.FindOne(map[string]interface{}{"partner_id": header.UserId, "isDisable": 1}); e == nil {
bonusAll.Receivable += bonus.Bonus
}
rsp = &protocol.DividendStatisticsResponse{}
rsp.Statistics = protocol.DividendStatistics{
Received: bonusAll.Received,
... ...
... ... @@ -7,6 +7,8 @@ import (
"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/infrastructure/pg/transaction"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/svr"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
protocolx "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol/auth"
... ... @@ -137,6 +139,13 @@ func ChangePhone(header *protocol.RequestHeader, request *protocol.ChangePhoneRe
defer func() {
transactionContext.RollbackTransaction()
}()
// 管理员不支持修改手机号
if header.AdminType == int(protocolx.AdminTypeManager) {
//if err=changeUserPhone(header.UserId,request.Phone,transactionContext);err!=nil{
// err = protocol.NewCustomMessage(1,err.Error())
//}
return
}
if partnerInfo, err = PartnerInfoService.FindOne(map[string]interface{}{"id": header.UserId}); err != nil {
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
... ... @@ -183,6 +192,12 @@ func ResetPassword(header *protocol.RequestHeader, request *protocol.ResetPasswo
err = protocol.NewErrWithMessage(2026)
return
}
if header.AdminType == int(protocolx.AdminTypeManager) {
if err = changeUserInfo(header.UserId, fmt.Sprintf("%v", header.SimNum), request.NewPwd, transactionContext); err != nil {
err = protocol.NewCustomMessage(1, err.Error())
}
return
}
if partnerInfo, err = PartnerInfoService.FindOne(map[string]interface{}{"id": header.UserId}); err != nil {
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
... ... @@ -226,6 +241,12 @@ func ChangePassword(header *protocol.RequestHeader, request *protocol.ChangePass
err = protocol.NewErrWithMessage(2026)
return
}
if header.AdminType == int(protocolx.AdminTypeManager) {
if err = changeUserPassword(header.UserId, request.NewPwd, request.OldPwd, fmt.Sprintf("%v", header.SimNum), transactionContext); err != nil {
err = protocol.NewCustomMessage(1, err.Error())
}
return
}
if strings.EqualFold(request.NewPwd, request.OldPwd) {
err = protocol.NewErrWithMessage(2030)
return
... ... @@ -368,3 +389,53 @@ func UserInfoV2(header *protocol.RequestHeader, request *protocol.UserInfoReques
err = transactionContext.CommitTransaction()
return
}
func changeUserInfo(userId int64, phone, password string, transactionContext *transaction.TransactionContext) (err error) {
var (
ucenterApiGateway = svr.NewHttplibUCenterApiServiceGateway()
UsersRepository, _ = factory.CreateUsersRepository(transactionContext)
user *domain.Users
)
if user, err = UsersRepository.FindOne(map[string]interface{}{"id": userId}); err != nil || user == nil {
log.Error(err)
err = fmt.Errorf("用户不存在")
return
}
_, err = ucenterApiGateway.UpdateUserPassword(user.OpenId, phone, password)
return
}
func changeUserPhone(userId int64, phone string, transactionContext *transaction.TransactionContext) (err error) {
var (
ucenterApiGateway = svr.NewHttplibUCenterApiServiceGateway()
UsersRepository, _ = factory.CreateUsersRepository(transactionContext)
user *domain.Users
)
if user, err = UsersRepository.FindOne(map[string]interface{}{"id": userId}); err != nil || user == nil {
log.Error(err)
err = fmt.Errorf("用户不存在")
return
}
if newUser, e := UsersRepository.FindOne(map[string]interface{}{"phone": phone}); e == nil || newUser != nil {
err = protocol.NewErrWithMessage(2029, err)
return
}
_, err = ucenterApiGateway.UpdateUserPassword(user.OpenId, phone, "")
if err != nil {
_, err = transactionContext.PgTx.Exec("update users set phone=? where id=?", phone, userId)
}
return
}
func changeUserPassword(userId int64, newPwd, oldPwd, phone string, transactionContext *transaction.TransactionContext) (err error) {
var (
ucenterApiGateway = svr.NewHttplibUCenterApiServiceGateway()
UsersRepository, _ = factory.CreateUsersRepository(transactionContext)
user *domain.Users
)
if user, err = UsersRepository.FindOne(map[string]interface{}{"id": userId}); err != nil || user == nil {
log.Error(err)
err = fmt.Errorf("用户不存在")
return
}
_, err = ucenterApiGateway.ChangePassword(phone, newPwd, oldPwd)
return
}
... ...
... ... @@ -11,6 +11,9 @@ var MMM_SMS_SERVICE_HOST = "https://sms.fjmaimaimai.com:9897"
var UCENTER_SERVICE_HOST = "https://suplus-ucenter-dev.fjmaimaimai.com"
var UCENTER_APP_KEY = "0c2c2a23dfc64ae230f5c54ab243ab52"
var BUSINESS_ADMIN_SERVICE_HOST = "http://suplus-business-admin-dev.fjmaimaimai.com"
var BUSINESS_ADMIN_PLATFORM_ID = "25" //合伙人模块
func init() {
if os.Getenv("LOG_LEVEL") != "" {
LOG_LEVEL = os.Getenv("LOG_LEVEL")
... ... @@ -18,4 +21,7 @@ func init() {
if os.Getenv("UCENTER_SERVICE_HOST") != "" {
UCENTER_SERVICE_HOST = os.Getenv("UCENTER_SERVICE_HOST")
}
if os.Getenv("BUSINESS_ADMIN_SERVICE_HOST") != "" {
BUSINESS_ADMIN_SERVICE_HOST = os.Getenv("BUSINESS_ADMIN_SERVICE_HOST")
}
}
... ...
... ... @@ -24,6 +24,8 @@ type Company struct {
UpdateAt time.Time `json:"updateAt"`
// 删除时间
DeleteAt time.Time `json:"deleteAt"`
// 是否开启合伙人模块,是否有效【1:有效】【2:无效】
Enable int8 `json:"enable"`
}
type CompanyRepository interface {
... ...
... ... @@ -44,6 +44,8 @@ type Users struct {
DeleteAt time.Time `json:"deleteAt"`
// 可查看的合伙人信息
AccessPartners []*PartnerInfo
// 1普通用户 2主管理员
AdminType int8 `json:"adminType"`
}
func (Users *Users) AccessPartnerIds() []int64 {
... ...
... ... @@ -2,6 +2,7 @@ package domain_service
import (
"github.com/tiptok/gocomm/xa/eda"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/constant"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
... ... @@ -54,8 +55,12 @@ func (svr *PgLoginService) ManagerLogin(phone string, password string) (err erro
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
var openid int64
ucenerSvr := http_gateway.NewHttplibUCenterApiServiceGateway()
_, err = ucenerSvr.ServerLogin(phone, password, 1)
openid, err = ucenerSvr.ServerLogin(phone, password, 1)
if err == nil && openid > 0 {
_, err = svr.transactionContext.PgTx.Exec("update users set open_id=? where phone=?", openid, phone)
}
return
}
... ... @@ -137,7 +142,8 @@ func (svr *PgLoginService) ManagerStaticInfo() (interface{}, error) {
return nil, nil
}
var (
companyList []*domain.Company
companyList []*domain.Company
adminApiGateway = http_gateway.NewHttplibBusinessAdminApiServiceGateway()
)
doGetCompanyIds := func() []int64 {
var companies []int64
... ... @@ -150,13 +156,23 @@ func (svr *PgLoginService) ManagerStaticInfo() (interface{}, error) {
var companys = make([]protocol.CompanyBase, 0)
for i := range companyList {
c := companyList[i]
//var user *domain.Users
//for j:=range svr.Users{
// if svr.Users[j].CompanyId==c.Id{
// user = svr.Users[j]
// break
// }
//}
//通过企业平台 校验模块权限
var user *domain.Users
for j := range svr.Users {
if svr.Users[j].CompanyId == c.Id {
user = svr.Users[j]
break
}
}
if user != nil {
if code, e := adminApiGateway.UserAuth(user.Id, constant.BUSINESS_ADMIN_PLATFORM_ID); e != nil || code != 0 {
log.Debug("【检查权限】", svr.Phone, "【公司】", c.Id, user.Id, code, e.Error())
continue
} else {
log.Debug("【检查权限】", svr.Phone, "【公司】", c.Id, user.Id, code, e)
}
}
item := newCompanyBase(c)
companys = append(companys, item)
}
... ...
... ... @@ -25,4 +25,6 @@ type Company struct {
UpdateAt time.Time
// 删除时间
DeleteAt time.Time
// 是否开启合伙人模块,是否有效【1:有效】【2:无效】
Enable int8
}
... ...
... ... @@ -48,4 +48,6 @@ type Users struct {
DeleteAt time.Time
// 可查看的合伙人信息
AccessPartners []*domain.PartnerInfo
// 1普通用户 2主管理员
AdminType int8
}
... ...
... ... @@ -49,8 +49,10 @@ func (repository *CompanyRepository) FindOne(queryOptions map[string]interface{}
CompanyModel := new(models.Company)
query := NewQuery(tx.Model(CompanyModel), queryOptions)
query.SetWhere(`"company".id = ?`, "id")
query.SetWhere(`"company".status = ?`, "status")
query.SetWhere(`"company".enable = ?`, "enable")
if err := query.First(); err != nil {
return nil, query.HandleError(err, "没有此订单")
return nil, query.HandleError(err, "没有此公司")
}
if CompanyModel.Id == 0 {
return nil, nil
... ...
package svr
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/constant"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
"strconv"
"strings"
"time"
)
type HttplibBusinessAdminApiServiceGateway struct {
httplibBaseServiceGateway
}
// 服务登录
func (serviceGateway *HttplibBusinessAdminApiServiceGateway) UserAuth(userId int64, platformId string) (int, error) {
url := strings.Join([]string{serviceGateway.baseURL, "auth", "get-user-auth"}, "/")
request := serviceGateway.createRequest(url, "post")
request.Header("appKey", constant.UCENTER_APP_KEY)
options := make(map[string]interface{})
options["userId"] = fmt.Sprintf("%v", userId)
options["platformId"] = fmt.Sprintf("%v", platformId)
request.JSONBody(options)
response := make(map[string]interface{})
err := request.ToJSON(&response)
if err != nil {
log.Error("Service Gateway Fail:", err)
return 0, err
}
return serviceGateway.handlerError(response)
}
func (serviceGateway *HttplibBusinessAdminApiServiceGateway) handlerError(in map[string]interface{}) (int, error) {
var rspCode int
var err error
if code, ok := in["code"]; ok {
rspCode, _ = strconv.Atoi(fmt.Sprintf("%v", code))
} else {
err = fmt.Errorf("网关解析错误")
}
if msg, ok := in["msg"]; ok {
msg := msg.(string)
if rspCode != 0 && len(msg) > 0 {
err = fmt.Errorf(msg)
}
}
return rspCode, err
}
func NewHttplibBusinessAdminApiServiceGateway() *HttplibBusinessAdminApiServiceGateway {
return &HttplibBusinessAdminApiServiceGateway{
httplibBaseServiceGateway: httplibBaseServiceGateway{
baseURL: constant.BUSINESS_ADMIN_SERVICE_HOST,
connectTimeout: 100 * time.Second,
readWriteTimeout: 30 * time.Second,
},
}
}
... ...
... ... @@ -14,7 +14,7 @@ type HttplibUCenterApiServiceGateway struct {
}
// 服务登录
func (serviceGateway *HttplibUCenterApiServiceGateway) ServerLogin(phone, password string, loginType int) (int, error) {
func (serviceGateway *HttplibUCenterApiServiceGateway) ServerLogin(phone, password string, loginType int) (int64, error) {
url := strings.Join([]string{serviceGateway.baseURL, "auth", "serverLogin"}, "/")
request := serviceGateway.createRequest(url, "post")
request.Header("appKey", constant.UCENTER_APP_KEY)
... ... @@ -24,28 +24,56 @@ func (serviceGateway *HttplibUCenterApiServiceGateway) ServerLogin(phone, passwo
options["type"] = loginType
request.JSONBody(options)
response := make(map[string]interface{})
//data,_:=request.Bytes()
//fmt.Println(string(data))
err := request.ToJSON(&response)
var openid int64
if err != nil {
log.Error("Service Gateway Fail:", err)
return 0, err
}
return serviceGateway.handlerError(response)
if data, ok := response["data"]; ok {
if data, ok := data.(map[string]interface{}); ok {
if data, ok := data["id"]; ok {
openid = int64(data.(float64))
//openid,err=strconv.ParseInt(fmt.Sprintf("%v",data.(float64)),10,64)
}
}
}
_, err = serviceGateway.handlerError(response)
return openid, err
}
// 修改密码
func (serviceGateway *HttplibUCenterApiServiceGateway) UpdateUserPassword(uid int64, phone, password string) (int, error) {
url := strings.Join([]string{serviceGateway.baseURL, "users", fmt.Sprintf("%v", uid)}, "/")
request := serviceGateway.createRequest(url, "put")
//request.Header("appKey", constant.UCENTER_APP_KEY)
options := make(map[string]interface{})
if len(phone) > 0 {
options["phone"] = strings.TrimSpace(phone)
}
if len(password) > 0 {
options["password"] = strings.TrimSpace(password)
}
request.JSONBody(options)
response := make(map[string]interface{})
err := request.ToJSON(&response)
if err != nil {
log.Error("Service Gateway Fail:", err)
return 0, err
}
return serviceGateway.handlerError(response)
}
func (serviceGateway *HttplibUCenterApiServiceGateway) ChangePassword(phone, newPwd, oldPwd string) (int, error) {
url := strings.Join([]string{serviceGateway.baseURL, "users", "changePassword"}, "/")
request := serviceGateway.createRequest(url, "post")
request.Header("appKey", constant.UCENTER_APP_KEY)
options := make(map[string]interface{})
options["phone"] = strings.TrimSpace(phone)
options["password"] = strings.TrimSpace(password)
options["newPassword"] = strings.TrimSpace(newPwd)
//options["confirmPwd"] = strings.TrimSpace(confirmPwd)
options["password"] = strings.TrimSpace(oldPwd)
request.JSONBody(options)
response := make(map[string]interface{})
//data,_:=request.Bytes()
//fmt.Println(string(data))
err := request.ToJSON(&response)
if err != nil {
log.Error("Service Gateway Fail:", err)
... ...
... ... @@ -67,19 +67,19 @@ 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
}
} else {
msg = protocol.NewMesage(1009) //验证码不一致
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
// }
//} else {
// msg = protocol.NewMesage(1009) //验证码不一致
// return
//}
header := this.GetRequestHeader(this.Ctx)
data, err := user.ChangePhone(header, request)
if err == nil {
... ...
... ... @@ -12,7 +12,7 @@ type PartnersRequest struct {
// 区域(空或不传,即所有区域)
Districts []Districts `json:"districts"`
// 合作类型(空或不传,即所有类型)
JoinWays []int64 `json:"joinWays"`
JoinWays []JoinWays `json:"joinWays"`
// 分红排序(0.从多到少 1.从少到多)
SortBy int `json:"sortBy"`
}
... ...