一波更新
This commit is contained in:
164
trunk/center/usercenter/internal/wxuser/api.go
Normal file
164
trunk/center/usercenter/internal/wxuser/api.go
Normal file
@@ -0,0 +1,164 @@
|
||||
package wxuser
|
||||
|
||||
import (
|
||||
"common/remark"
|
||||
"common/resultStatus"
|
||||
"common/webServer"
|
||||
"goutil/logUtilPlus"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
//注册接口
|
||||
webServer.RegisterFunction(new(WxuserApi))
|
||||
}
|
||||
|
||||
func init() {
|
||||
moduleName := "WxuserApi"
|
||||
desc := "用户接口"
|
||||
author := "youjinlan"
|
||||
mendor := ""
|
||||
date := "2025-12-21 09:40:00"
|
||||
remark.RegisterModuleRemark(moduleName, desc, author, mendor, date)
|
||||
}
|
||||
|
||||
// UserApi 用户接口
|
||||
type WxuserApi struct {
|
||||
}
|
||||
|
||||
// ---------------------------------------- 接口 --------------------------------------------------
|
||||
func init() {
|
||||
moduleName := "WxuserApi"
|
||||
methodName := "LoginByWechat"
|
||||
skipVerifyTokenPage := true
|
||||
methodDesc := "微信登录"
|
||||
methodAuthor := "youjinlan"
|
||||
methodMendor := ""
|
||||
methodDate := "2025-01-21 09:40:00"
|
||||
methodInParam := []string{"string code int32 severId"}
|
||||
methodOutParam := `
|
||||
{
|
||||
"Code '类型:int'": "响应结果的状态值",
|
||||
"Message '类型:string'": "响应结果的状态值所对应的描述信息",
|
||||
"Data '类型:interface{}'": "响应结果的数据"
|
||||
{
|
||||
"Uid '类型:int64'": "用户对应区服的唯一Id",
|
||||
}
|
||||
}`
|
||||
remark.RegisterMethodRemark(moduleName, methodName, methodDesc, methodAuthor, methodMendor, methodDate, methodInParam, methodOutParam, skipVerifyTokenPage)
|
||||
}
|
||||
func (a *WxuserApi) LoginByWechat(code string, severId int32) (responseObj *webServer.ResponseObject) {
|
||||
responseObj = webServer.GetInitResponseObj()
|
||||
if code == "" {
|
||||
logUtilPlus.ErrorLog("Wrongcode,code为空")
|
||||
responseObj.SetResultStatus(resultStatus.APIDataError)
|
||||
return
|
||||
}
|
||||
res, uid := GetWechatsdkService().GetAccessToken(code)
|
||||
if res != resultStatus.Success {
|
||||
responseObj.SetResultStatus(res)
|
||||
}
|
||||
if uid != 0 {
|
||||
resultMap := make(map[string]any)
|
||||
resultMap["Uid"] = uid
|
||||
responseObj.SetData(resultMap)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func init() {
|
||||
moduleName := "WxuserApi"
|
||||
methodName := "LoginOut"
|
||||
skipVerifyTokenPage := true
|
||||
methodDesc := "下线通知"
|
||||
methodAuthor := "youjinlan"
|
||||
methodMendor := ""
|
||||
methodDate := "2025-01-21 11:00:00"
|
||||
methodInParam := []string{"string openid,int32 severId"}
|
||||
methodOutParam := `
|
||||
{
|
||||
"Code '类型:int'": "响应结果的状态值",
|
||||
"Message '类型:string'": "响应结果的状态值所对应的描述信息",
|
||||
"Data '类型:interface{}'": "响应结果的数据"
|
||||
{
|
||||
|
||||
}
|
||||
}`
|
||||
remark.RegisterMethodRemark(moduleName, methodName, methodDesc, methodAuthor, methodMendor, methodDate, methodInParam, methodOutParam, skipVerifyTokenPage)
|
||||
}
|
||||
|
||||
func (a *WxuserApi) LoginOut(uid int64) (responseObj *webServer.ResponseObject) {
|
||||
responseObj = webServer.GetInitResponseObj()
|
||||
if uid == 0 {
|
||||
logUtilPlus.ErrorLog("uid=0")
|
||||
responseObj.SetResultStatus(resultStatus.APIDataError)
|
||||
return
|
||||
}
|
||||
RecordLoginOut(uid)
|
||||
return
|
||||
}
|
||||
|
||||
func init() {
|
||||
moduleName := "WxuserApi"
|
||||
methodName := "SwitchSever"
|
||||
skipVerifyTokenPage := true
|
||||
methodDesc := "切服通知"
|
||||
methodAuthor := "youjinlan"
|
||||
methodMendor := ""
|
||||
methodDate := "2025-01-21 14:40:00"
|
||||
methodInParam := []string{"string openid,int32 oldseverId,int32 newseverId"}
|
||||
methodOutParam := `
|
||||
{
|
||||
"Code '类型:int'": "响应结果的状态值",
|
||||
"Message '类型:string'": "响应结果的状态值所对应的描述信息",
|
||||
"Data '类型:interface{}'": "响应结果的数据"
|
||||
{
|
||||
|
||||
}
|
||||
}`
|
||||
remark.RegisterMethodRemark(moduleName, methodName, methodDesc, methodAuthor, methodMendor, methodDate, methodInParam, methodOutParam, skipVerifyTokenPage)
|
||||
}
|
||||
|
||||
func (a *WxuserApi) SwitchSever(uid int64, oldseverId, newseverId int32) (responseObj *webServer.ResponseObject) {
|
||||
responseObj = webServer.GetInitResponseObj()
|
||||
if uid == 0 {
|
||||
logUtilPlus.ErrorLog("uid=0")
|
||||
responseObj.SetResultStatus(resultStatus.APIDataError)
|
||||
return
|
||||
}
|
||||
// RecordLoginOut(openid, oldseverId)
|
||||
// RecordLoginIn(openid, newseverId)
|
||||
return
|
||||
}
|
||||
|
||||
func init() {
|
||||
moduleName := "WxuserApi"
|
||||
methodName := "WatchAD"
|
||||
skipVerifyTokenPage := true
|
||||
methodDesc := "看广告通知"
|
||||
methodAuthor := "youjinlan"
|
||||
methodMendor := ""
|
||||
methodDate := "2025-01-22 10:40:00"
|
||||
methodInParam := []string{"string openid,int32 severId"}
|
||||
methodOutParam := `
|
||||
{
|
||||
"Code '类型:int'": "响应结果的状态值",
|
||||
"Message '类型:string'": "响应结果的状态值所对应的描述信息",
|
||||
"Data '类型:interface{}'": "响应结果的数据"
|
||||
{
|
||||
|
||||
}
|
||||
}`
|
||||
remark.RegisterMethodRemark(moduleName, methodName, methodDesc, methodAuthor, methodMendor, methodDate, methodInParam, methodOutParam, skipVerifyTokenPage)
|
||||
}
|
||||
|
||||
func (a *WxuserApi) WatchAD(uid int64) (responseObj *webServer.ResponseObject) {
|
||||
responseObj = webServer.GetInitResponseObj()
|
||||
if uid == 0 {
|
||||
logUtilPlus.ErrorLog("openid,openid为空")
|
||||
responseObj.SetResultStatus(resultStatus.APIDataError)
|
||||
return
|
||||
}
|
||||
RecordWatchAD(uid)
|
||||
return
|
||||
}
|
||||
217
trunk/center/usercenter/internal/wxuser/logic.go
Normal file
217
trunk/center/usercenter/internal/wxuser/logic.go
Normal file
@@ -0,0 +1,217 @@
|
||||
package wxuser
|
||||
|
||||
import (
|
||||
"common/connection"
|
||||
"goutil/logUtilPlus"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
// 用户缓存对象
|
||||
userInfoMap = make(map[string]*WxUserInfo)
|
||||
rwmu sync.RWMutex
|
||||
userRecordMap = make(map[int64]*RecordLoginOfWxUser)
|
||||
rwmu2 sync.RWMutex
|
||||
userADRecordMap = make(map[int64]*RecordWatchADOfWxUser)
|
||||
rwmu3 sync.RWMutex
|
||||
)
|
||||
|
||||
func GetUserByOpenId(openId string) (*WxUserInfo, error) {
|
||||
var userInfo *WxUserInfo
|
||||
func() *WxUserInfo {
|
||||
rwmu.RLock()
|
||||
defer rwmu.RUnlock()
|
||||
ok := true
|
||||
if userInfo, ok = userInfoMap[openId]; ok {
|
||||
return userInfo
|
||||
}
|
||||
return nil
|
||||
}()
|
||||
if userInfo != nil {
|
||||
return userInfo, nil
|
||||
}
|
||||
result := connection.GetUserDB().Where("openId = ?", openId).First(&userInfo)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return userInfo, nil
|
||||
}
|
||||
|
||||
// AddUser 添加用户
|
||||
// AddUser 添加新的用户到数据库中。
|
||||
// 参数 User: 包含用户信息的对象。
|
||||
func AddUser(user *WxUserInfo) {
|
||||
|
||||
//处理一些验证
|
||||
|
||||
// 写入到数据库
|
||||
result := connection.GetUserDB().Create(&user) // 通过数据的指针来创建
|
||||
if result.Error != nil {
|
||||
logUtilPlus.ErrorLog("添加用户失败 错误信息:", result.Error.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func AddUserCache(user *WxUserInfo) {
|
||||
rwmu.Lock()
|
||||
defer rwmu.Unlock()
|
||||
userInfoMap[user.OpenId] = user
|
||||
}
|
||||
|
||||
// 登录登出相关
|
||||
func AddUserRecordCache(userrecord *RecordLoginOfWxUser) {
|
||||
rwmu2.Lock()
|
||||
defer rwmu2.Unlock()
|
||||
userRecordMap[userrecord.Uid] = userrecord
|
||||
}
|
||||
|
||||
func GetUserRecord(uid int64) (*RecordLoginOfWxUser, error) {
|
||||
var userRecord *RecordLoginOfWxUser
|
||||
func() *RecordLoginOfWxUser {
|
||||
rwmu2.RLock()
|
||||
defer rwmu2.RUnlock()
|
||||
ok := true
|
||||
if userRecord, ok = userRecordMap[uid]; ok {
|
||||
return userRecord
|
||||
}
|
||||
return nil
|
||||
}()
|
||||
if userRecord != nil {
|
||||
return userRecord, nil
|
||||
}
|
||||
result := connection.GetUserDB().Where("uid = ?", uid).Last(&userRecord)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
//添加缓存
|
||||
func() {
|
||||
rwmu2.Lock()
|
||||
defer rwmu2.Unlock()
|
||||
userRecordMap[userRecord.Uid] = userRecord
|
||||
}()
|
||||
return userRecord, nil
|
||||
}
|
||||
|
||||
func AddUserRecord(userrecord *RecordLoginOfWxUser) (int64, error) {
|
||||
|
||||
//处理一些验证
|
||||
|
||||
// 写入到数据库
|
||||
result := connection.GetUserDB().Create(&userrecord) // 通过数据的指针来创建
|
||||
if result.Error != nil {
|
||||
logUtilPlus.ErrorLog("添加用户记录失败 错误信息:", result.Error.Error())
|
||||
}
|
||||
return userrecord.ID, nil
|
||||
}
|
||||
|
||||
func SaveUserRecord(userrecord *RecordLoginOfWxUser) (int64, error) {
|
||||
|
||||
//处理一些验证
|
||||
|
||||
// 写入到数据库
|
||||
result := connection.GetUserDB().Save(&userrecord) // 通过数据的指针来创建
|
||||
if result.Error != nil {
|
||||
logUtilPlus.ErrorLog("保存用户记录失败 错误信息:", result.Error.Error())
|
||||
}
|
||||
return userrecord.ID, nil
|
||||
}
|
||||
|
||||
// 看广告相关
|
||||
func AddUserADRecordCache(userADrecord *RecordWatchADOfWxUser) {
|
||||
rwmu3.Lock()
|
||||
defer rwmu3.Unlock()
|
||||
userADRecordMap[userADrecord.Uid] = userADrecord
|
||||
}
|
||||
|
||||
func GetUserADRecord(uid int64) (*RecordWatchADOfWxUser, error) {
|
||||
var userADRecord *RecordWatchADOfWxUser
|
||||
func() *RecordWatchADOfWxUser {
|
||||
rwmu3.RLock()
|
||||
defer rwmu3.RUnlock()
|
||||
ok := true
|
||||
if userADRecord, ok = userADRecordMap[uid]; ok {
|
||||
return userADRecord
|
||||
}
|
||||
return nil
|
||||
}()
|
||||
if userADRecord != nil {
|
||||
return userADRecord, nil
|
||||
}
|
||||
result := connection.GetUserDB().Where("uid = ?", uid).Last(&userADRecord)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
//添加缓存
|
||||
func() {
|
||||
rwmu3.Lock()
|
||||
defer rwmu3.Unlock()
|
||||
userADRecordMap[userADRecord.Uid] = userADRecord
|
||||
}()
|
||||
return userADRecord, nil
|
||||
}
|
||||
|
||||
func AddUserADRecord(userADrecord *RecordWatchADOfWxUser) (int64, error) {
|
||||
|
||||
//处理一些验证
|
||||
|
||||
// 写入到数据库
|
||||
result := connection.GetUserDB().Create(&userADrecord) // 通过数据的指针来创建
|
||||
if result.Error != nil {
|
||||
logUtilPlus.ErrorLog("添加用户看广告记录失败 错误信息:", result.Error.Error())
|
||||
}
|
||||
return userADrecord.ID, nil
|
||||
}
|
||||
|
||||
func SaveUserADRecord(userADrecord *RecordWatchADOfWxUser) (int64, error) {
|
||||
|
||||
//处理一些验证
|
||||
|
||||
// 写入到数据库
|
||||
result := connection.GetUserDB().Save(&userADrecord) // 通过数据的指针来创建
|
||||
if result.Error != nil {
|
||||
logUtilPlus.ErrorLog("保存用户看广告记录失败 错误信息:", result.Error.Error())
|
||||
}
|
||||
return userADrecord.ID, nil
|
||||
}
|
||||
|
||||
// 区服相关
|
||||
func AddUserSeverInfo(userSeverInfo *WxUserSeverInfo) (int64, error) {
|
||||
|
||||
//处理一些验证
|
||||
|
||||
// 写入到数据库
|
||||
result := connection.GetUserDB().Create(&userSeverInfo) // 通过数据的指针来创建
|
||||
if result.Error != nil {
|
||||
logUtilPlus.ErrorLog("添加用户区服记录失败 错误信息:", result.Error.Error())
|
||||
}
|
||||
return userSeverInfo.ID, nil
|
||||
}
|
||||
|
||||
func SaveUserSeverInfo(userSeverInfo *WxUserSeverInfo) (int64, error) {
|
||||
|
||||
//处理一些验证
|
||||
|
||||
// 写入到数据库
|
||||
result := connection.GetUserDB().Save(&userSeverInfo) // 通过数据的指针来创建
|
||||
if result.Error != nil {
|
||||
logUtilPlus.ErrorLog("保存用户区服记录失败 错误信息:", result.Error.Error())
|
||||
}
|
||||
return userSeverInfo.ID, nil
|
||||
}
|
||||
|
||||
func GetUserSeverInfo() (*WxUserSeverInfo, error) {
|
||||
var userSeverInfoRecord *WxUserSeverInfo
|
||||
result := connection.GetUserDB().Last(&userSeverInfoRecord)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return userSeverInfoRecord, nil
|
||||
}
|
||||
|
||||
func GetUserSeverInfoByIds(openId string, severId int32) (*WxUserSeverInfo, error) {
|
||||
var userSeverInfoRecord *WxUserSeverInfo
|
||||
result := connection.GetUserDB().Where("openId = ? AND severId = ?", openId, severId).First(&userSeverInfoRecord)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return userSeverInfoRecord, nil
|
||||
}
|
||||
80
trunk/center/usercenter/internal/wxuser/user.go
Normal file
80
trunk/center/usercenter/internal/wxuser/user.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package wxuser
|
||||
|
||||
import (
|
||||
"common/connection"
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
func init() {
|
||||
//注册数据库
|
||||
connection.RegisterDBModel(&WxUserInfo{})
|
||||
connection.RegisterDBModel(&RecordLoginOfWxUser{})
|
||||
connection.RegisterDBModel(&RecordWatchADOfWxUser{})
|
||||
connection.RegisterDBModel(&WxUserSeverInfo{})
|
||||
}
|
||||
|
||||
type WechatTokens struct {
|
||||
Errcode int32 `json:"errcode"`
|
||||
Errmsg string `json:"errmsg"`
|
||||
AccessToken string `json:"access_token"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
ExpiresIn int64 `json:"expires_in"`
|
||||
OpenId string `json:"openid"`
|
||||
UnionId string `json:"unionid"`
|
||||
}
|
||||
|
||||
type WxUserInfo struct {
|
||||
Nickname string `gorm:"column:nickname;comment:昵称" json:"nickname"`
|
||||
Sex string `gorm:"column:sex;comment:性别" json:"sex"`
|
||||
Language int64 `gorm:"column:language;comment:语言" json:"language"`
|
||||
city string `gorm:"column:city;comment:城市" json:"city"`
|
||||
province string `gorm:"column:province;comment:省份" json:"province"`
|
||||
country string `gorm:"column:country;comment:国籍" json:"country"`
|
||||
headimgurl string `gorm:"column:headimgurl;comment:头像" json:"headimgurl"`
|
||||
OpenId string `gorm:"column:openId;comment:openId;primaryKey" json:"openid"`
|
||||
UnionId string `gorm:"column:unionId;comment:unionId" json:"unionid"`
|
||||
}
|
||||
|
||||
// 同一个玩家在不同的区服Uid是不同的
|
||||
type WxUserSeverInfo struct {
|
||||
ID int64 `gorm:"column:id;primary_key;comment:自增索引;autoIncrementIncrement" json:"id"`
|
||||
OpenId string `gorm:"column:openId;comment:openId" json:"openId"`
|
||||
SeverId int32 `gorm:"column:severId;comment:区服Id" json:"severId"`
|
||||
Uid int64 `gorm:"column:uid;comment:用户唯一Id" json:"uid"`
|
||||
}
|
||||
|
||||
// 登录相关的记录
|
||||
type RecordLoginOfWxUser struct {
|
||||
ID int64 `gorm:"column:id;primary_key;comment:自增索引;autoIncrementIncrement" json:"id"`
|
||||
Uid int64 `gorm:"column:uid;comment:用户唯一Id" json:"uid"`
|
||||
LoginInTime int64 `gorm:"column:loginintime;comment:登录时间" json:"loginintime"`
|
||||
LoginOutTime int64 `gorm:"column:loginouttime;comment:登出时间" json:"loginouttime"`
|
||||
PlayTimes int64 `gorm:"column:playtimes;comment:游玩时长" json:"playtimes"`
|
||||
}
|
||||
|
||||
// 看广告相关记录
|
||||
// 记录日期便于按天统计
|
||||
type RecordWatchADOfWxUser struct {
|
||||
ID int64 `gorm:"column:id;primary_key;comment:自增索引;autoIncrementIncrement" json:"id"`
|
||||
RecordDate time.Time `gorm:"column:recorddate;type:date;comment:记录日期" json:"recorddate"`
|
||||
Uid int64 `gorm:"column:uid;comment:用户唯一Id" json:"uid"`
|
||||
WatchADNum int32 `gorm:"column:watchadnum;comment:看广告次数" json:"watchadnum"`
|
||||
}
|
||||
|
||||
func (WxUserInfo) TableName() string {
|
||||
return "wxuserinfo"
|
||||
}
|
||||
|
||||
func (r RecordLoginOfWxUser) TableName(db *gorm.DB) string {
|
||||
return "recordloginofwxuser"
|
||||
}
|
||||
|
||||
func (RecordWatchADOfWxUser) TableName() string {
|
||||
return "recordwatchadofwxuser"
|
||||
}
|
||||
|
||||
func (WxUserSeverInfo) TableName() string {
|
||||
return "wxuserseverinfo"
|
||||
}
|
||||
80
trunk/center/usercenter/internal/wxuser/userrecord.go
Normal file
80
trunk/center/usercenter/internal/wxuser/userrecord.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package wxuser
|
||||
|
||||
import (
|
||||
"common/resultStatus"
|
||||
"goutil/logUtilPlus"
|
||||
"time"
|
||||
)
|
||||
|
||||
func RecordLoginIn(uid int64) {
|
||||
nowTime := time.Now().Unix()
|
||||
// if userLastRecord, _ := GetUserRecord(uid); userLastRecord != nil {
|
||||
// if userLastRecord.LoginOutTime == 0 {
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
userRecord := &RecordLoginOfWxUser{}
|
||||
userRecord.LoginInTime = nowTime
|
||||
AddUserRecord(userRecord)
|
||||
AddUserRecordCache(userRecord)
|
||||
}
|
||||
|
||||
func RecordLoginOut(uid int64) resultStatus.ResultStatus {
|
||||
var userRecord *RecordLoginOfWxUser
|
||||
if userRecord, _ = GetUserRecord(uid); userRecord == nil {
|
||||
return resultStatus.PlayerNotExist
|
||||
}
|
||||
if userRecord.LoginOutTime != 0 {
|
||||
logUtilPlus.ErrorLog("PlayerIsLoginOut,uid=%d", uid)
|
||||
return resultStatus.PlayerIsLoginOut
|
||||
}
|
||||
go func() {
|
||||
nowTime := time.Now().Unix()
|
||||
userRecord.LoginOutTime = nowTime
|
||||
playTime := nowTime - userRecord.LoginInTime
|
||||
userRecord.PlayTimes += playTime
|
||||
SaveUserRecord(userRecord)
|
||||
}()
|
||||
return resultStatus.Success
|
||||
}
|
||||
|
||||
func RecordWatchAD(uid int64) resultStatus.ResultStatus {
|
||||
nowTime := time.Now()
|
||||
userADRecord := &RecordWatchADOfWxUser{}
|
||||
if userLastADRecord, _ := GetUserADRecord(uid); userLastADRecord != nil {
|
||||
if nowTime.Format("2006-01-02") == userLastADRecord.RecordDate.Format("2006-01-02") {
|
||||
userLastADRecord.WatchADNum++
|
||||
SaveUserADRecord(userLastADRecord)
|
||||
return resultStatus.Success
|
||||
}
|
||||
}
|
||||
userADRecord.RecordDate = nowTime
|
||||
userADRecord.WatchADNum += 1
|
||||
AddUserADRecord(userADRecord)
|
||||
AddUserADRecordCache(userADRecord)
|
||||
return resultStatus.Success
|
||||
}
|
||||
|
||||
func CreatUid(openId string, severId int32) int64 {
|
||||
var lastId int64
|
||||
if userRecord, _ := GetUserSeverInfoByIds(openId, severId); userRecord != nil {
|
||||
return userRecord.Uid
|
||||
}
|
||||
if userLastRecord, _ := GetUserSeverInfo(); userLastRecord != nil {
|
||||
lastId = userLastRecord.ID
|
||||
}
|
||||
userSeverInfo := &WxUserSeverInfo{}
|
||||
userSeverInfo.OpenId = openId
|
||||
userSeverInfo.SeverId = severId
|
||||
userSeverInfo.Uid = lastId + 1 + 100000000
|
||||
AddUserSeverInfo(userSeverInfo)
|
||||
return userSeverInfo.Uid
|
||||
}
|
||||
|
||||
// func init() {
|
||||
// testRecordWatchAD()
|
||||
// }
|
||||
|
||||
func testRecordWatchAD() {
|
||||
//RecordWatchAD("hell)
|
||||
}
|
||||
82
trunk/center/usercenter/internal/wxuser/wechatsdkservice.go
Normal file
82
trunk/center/usercenter/internal/wxuser/wechatsdkservice.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package wxuser
|
||||
|
||||
import (
|
||||
configYaml "common/configsYaml"
|
||||
"common/resultStatus"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"goutil/logUtilPlus"
|
||||
"goutil/webUtil"
|
||||
)
|
||||
|
||||
const (
|
||||
Url = "https://api.weixin.qq.com/sns/"
|
||||
)
|
||||
|
||||
type WechatsdkService struct {
|
||||
}
|
||||
|
||||
// 通过code来换取assesstoken
|
||||
func (w *WechatsdkService) GetAccessToken(code string) (resultStatus.ResultStatus, int64) {
|
||||
weburl := fmt.Sprintf("%s%s", Url, "oauth2/access_token") //"https://api.weixin.qq.com/sns/oauth2/access_token"
|
||||
data := make(map[string]string)
|
||||
data["appid"] = configYaml.GetWxconfig().AppId
|
||||
data["secret"] = configYaml.GetWxconfig().AppSecret
|
||||
data["code"] = code
|
||||
data["grant_type"] = "authorization_code"
|
||||
status, resp, err := webUtil.GetWebData2(weburl, data, nil, nil)
|
||||
if status != 200 || err != nil {
|
||||
return resultStatus.RequestError, 0
|
||||
}
|
||||
|
||||
if len(resp) == 0 {
|
||||
logUtilPlus.ErrorLog("The result is empty, but we expect not empty")
|
||||
return resultStatus.EmptyResp, 0
|
||||
}
|
||||
logUtilPlus.ErrorLog("resp=%s", string(resp))
|
||||
tokens := &WechatTokens{}
|
||||
if err := json.Unmarshal(resp, tokens); err != nil {
|
||||
logUtilPlus.ErrorLog("Unmarshal is error, err:%s", err)
|
||||
return resultStatus.UnmarshalError, 0
|
||||
}
|
||||
if tokens.Errcode != 0 || tokens.Errmsg != "" {
|
||||
logUtilPlus.ErrorLog("errcode:%d, msg:%s,", tokens.Errcode, tokens.Errmsg)
|
||||
return resultStatus.RequestError, 0
|
||||
}
|
||||
Uid := CreatUid(tokens.OpenId, 1)
|
||||
go func() {
|
||||
if userInfo, _ := GetUserByOpenId(tokens.OpenId); userInfo == nil {
|
||||
w.GetUserInfo(tokens.AccessToken, tokens.OpenId)
|
||||
}
|
||||
}()
|
||||
go RecordLoginIn(Uid)
|
||||
return resultStatus.Success, Uid
|
||||
}
|
||||
|
||||
// 换取用户信息
|
||||
func (w *WechatsdkService) GetUserInfo(accessToken, openid string) {
|
||||
weburl := fmt.Sprintf("%s%s", Url, "userinfo") //"https://api.weixin.qq.com/sns/userinfo"
|
||||
data := make(map[string]string)
|
||||
data["appid"] = configYaml.GetWxconfig().AppId
|
||||
data["access_token"] = accessToken
|
||||
data["openid"] = openid
|
||||
status, resp, err := webUtil.GetWebData2(weburl, data, nil, nil)
|
||||
if status != 200 || err != nil {
|
||||
logUtilPlus.ErrorLog("Wrongstatus. status:%d, err:%s", status, err)
|
||||
}
|
||||
if len(resp) == 0 {
|
||||
logUtilPlus.ErrorLog("The result is empty, but we expect not empty")
|
||||
}
|
||||
var userInfo *WxUserInfo
|
||||
if err := json.Unmarshal(resp, userInfo); err != nil {
|
||||
logUtilPlus.ErrorLog("Unmarshal is error, err:%s", err)
|
||||
return
|
||||
}
|
||||
AddUser(userInfo)
|
||||
}
|
||||
|
||||
var wechatsdkservice *WechatsdkService
|
||||
|
||||
func GetWechatsdkService() *WechatsdkService {
|
||||
return wechatsdkservice
|
||||
}
|
||||
Reference in New Issue
Block a user