黄梓健 5 years ago
parent
commit
49289f77e1
  1. 16
      controllers/client/login.go
  2. 1
      controllers/client/login_test.go
  3. 1
      go.mod
  4. 2
      go.sum
  5. 58
      libs/im/im.go
  6. 11
      libs/im/im_test.go
  7. 4
      models/live_config.go
  8. 3
      models/user.go
  9. 13
      utils/utils.go

16
controllers/client/login.go

@ -1,8 +1,10 @@
package client
import (
"fmt"
mpoauth2 "github.com/chanxuehong/wechat/mp/oauth2"
"hudongzhuanjia/controllers"
"hudongzhuanjia/libs/im"
"hudongzhuanjia/libs/jwt"
"hudongzhuanjia/libs/wechat"
"hudongzhuanjia/models"
@ -116,7 +118,6 @@ func (t *UserCtl) WxLogin() {
user.Country = info.Country
user.CreatedAt = time.Now()
user.UpdatedAt = time.Now()
//user.Openid = info.OpenId
}
err = user.SaveAndUpdateWithOpenId()
@ -192,7 +193,7 @@ func (t *UserCtl) Login() {
user.Country = info.Country
user.CreatedAt = time.Now()
user.UpdatedAt = time.Now()
//user.Openid = info.OpenId
}
err := user.SaveAndUpdateWithOpenId()
@ -202,6 +203,10 @@ func (t *UserCtl) Login() {
t.CheckErr(err)
t.SetSession(define.TOKEN, jwtToken)
sign, err := im.AccountImport(fmt.Sprintf("%d", user.Id), user.Nickname, user.Avatar)
t.CheckErr(err)
user.Sig = sign
t.JSON(map[string]interface{}{
"user": user,
"token": jwtToken,
@ -270,7 +275,12 @@ func (t *UserAuthCtl) SaveUserInfo() {
user.Country = info.Country
_, err := user.SaveUserInfo(uid)
t.CheckErr(err)
t.SUCCESS("保存成功")
sign, err := im.AccountImport(fmt.Sprintf("%d", user.Id), user.Avatar, user.Nickname)
t.CheckErr(err)
user.Sig = sign
t.JSON(user)
}
//退出

1
controllers/client/login_test.go

@ -48,7 +48,6 @@ func TestEntryLogin(t *testing.T) {
So(err, ShouldNotBeNil)
So(claims.ActivityId, ShouldEqual, conf.ActivityId)
So(claims.AccountType, ShouldEqual, define.TYPE_ENTRY)
So(claims.Username, ShouldEqual, conf.EntryAccount)
// 登录失败的场景
// 录入人员不存在

1
go.mod

@ -22,6 +22,7 @@ require (
github.com/swaggo/http-swagger v0.0.0-20190614090009-c2865af9083e
github.com/swaggo/swag v1.6.3
github.com/tencentyun/cos-go-sdk-v5 v0.0.0-20190926121715-a33db3b0dede
github.com/tencentyun/tls-sig-api-v2-golang v1.0.0
github.com/xormplus/xorm v0.0.0-20190419084314-284871d688a4
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/multierr v1.2.0 // indirect

2
go.sum

@ -253,6 +253,8 @@ github.com/tealeg/xlsx v1.0.3 h1:BXsDIQYBPq2HgbwUxrsVXIrnO0BDxmsdUfHSfvwfBuQ=
github.com/tealeg/xlsx v1.0.3/go.mod h1:uxu5UY2ovkuRPWKQ8Q7JG0JbSivrISjdPzZQKeo74mA=
github.com/tencentyun/cos-go-sdk-v5 v0.0.0-20190926121715-a33db3b0dede h1:4FiecrX2TsfWVkXozpV2JOOI09KvC4jD1JotxY2bSnQ=
github.com/tencentyun/cos-go-sdk-v5 v0.0.0-20190926121715-a33db3b0dede/go.mod h1:wk2XFUg6egk4tSDNZtXeKfe2G6690UVyt163PuUxBZk=
github.com/tencentyun/tls-sig-api-v2-golang v1.0.0 h1:NavMw9XO2iCLv8hTKaJW2kTaGR2SdNljMABbe39yu6Q=
github.com/tencentyun/tls-sig-api-v2-golang v1.0.0/go.mod h1:u7WiArmCTXTaQAHJwAOaLgpJ5e2xdY5/cgMEy3ubL60=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/ugorji/go v1.1.5-pre h1:jyJKFOSEbdOc2HODrf2qcCkYOdq7zzXqA9bhW5oV4fM=
github.com/ugorji/go v1.1.5-pre/go.mod h1:FwP/aQVg39TXzItUBMwnWp9T9gPQnXw4Poh4/oBQZ/0=

58
libs/im/im.go

@ -0,0 +1,58 @@
package im
import (
"encoding/json"
"errors"
"fmt"
"github.com/kirinlabs/HttpRequest"
"github.com/tencentyun/tls-sig-api-v2-golang/tencentyun"
"hudongzhuanjia/utils"
)
var (
//SdkAppid = 1400345581
//SdkAppKey = "e3d095ce8253fe18109d6c1ad068907978cac0a428fbad181d8958631b78e930"
SdkAppKey = "9683fc9d2e50857f33c7ef5431942458f2dbc7042ba526f87042092afb646331"
SdkAppid = 1400337419
TxImHost = "https://console.tim.qq.com"
)
type AccountImportResult struct {
ActionStatus string `json:"ActionStatus"`
ErrorCode int `json:"ErrorCode"`
ErrorInfo string `json:"ErrorInfo"`
}
type AccountImportParam struct {
Identifier string `json:"Identifier"`
Nick string `json:"Nick"`
FaceUrl string `json:"FaceUrl"`
}
func AccountImport(identifier, nickname, faceurl string) (string, error) {
sign, err := tencentyun.GenSig(SdkAppid, SdkAppKey, "admin", 86400*180)
if err != nil {
return "", err
}
random := utils.RandomStr(32)
u := fmt.Sprintf("%s/v4/im_open_login_svc/account_import?sdkappid=%d&identifier=admin&usersig=%s&random=%s&contenttype=json", TxImHost, SdkAppid, sign, random)
bs, _ := json.Marshal(&AccountImportParam{
Identifier: identifier,
Nick: nickname,
FaceUrl: faceurl,
})
resp, err := HttpRequest.NewRequest().Debug(true).JSON().Post(u, string(bs))
if err != nil {
return "", err
}
res := AccountImportResult{}
err = resp.Json(&res)
if err != nil {
return "", err
}
if res.ErrorCode != 0 {
return "", errors.New(res.ErrorInfo)
}
return tencentyun.GenSig(SdkAppid, SdkAppKey, identifier, 86400000)
}

11
libs/im/im_test.go

@ -0,0 +1,11 @@
package im
import (
"fmt"
"testing"
)
func TestAccountImport(t *testing.T) {
sig, err := AccountImport("test1", "tommy", "http://a.cphotos.bdimg.com/timg?image&quality=100&size=b4000_4000&sec=1585813714&di=c76777c95455780deffa04d28b25cb70&src=http://b-ssl.duitang.com/uploads/item/201412/28/20141228171331_L5T2n.jpeg")
fmt.Println(sig, err)
}

4
models/live_config.go

@ -32,6 +32,10 @@ type LiveConfig struct {
ActivityDesc string `json:"activity_desc" xorm:"not null default '' comment('活动介绍页图片') VARCHAR(255)"`
SharePosterImg string `json:"share_poster_img" xorm:"not null default '' comment('分享的海报图片') VARCHAR(255)"`
Status int `json:"status" xorm:"not null default 1 comment('直播的状态1未开始2进行中3已结束') VARCHAR(255)"`
Announcement string `json:"announcement" xorm:"not null default '' comment('公告内容') VARCHAR(255)"`
WatchNum int `json:"watch_num" xorm:"not null default 0 comment('观看人数') BIGINT(11)"`
ImGroupId int64 `json:"im_group_id" xorm:"not null default 0 comment('腾讯im聊天群id') BIGINT(11)"`
ImGroupName string `json:"im_group_name" xorm:"not null default '' comment('聊天群名称') VARCHAR(255)"`
}
func (t *LiveConfig) TableName() string {

3
models/user.go

@ -10,7 +10,7 @@ const UserTN = TableNamePrefix + "user"
//用户
type User struct {
Id int64 `json:"id" xorm:"pk autoincr"`
Id int64 `json:"id" xorm:"not null pk autoincr INT(11)"`
Phone string `json:"phone" description:"手机号码"`
Code string `json:"code" description:"验证码"`
City string `json:"city" xorm:"not null"`
@ -26,6 +26,7 @@ type User struct {
ActivityId int64 `json:"activity_id" description:"主活动id"` // 不存在
AreaId int64 `json:"area_id" description:"地区id"` // 不存在
AreaName string `json:"area_name" description:"地区名字"`
Sig string `json:"sig" xorm:"-"`
IsDelete bool `json:"is_delete" xorm:"default(0)" description:"是否删除"`
CreatedAt time.Time `json:"created_at" xorm:"created" description:"创建时间"`
UpdatedAt time.Time `json:"updated_at" xorm:"updated" description:"更新时间"`

13
utils/utils.go

@ -7,10 +7,12 @@ import (
"github.com/skip2/go-qrcode"
"hudongzhuanjia/utils/define"
"io/ioutil"
"math/rand"
"net/http"
"os"
"strconv"
"strings"
"time"
)
func Int(str string) int {
@ -106,3 +108,14 @@ func CountDownFormat(duration int64) string {
m = m - h*60 // 剩余分钟
return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
}
func RandomStr(length int) string {
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
bytes := []byte(str)
var result []byte
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < length; i++ {
result = append(result, bytes[r.Intn(len(bytes))])
}
return string(result)
}
Loading…
Cancel
Save