/
bind_weibo.go
75 lines (67 loc) · 1.56 KB
/
bind_weibo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package model
import (
"fmt"
"github.com/aiyi/go-user/db"
)
// 绑定微博(一般在认证后进行操作).
// 调用该函数前, 请确认:
// 1. 该用户存在并且 verified
// 2. 该用户未绑定微博
// 3. 该微博未绑定用户
func BindWeibo(userId int64, openid string) (err error) {
if err = removeFromCache(userId); err != nil {
return
}
if err = bindWeibo(userId, openid); err != nil {
return
}
return syncToCache(userId)
}
func bindWeibo(userId int64, openid string) (err error) {
para := struct {
UserId int64 `sqlx:"user_id"`
OpenId string `sqlx:"openid"`
BindType BindType `sqlx:"bind_type"`
}{
UserId: userId,
OpenId: openid,
BindType: BindTypeWeibo,
}
tx, err := db.GetDB().Beginx()
if err != nil {
return
}
// user_weibo 表增加一个 item
stmt1, err := tx.Prepare("insert into user_weibo(user_id, openid, verified) values(?, ?, 1)")
if err != nil {
tx.Rollback()
return
}
if _, err = stmt1.Exec(para.UserId, para.OpenId); err != nil {
tx.Rollback()
return
}
// user 更新 item
stmt2, err := tx.PrepareNamed("update user set bind_types = bind_types|:bind_type where id=:user_id and verified=1 and bind_types&:bind_type=0")
if err != nil {
tx.Rollback()
return
}
rslt2, err := stmt2.Exec(para)
if err != nil {
tx.Rollback()
return
}
rowsAffected2, err := rslt2.RowsAffected()
if err != nil {
tx.Rollback()
return
}
if rowsAffected2 != 1 {
err = fmt.Errorf("绑定微博 %s 到用户 %d 失败", para.OpenId, para.UserId)
tx.Rollback()
return
}
err = tx.Commit()
return
}