作者 唐旭辉

更新

1 package domain 1 package domain
2 2
3 import ( 3 import (
4 - "fmt"  
5 "time" 4 "time"
6 ) 5 )
7 6
@@ -49,25 +48,14 @@ type PartnerInfo struct { @@ -49,25 +48,14 @@ type PartnerInfo struct {
49 48
50 func (p *PartnerInfo) GetPartnerCategory() map[int]string { 49 func (p *PartnerInfo) GetPartnerCategory() map[int]string {
51 categoryMap := map[int]string{} 50 categoryMap := map[int]string{}
52 - for k, v := range partnerCategoryMap {  
53 - //合伙类别 按二进制位区分  
54 - if (p.PartnerCategory & k) > 0 {  
55 - categoryMap[k] = v  
56 - } 51 + if v, ok := partnerCategoryMap[p.PartnerCategory]; ok {
  52 + categoryMap[p.PartnerCategory] = v
57 } 53 }
58 return categoryMap 54 return categoryMap
59 } 55 }
60 56
61 -func (p *PartnerInfo) SetPartnerCategory(category []int) error {  
62 - n := 0  
63 - for _, v := range category {  
64 - if _, ok := partnerCategoryMap[v]; !ok {  
65 - return fmt.Errorf("未知的合伙人类型:%d", v)  
66 - }  
67 - n += v  
68 - }  
69 - p.PartnerCategory = n  
70 - return nil 57 +func (p *PartnerInfo) IsUsable() bool {
  58 + return p.Status == PARTNER_STATUS_YES
71 } 59 }
72 60
73 type PartnerFindOneQuery struct { 61 type PartnerFindOneQuery struct {
  1 +package domain
  2 +
  3 +import "time"
  4 +
  5 +//用户是否可用状态:【1:正常】【 2:禁用】
  6 +const (
  7 + userStatusUsable int8 = 1
  8 + userStatusUnusable int8 = 2
  9 +)
  10 +
  11 +//用户是否是主管 :【1:是主管】【 2:不是主管】
  12 +const (
  13 + userIsCharge int8 = 1
  14 + userIsNotCharge int8 = 2
  15 +)
  16 +
  17 +//Users 企业平台的用户
  18 +type Users struct {
  19 + Id int64 //用户id
  20 + CompanyId int64 //公司id
  21 + OpenId int64 //统一用户中心
  22 + Name string //用户名称
  23 + Sex int8 //性别:【0:未知】【1:男】【2:女】
  24 + JobNum string //工号
  25 + Phone string //手机号,同账号
  26 + PrivatePhone string //私人手机号
  27 + Email string //邮件
  28 + ExtensionNum string //分机号
  29 + EntryTime time.Time //入职时间
  30 + Workspace string //工作地
  31 + Status int8 //状态:【1:正常】【 2:禁用】
  32 + Avatar string ///头像
  33 + Remarks string //备注
  34 + ChargeStatus int8 //是否为当前公司主管 【1:是】【2:否】
  35 + CreateAt time.Time
  36 + UpdateAt time.Time
  37 + Permission []AdminPermissionBase //权限
  38 +}
  39 +
  40 +//IsUsable 用户是否可用
  41 +func (u Users) IsUsable() bool {
  42 + return u.Status == userStatusUsable
  43 +}
  44 +
  45 +//IsCharge 用户是否是主管
  46 +func (u Users) IsCharge() bool {
  47 + return u.ChargeStatus == userIsCharge
  48 +}
  1 +package dao
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
  7 +)
  8 +
  9 +type UsersDao struct {
  10 + transactionContext *transaction.TransactionContext
  11 +}
  12 +
  13 +func NewUserDao(transactionContext *transaction.TransactionContext) (*UsersDao, error) {
  14 + if transactionContext == nil {
  15 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  16 + } else {
  17 + return &UsersDao{
  18 + transactionContext: transactionContext,
  19 + }, nil
  20 + }
  21 +}
1 package service_gateway 1 package service_gateway
2 2
3 import ( 3 import (
  4 + "bytes"
4 "crypto/sha1" 5 "crypto/sha1"
  6 + "encoding/json"
5 "fmt" 7 "fmt"
  8 + "io/ioutil"
6 "net/http" 9 "net/http"
  10 + "net/url"
7 "time" 11 "time"
8 12
9 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/constant" 13 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/constant"
10 ) 14 )
11 15
  16 +type UCenterCommonMsg struct {
  17 + Code int `json:"code"`
  18 + Msg string `json:"msg"`
  19 +}
  20 +
  21 +func (msg UCenterCommonMsg) IsOK() error {
  22 + if msg.Code != 0 {
  23 + return fmt.Errorf("统一用户中心响应数据异常,code:%d,msg:%s", msg.Code, msg.Msg)
  24 + }
  25 + return nil
  26 +}
  27 +
12 type MmmUserCenterServiceGateway struct { 28 type MmmUserCenterServiceGateway struct {
13 - httplibBaseServiceGateway 29 + baseURL string
14 } 30 }
15 31
16 func NewMmmUserCenterServiceGateway() *MmmUserCenterServiceGateway { 32 func NewMmmUserCenterServiceGateway() *MmmUserCenterServiceGateway {
17 return &MmmUserCenterServiceGateway{ 33 return &MmmUserCenterServiceGateway{
18 - httplibBaseServiceGateway{  
19 - baseURL: constant.UCENTER_HOST,  
20 - connectTimeout: 100 * time.Second,  
21 - readWriteTimeout: 30 * time.Second,  
22 - }} 34 + baseURL: constant.UCENTER_HOST,
  35 + }
23 } 36 }
24 37
25 func (gateway MmmUserCenterServiceGateway) buildHeader() http.Header { 38 func (gateway MmmUserCenterServiceGateway) buildHeader() http.Header {
@@ -37,9 +50,34 @@ func (gateway MmmUserCenterServiceGateway) buildHeader() http.Header { @@ -37,9 +50,34 @@ func (gateway MmmUserCenterServiceGateway) buildHeader() http.Header {
37 return h 50 return h
38 } 51 }
39 52
  53 +func (gateway MmmUserCenterServiceGateway) httpDo(reqURL string, mathod string, bodyData interface{}) ([]byte, error) {
  54 + httpclient := http.Client{
  55 + Timeout: 60 * time.Second, //请求超时时间60秒
  56 + }
  57 + bt := &bytes.Buffer{}
  58 + if bodyData != nil {
  59 + enc := json.NewEncoder(bt)
  60 + enc.Encode(bodyData)
  61 + }
  62 + req, err := http.NewRequest(mathod, reqURL, bt)
  63 + if err != nil {
  64 + return nil, err
  65 + }
  66 + req.Header = gateway.buildHeader()
  67 + resp, err := httpclient.Do(req)
  68 + if err != nil {
  69 + return nil, err
  70 + }
  71 + defer resp.Body.Close()
  72 + body, err := ioutil.ReadAll(resp.Body)
  73 + if err != nil {
  74 + return nil, err
  75 + }
  76 + return body, nil
  77 +}
  78 +
40 type ResponseLogin struct { 79 type ResponseLogin struct {
41 - Code int `json:"code"`  
42 - Msg string `json:"msg"` 80 + UCenterCommonMsg
43 Data struct { 81 Data struct {
44 Id int64 `json:"id"` //统一用户中心的id,对应本系统中users表的open_id 82 Id int64 `json:"id"` //统一用户中心的id,对应本系统中users表的open_id
45 Phone string `json:"phone"` 83 Phone string `json:"phone"`
@@ -53,13 +91,18 @@ type ResponseLogin struct { @@ -53,13 +91,18 @@ type ResponseLogin struct {
53 } `json:"data"` 91 } `json:"data"`
54 } 92 }
55 93
  94 +//RequestUCenterLoginBySecret 使用密钥方式登录统一用户中心
56 func (gateway MmmUserCenterServiceGateway) RequestUCenterLoginBySecret(secret string) (*ResponseLogin, error) { 95 func (gateway MmmUserCenterServiceGateway) RequestUCenterLoginBySecret(secret string) (*ResponseLogin, error) {
57 - // param := map[string]interface{}{  
58 - // "type": 3, //登录方式 固定值  
59 - // "secret": url.QueryEscape(secret), //必要的转换  
60 - // }  
61 - // url := "/auth/serverLogin"  
62 - // httpRequest := gateway.createRequest(url, "post")  
63 - // httpRequest.  
64 - return nil, nil 96 + param := map[string]interface{}{
  97 + "type": 3, //登录方式 固定值
  98 + "secret": url.QueryEscape(secret), //必要的转换
  99 + }
  100 + url := constant.UCENTER_HOST + "/auth/serverLogin"
  101 + byteData, err := gateway.httpDo(url, "post", param)
  102 + if err != nil {
  103 + return nil, err
  104 + }
  105 + respData := &ResponseLogin{}
  106 + err = json.Unmarshal(byteData, respData)
  107 + return respData, err
65 } 108 }