作者 唐旭辉

更新

package domain
import (
"fmt"
"time"
)
... ... @@ -49,25 +48,14 @@ type PartnerInfo struct {
func (p *PartnerInfo) GetPartnerCategory() map[int]string {
categoryMap := map[int]string{}
for k, v := range partnerCategoryMap {
//合伙类别 按二进制位区分
if (p.PartnerCategory & k) > 0 {
categoryMap[k] = v
}
if v, ok := partnerCategoryMap[p.PartnerCategory]; ok {
categoryMap[p.PartnerCategory] = v
}
return categoryMap
}
func (p *PartnerInfo) SetPartnerCategory(category []int) error {
n := 0
for _, v := range category {
if _, ok := partnerCategoryMap[v]; !ok {
return fmt.Errorf("未知的合伙人类型:%d", v)
}
n += v
}
p.PartnerCategory = n
return nil
func (p *PartnerInfo) IsUsable() bool {
return p.Status == PARTNER_STATUS_YES
}
type PartnerFindOneQuery struct {
... ...
package domain
import "time"
//用户是否可用状态:【1:正常】【 2:禁用】
const (
userStatusUsable int8 = 1
userStatusUnusable int8 = 2
)
//用户是否是主管 :【1:是主管】【 2:不是主管】
const (
userIsCharge int8 = 1
userIsNotCharge int8 = 2
)
//Users 企业平台的用户
type Users struct {
Id int64 //用户id
CompanyId int64 //公司id
OpenId int64 //统一用户中心
Name string //用户名称
Sex int8 //性别:【0:未知】【1:男】【2:女】
JobNum string //工号
Phone string //手机号,同账号
PrivatePhone string //私人手机号
Email string //邮件
ExtensionNum string //分机号
EntryTime time.Time //入职时间
Workspace string //工作地
Status int8 //状态:【1:正常】【 2:禁用】
Avatar string ///头像
Remarks string //备注
ChargeStatus int8 //是否为当前公司主管 【1:是】【2:否】
CreateAt time.Time
UpdateAt time.Time
Permission []AdminPermissionBase //权限
}
//IsUsable 用户是否可用
func (u Users) IsUsable() bool {
return u.Status == userStatusUsable
}
//IsCharge 用户是否是主管
func (u Users) IsCharge() bool {
return u.ChargeStatus == userIsCharge
}
... ...
package dao
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)
type UsersDao struct {
transactionContext *transaction.TransactionContext
}
func NewUserDao(transactionContext *transaction.TransactionContext) (*UsersDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &UsersDao{
transactionContext: transactionContext,
}, nil
}
}
... ...
package service_gateway
import (
"bytes"
"crypto/sha1"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/constant"
)
type UCenterCommonMsg struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
func (msg UCenterCommonMsg) IsOK() error {
if msg.Code != 0 {
return fmt.Errorf("统一用户中心响应数据异常,code:%d,msg:%s", msg.Code, msg.Msg)
}
return nil
}
type MmmUserCenterServiceGateway struct {
httplibBaseServiceGateway
baseURL string
}
func NewMmmUserCenterServiceGateway() *MmmUserCenterServiceGateway {
return &MmmUserCenterServiceGateway{
httplibBaseServiceGateway{
baseURL: constant.UCENTER_HOST,
connectTimeout: 100 * time.Second,
readWriteTimeout: 30 * time.Second,
}}
}
}
func (gateway MmmUserCenterServiceGateway) buildHeader() http.Header {
... ... @@ -37,9 +50,34 @@ func (gateway MmmUserCenterServiceGateway) buildHeader() http.Header {
return h
}
func (gateway MmmUserCenterServiceGateway) httpDo(reqURL string, mathod string, bodyData interface{}) ([]byte, error) {
httpclient := http.Client{
Timeout: 60 * time.Second, //请求超时时间60秒
}
bt := &bytes.Buffer{}
if bodyData != nil {
enc := json.NewEncoder(bt)
enc.Encode(bodyData)
}
req, err := http.NewRequest(mathod, reqURL, bt)
if err != nil {
return nil, err
}
req.Header = gateway.buildHeader()
resp, err := httpclient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
type ResponseLogin struct {
Code int `json:"code"`
Msg string `json:"msg"`
UCenterCommonMsg
Data struct {
Id int64 `json:"id"` //统一用户中心的id,对应本系统中users表的open_id
Phone string `json:"phone"`
... ... @@ -53,13 +91,18 @@ type ResponseLogin struct {
} `json:"data"`
}
//RequestUCenterLoginBySecret 使用密钥方式登录统一用户中心
func (gateway MmmUserCenterServiceGateway) RequestUCenterLoginBySecret(secret string) (*ResponseLogin, error) {
// param := map[string]interface{}{
// "type": 3, //登录方式 固定值
// "secret": url.QueryEscape(secret), //必要的转换
// }
// url := "/auth/serverLogin"
// httpRequest := gateway.createRequest(url, "post")
// httpRequest.
return nil, nil
param := map[string]interface{}{
"type": 3, //登录方式 固定值
"secret": url.QueryEscape(secret), //必要的转换
}
url := constant.UCENTER_HOST + "/auth/serverLogin"
byteData, err := gateway.httpDo(url, "post", param)
if err != nil {
return nil, err
}
respData := &ResponseLogin{}
err = json.Unmarshal(byteData, respData)
return respData, err
}
... ...