wangfei 6 年前
父节点
当前提交
7f8100e378

+ 32
- 1
controllers/component.go 查看文件

109
 		}
109
 		}
110
 		beego.Error(wxclient)
110
 		beego.Error(wxclient)
111
 		utils.AppendWxClient(wxclient)
111
 		utils.AppendWxClient(wxclient)
112
+
113
+		// 获取微信信息
114
+		info, err := wxclient.GetWechatInfo()
115
+		if err != nil {
116
+			utils.LogError("获取微信信息失败")
117
+			c.ResponseRaw([]byte("success"))
118
+		}
119
+		authorizerInfo := (info["authorizer_info"]).(map[string]interface{})
120
+
112
 		// 插入数据库
121
 		// 插入数据库
113
 		mjson, _ := json.Marshal(msg)
122
 		mjson, _ := json.Marshal(msg)
114
 		var conf = model.SysWechatConf{
123
 		var conf = model.SysWechatConf{
115
 			Appid:             msg["AuthorizerAppid"],
124
 			Appid:             msg["AuthorizerAppid"],
116
 			AuthorizationCode: msg["AuthorizationCode"],
125
 			AuthorizationCode: msg["AuthorizationCode"],
117
 			AuthorizationInfo: string(mjson),
126
 			AuthorizationInfo: string(mjson),
127
+			WxNikeName:        authorizerInfo["nikd_name"].(string),
128
+			HeadImg:           authorizerInfo["head_img"].(string),
129
+			UserName:          authorizerInfo["user_name"].(string),
130
+			PrincipalName:     authorizerInfo["principal_name"].(string),
131
+			QrcodeUrl:         authorizerInfo["qrcode_url"].(string),
118
 		}
132
 		}
119
 
133
 
120
-		err := c.wechatServ.SaveWechatConf(conf)
134
+		err = c.wechatServ.SaveWechatConf(conf)
121
 		if err != nil {
135
 		if err != nil {
122
 			utils.LogError("保存微信授权信息失败:", err)
136
 			utils.LogError("保存微信授权信息失败:", err)
123
 			c.ResponseRaw([]byte("success"))
137
 			c.ResponseRaw([]byte("success"))
217
 		"code":  code,
231
 		"code":  code,
218
 	})
232
 	})
219
 }
233
 }
234
+
235
+// GetWechatMenuByAppID 根据appid获取微信详情
236
+func (c *WechatController) GetWechatInfoByAppID() {
237
+	appid := c.GetString(":appid")
238
+	beego.Error(utils.Component)
239
+	wxclient, err := utils.Component.GetWxClient(appid)
240
+	if err != nil {
241
+		utils.LogError("获取微信信息失败: " + err.Error())
242
+		c.ResponseError(err)
243
+	}
244
+	info, err := wxclient.GetWechatInfo()
245
+	if err != nil {
246
+		utils.LogError("获取微信详情失败: " + err.Error())
247
+		c.ResponseError(err)
248
+	}
249
+	c.ResponseJSON(info)
250
+}

+ 8
- 0
controllers/sysorg/sysorg.go 查看文件

61
 	c.ResponseJSON("删除成功")
61
 	c.ResponseJSON("删除成功")
62
 
62
 
63
 }
63
 }
64
+
65
+// OrgBindWechat 项目绑定微信
66
+func (c *SysorgController) OrgBindWechat() {
67
+	orgid := c.GetString(":orgid")
68
+	code := c.GetString(":code")
69
+
70
+	c.dao.BindWechat(orgid, code)
71
+}

+ 5
- 0
models/model/sys_wechat_conf.go 查看文件

9
 	Appid             string    `xorm:"VARCHAR(50)"`
9
 	Appid             string    `xorm:"VARCHAR(50)"`
10
 	AuthorizationCode string    `xorm:"VARCHAR(100)"`
10
 	AuthorizationCode string    `xorm:"VARCHAR(100)"`
11
 	RefreshToken      string    `xorm:"VARCHAR(100)"`
11
 	RefreshToken      string    `xorm:"VARCHAR(100)"`
