-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_login.go
206 lines (200 loc) · 5.86 KB
/
user_login.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package BaseWeixinApp
import (
"errors"
"fmt"
BaseQiniu "github.com/fotomxq/weeekj_core/v5/base/qiniu"
CoreHttp "github.com/fotomxq/weeekj_core/v5/core/http"
CoreSQLFrom "github.com/fotomxq/weeekj_core/v5/core/sql/from"
UserCore "github.com/fotomxq/weeekj_core/v5/user/core"
UserLogin2 "github.com/fotomxq/weeekj_core/v5/user/login2"
"github.com/tidwall/gjson"
"strings"
"time"
)
// ArgsUserLogin 用户登陆和授权接口处理参数
type ArgsUserLogin struct {
//组织ID
// 可以留空,则表明为平台
OrgID int64 `db:"org_id" json:"orgID" check:"id" empty:"true"`
//编码
Code string `json:"code"`
//推荐人手机号
ReferrerNationCode string `db:"referrer_nation_code" json:"referrerNationCode" check:"nationCode" empty:"true"`
ReferrerPhone string `json:"referrerPhone" check:"phone" empty:"true"`
}
// UserLogin 用户登陆和授权接口处理
func UserLogin(args *ArgsUserLogin) (isNewUser bool, userData UserCore.FieldsUserType, errCode string, err error) {
//获取openID等数据
var openIDData DataGetOpenIDByCode
openIDData, errCode, err = GetOpenIDByCode(args.OrgID, args.Code)
if err != nil {
return
}
//检查用户是否存在?
var waitCheckLogins []UserCore.FieldsUserLoginType
if openIDData.UnionID != "" {
waitCheckLogins = append(waitCheckLogins, UserCore.FieldsUserLoginType{
Mark: "weixin_app_unionid",
Val: openIDData.UnionID,
Config: "",
})
}
if openIDData.OpenID != "" {
waitCheckLogins = append(waitCheckLogins, UserCore.FieldsUserLoginType{
Mark: "weixin_app_openid",
Val: openIDData.OpenID,
Config: "",
})
}
userData, errCode, err = UserCore.MargeCheckLogins(args.OrgID, waitCheckLogins)
if err != nil {
if errCode == "err_user_no_exist" {
err = nil
errCode = ""
} else {
return
}
} else {
//用户存在,注册成功反馈
updateUserAvatarByURL(openIDData.Headimgurl, userData.ID)
return
}
//注册新的用户
var logins []UserCore.FieldsUserLoginType
if openIDData.UnionID != "" {
logins = append(logins, UserCore.FieldsUserLoginType{
Mark: "weixin_app_unionid",
Val: openIDData.UnionID,
Config: "",
})
}
if openIDData.OpenID != "" {
logins = append(logins, UserCore.FieldsUserLoginType{
Mark: "weixin_app_openid",
Val: openIDData.OpenID,
Config: "",
})
}
userData, _, err = UserLogin2.CreateUser(&UserCore.ArgsCreateUser{
OrgID: args.OrgID,
Name: openIDData.Nickname,
Password: "",
NationCode: "",
Phone: "",
AllowSkipPhoneVerify: false,
AllowSkipWaitEmail: false,
Email: "",
Username: "",
Avatar: 0,
Status: 2,
Parents: nil,
Groups: nil,
Infos: nil,
Logins: logins,
SortID: 0,
Tags: nil,
}, &UserLogin2.ArgsCreateUser{
RegFrom: "weixin_app",
ReferrerNationCode: args.ReferrerNationCode,
ReferrerPhone: args.ReferrerPhone,
})
if err != nil {
errCode = "err_user_reg"
return
}
//标记新用户
isNewUser = true
//保存用户头像
updateUserAvatarByURL(openIDData.Headimgurl, userData.ID)
//反馈
return
}
// DataGetOpenIDByCode 获取微信OpenID数据
type DataGetOpenIDByCode struct {
//OpenID
OpenID string `json:"openid"`
//UnionID
UnionID string `json:"unionid"`
//昵称
Nickname string `json:"nickname"`
//头像地址
Headimgurl string `json:"headimgurl"`
}
// GetOpenIDByCode 获取微信OpenID
func GetOpenIDByCode(orgID int64, code string) (result DataGetOpenIDByCode, errCode string, err error) {
//获取商户配置
appID, appKey := getAppConfig(orgID)
//构建URL地址
postAuthURL := strings.ReplaceAll(userLoginAuthURL, "$1", appID)
postAuthURL = strings.ReplaceAll(postAuthURL, "$2", appKey)
postAuthURL = strings.ReplaceAll(postAuthURL, "$3", code)
//向微信发起请求
var bodyAuthByte []byte
bodyAuthByte, err = CoreHttp.GetData(postAuthURL, nil, "", false)
if err != nil {
errCode = "err_http"
return
}
//分配内存
result = DataGetOpenIDByCode{}
//解析数据
accessToken := gjson.GetBytes(bodyAuthByte, "access_token").String()
result.OpenID = gjson.GetBytes(bodyAuthByte, "openid").String()
if accessToken == "" || result.OpenID == "" {
errCode = "err_user_login_code"
err = errors.New(fmt.Sprint("access token or open id empty, body data: ", string(bodyAuthByte)))
return
}
//请求用户基本信息
postInfoURL := strings.ReplaceAll(userLoginAuthInfoURL, "$1", accessToken)
postInfoURL = strings.ReplaceAll(postInfoURL, "$2", result.OpenID)
//向微信发起请求
var bodyInfoByte []byte
bodyInfoByte, err = CoreHttp.GetData(postInfoURL, nil, "", false)
if err != nil {
errCode = "err_http"
return
}
//解析数据
result.OpenID = gjson.GetBytes(bodyInfoByte, "openid").String()
result.Nickname = gjson.GetBytes(bodyInfoByte, "nickname").String()
result.Headimgurl = gjson.GetBytes(bodyInfoByte, "headimgurl").String()
result.UnionID = gjson.GetBytes(bodyInfoByte, "unionid").String()
if result.UnionID == "" && result.OpenID == "" {
errCode = "err_user_login_code"
err = errors.New(fmt.Sprint("open id or unionid is empty, body data: ", string(bodyInfoByte)))
return
}
return
}
// 更新用户头像
func updateUserAvatarByURL(imgURL string, userID int64) {
if imgURL == "" {
return
}
//上传用户头像
fileData, _, err := BaseQiniu.UploadByURL(&BaseQiniu.ArgsUploadByURL{
URL: imgURL,
BucketName: "",
FileType: "",
IP: "0.0.0.0",
CreateInfo: CoreSQLFrom.FieldsFrom{},
UserID: userID,
OrgID: 0,
IsPublic: true,
ExpireAt: time.Time{},
ClaimInfos: nil,
Des: "",
})
if err != nil {
return
}
err = UserCore.UpdateUserAvatar(&UserCore.ArgsUpdateUserAvatar{
ID: userID,
OrgID: -1,
AvatarID: fileData.ID,
})
if err != nil {
return
}
}