12
+	WxNikeName        string    `xorm:"VARCHAR(100)"`
13
+	HeadImg           string    `xorm:"TEXT"`
14
+	UserName          string    `xorm:"VARCHAR(100)"`
15
+	PrincipalName     string    `xorm:"VARCHAR(200)"`
16
+	QrcodeUrl         string    `xorm:"TEXT"`
12
 	Token             string    `xorm:"VARCHAR(100)"`
17
 	Token             string    `xorm:"VARCHAR(100)"`
13
 	Remark            string    `xorm:"TEXT"`
18
 	Remark            string    `xorm:"TEXT"`
14
 	Status            int       `xorm:"SMALLINT(6)"`
19
 	Status            int       `xorm:"SMALLINT(6)"`

+ 3
- 4
models/wechat.go 查看文件

30
 		d = string(subd)
30
 		d = string(subd)
31
 	}
31
 	}
32
 
32
 
33
-	beego.Error("过期时间:", d)
34
 	var cert = map[string]string{
33
 	var cert = map[string]string{
35
 		"appid":                 conf.Appid,
34
 		"appid":                 conf.Appid,
36
 		"aeskey":                conf.Aeskey,
35
 		"aeskey":                conf.Aeskey,
56
 
55
 
57
 	expire := token["expire_in"].(int64)
56
 	expire := token["expire_in"].(int64)
58
 	if expire > 0 {
57
 	if expire > 0 {
59
-		beego.Error(time.Now())
60
 		expriesTime := time.Now().Local().Add(time.Duration(expire) * time.Second)
58
 		expriesTime := time.Now().Local().Add(time.Duration(expire) * time.Second)
61
-		beego.Error(expriesTime)
62
 		conf.ExpiresDate = expriesTime
59
 		conf.ExpiresDate = expriesTime
63
 		sql := `update sys_component_conf set token=?,expires_date=?`
60
 		sql := `update sys_component_conf set token=?,expires_date=?`
64
 		_, err := DBEngine.Exec(sql, conf.Token, conf.ExpiresDate)
61
 		_, err := DBEngine.Exec(sql, conf.Token, conf.ExpiresDate)
77
 // InitWechat 初始化微信
74
 // InitWechat 初始化微信
78
 func InitWechat() {
75
 func InitWechat() {
79
 	var wxconfs []model.SysWechatConf
76
 	var wxconfs []model.SysWechatConf
80
-	err := DBEngine.Where("status=1").Find(&wxconfs)
77
+	err := DBEngine.Where("status>?", STATUS_DEL).Find(&wxconfs)
81
 	if err != nil {
78
 	if err != nil {
82
 		utils.LogError("初始化微信失败:", err)
79
 		utils.LogError("初始化微信失败:", err)
83
 		return
80
 		return
84
 	}
81
 	}
82
+
85
 	for _, conf := range wxconfs {
83
 	for _, conf := range wxconfs {
86
 		var cert = map[string]string{
84
 		var cert = map[string]string{
87
 			"authorizer_access_token":  conf.Token,
85
 			"authorizer_access_token":  conf.Token,
90
 			"appid":                    conf.Appid,
88
 			"appid":                    conf.Appid,
91
 		}
89
 		}
92
 		client := utils.WechatInit(cert, UpdateToken)
90
 		client := utils.WechatInit(cert, UpdateToken)
91
+		beego.Error(client)
93
 		utils.AppendWxClient(client)
92
 		utils.AppendWxClient(client)
94
 	}
93
 	}
95
 }
94
 }

+ 7
- 0
models/wechat/wechat.go 查看文件

93
 	}...).Where("appid = ?", conf.Appid).Update(conf)
93
 	}...).Where("appid = ?", conf.Appid).Update(conf)
94
 	return err
94
 	return err
95
 }
95
 }
96
+
97
+// GetWxByCode 根据code获取微信信息
98
+func (m *WechatDAO) GetWxByCode(code string) (*model.SysWechatConf, error) {
99
+	var conf model.SysWechatConf
100
+	_, err := m.db.Where("authorization_code=?", code).And("status>?", models.STATUS_DEL).Get(&conf)
101
+	return &conf, err
102
+}

+ 2
- 0
routers/router.go 查看文件

72
 		beego.NSRouter("/:appid/callback", &controllers.WechatController{}, "get:WxReceive"),
72
 		beego.NSRouter("/:appid/callback", &controllers.WechatController{}, "get:WxReceive"),
73
 		beego.NSRouter("/:appid/callback", &controllers.WechatController{}, "post:WxReceive"),
73
 		beego.NSRouter("/:appid/callback", &controllers.WechatController{}, "post:WxReceive"),
74
 		beego.NSRouter("/authcode", &controllers.WechatController{}, "get:GetPreAuthCode"),
74
 		beego.NSRouter("/authcode", &controllers.WechatController{}, "get:GetPreAuthCode"),
75
+		beego.NSRouter("/wechatinfo/:appid", &controllers.WechatController{}, "get:GetWechatInfoByAppID"),
76
+		// beego.NSRouter("/menu/:appid", &controllers.WechatController{}, "get:GetWechatMenuByAppID"),
75
 	)
77
 	)
76
 
78
 
77
 	beego.AddNamespace(ns)
79
 	beego.AddNamespace(ns)

+ 22
- 6
service/sysorg/sysorg.go 查看文件

4
 	"errors"
4
 	"errors"
5
 	"wechat-conf/models/model"
5
 	"wechat-conf/models/model"
6
 	"wechat-conf/models/sysorg"
6
 	"wechat-conf/models/sysorg"
7
+	"wechat-conf/models/wechat"
7
 	"wechat-conf/models/wechatmenu"
8
 	"wechat-conf/models/wechatmenu"
8
 	"wechat-conf/service"
9
 	"wechat-conf/service"
9
 	"wechat-conf/utils"
10
 	"wechat-conf/utils"
11
 
12
 
12
 // SysorgServ 用户
13
 // SysorgServ 用户
13
 type SysorgServ struct {
14
 type SysorgServ struct {
14
-	ctx  *utils.Context
15
-	dao  *sysorg.SysorgDAO
16
-	mdao *wechatmenu.WechatMenuDAO
15
+	ctx       *utils.Context
16
+	dao       *sysorg.SysorgDAO
17
+	mdao      *wechatmenu.WechatMenuDAO
18
+	wechatdao *wechat.WechatDAO
17
 }
19
 }
18
 
20
 
19
 // NewSysorgServ 初始化
21
 // NewSysorgServ 初始化
20
 func NewSysorgServ(ctx *utils.Context) *SysorgServ {
22
 func NewSysorgServ(ctx *utils.Context) *SysorgServ {
21
 	return &SysorgServ{
23
 	return &SysorgServ{
22
-		ctx:  ctx,
23
-		dao:  sysorg.NewSysorgDAO(ctx),
24
-		mdao: wechatmenu.NewWechatMenuDAO(ctx),
24
+		ctx:       ctx,
25
+		dao:       sysorg.NewSysorgDAO(ctx),
26
+		mdao:      wechatmenu.NewWechatMenuDAO(ctx),
27
+		wechatdao: wechat.NewWechatDAO(ctx),
25
 	}
28
 	}
26
 }
29
 }
27
 func (s *SysorgServ) GetOrgList(page, pageSize int) (map[string]interface{}, error) {
30
 func (s *SysorgServ) GetOrgList(page, pageSize int) (map[string]interface{}, error) {
78
 	}
81
 	}
79
 	return nil
82
 	return nil
80
 }
83
 }
84
+
85
+// BindWechat 绑定微信
86
+func (s *SysorgServ) BindWechat(orgid, code string) error {
87
+	wechatConf, err := s.wechatdao.GetWxByCode(code)
88
+	if err != nil {
89
+		return err
90
+	}
91
+	if wechatConf == nil && wechatConf.Appid == "" {
92
+		return errors.New("没有获取到微信信息")
93
+	}
94
+
95
+	return nil
96
+}